mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
GameEvent
GameEvent
This commit is contained in:
@@ -9,15 +9,19 @@ namespace TEngine
|
||||
class EventDelegateData
|
||||
{
|
||||
private int m_eventType = 0;
|
||||
public List<Delegate> m_listExist = new List<Delegate>();
|
||||
private List<Delegate> m_listExist = new List<Delegate>();
|
||||
private List<Delegate> m_addList = new List<Delegate>();
|
||||
private List<Delegate> m_deleteList = new List<Delegate>();
|
||||
private bool m_isExcute = false;
|
||||
private bool m_dirty = false;
|
||||
|
||||
public EventDelegateData(int evnetType)
|
||||
/// <summary>
|
||||
/// 构造函数。
|
||||
/// </summary>
|
||||
/// <param name="eventType"></param>
|
||||
public EventDelegateData(int eventType)
|
||||
{
|
||||
m_eventType = evnetType;
|
||||
m_eventType = eventType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -232,9 +236,12 @@ namespace TEngine
|
||||
/// <summary>
|
||||
/// 封装消息的底层分发和注册。
|
||||
/// </summary>
|
||||
class EventDispatcher
|
||||
public class EventDispatcher
|
||||
{
|
||||
static Dictionary<int, EventDelegateData> m_eventTable = new Dictionary<int, EventDelegateData>();
|
||||
/// <summary>
|
||||
/// 事件Table
|
||||
/// </summary>
|
||||
private static Dictionary<int, EventDelegateData> s_eventTable = new Dictionary<int, EventDelegateData>();
|
||||
|
||||
#region 事件管理接口
|
||||
/// <summary>
|
||||
@@ -245,10 +252,10 @@ namespace TEngine
|
||||
/// <returns></returns>
|
||||
public bool AddEventListener(int eventType, Delegate handler)
|
||||
{
|
||||
if (!m_eventTable.TryGetValue(eventType, out var data))
|
||||
if (!s_eventTable.TryGetValue(eventType, out var data))
|
||||
{
|
||||
data = new EventDelegateData(eventType);
|
||||
m_eventTable.Add(eventType, data);
|
||||
s_eventTable.Add(eventType, data);
|
||||
}
|
||||
|
||||
return data.AddHandler(handler);
|
||||
@@ -261,7 +268,7 @@ namespace TEngine
|
||||
/// <param name="handler"></param>
|
||||
public void RemoveEventListener(int eventType, Delegate handler)
|
||||
{
|
||||
if (m_eventTable.TryGetValue(eventType, out var data))
|
||||
if (s_eventTable.TryGetValue(eventType, out var data))
|
||||
{
|
||||
data.RmvHandler(handler);
|
||||
}
|
||||
@@ -275,7 +282,7 @@ namespace TEngine
|
||||
/// <param name="eventType">事件类型。</param>
|
||||
public void Send(int eventType)
|
||||
{
|
||||
if (m_eventTable.TryGetValue(eventType, out var d))
|
||||
if (s_eventTable.TryGetValue(eventType, out var d))
|
||||
{
|
||||
d.Callback();
|
||||
}
|
||||
@@ -289,7 +296,7 @@ namespace TEngine
|
||||
/// <typeparam name="TArg1"></typeparam>
|
||||
public void Send<TArg1>(int eventType, TArg1 arg1)
|
||||
{
|
||||
if (m_eventTable.TryGetValue(eventType, out var d))
|
||||
if (s_eventTable.TryGetValue(eventType, out var d))
|
||||
{
|
||||
d.Callback(arg1);
|
||||
}
|
||||
@@ -305,7 +312,7 @@ namespace TEngine
|
||||
/// <typeparam name="TArg2"></typeparam>
|
||||
public void Send<TArg1, TArg2>(int eventType, TArg1 arg1, TArg2 arg2)
|
||||
{
|
||||
if (m_eventTable.TryGetValue(eventType, out var d))
|
||||
if (s_eventTable.TryGetValue(eventType, out var d))
|
||||
{
|
||||
d.Callback(arg1, arg2);
|
||||
}
|
||||
@@ -323,7 +330,7 @@ namespace TEngine
|
||||
/// <typeparam name="TArg3"></typeparam>
|
||||
public void Send<TArg1, TArg2, TArg3>(int eventType, TArg1 arg1, TArg2 arg2, TArg3 arg3)
|
||||
{
|
||||
if (m_eventTable.TryGetValue(eventType, out var d))
|
||||
if (s_eventTable.TryGetValue(eventType, out var d))
|
||||
{
|
||||
d.Callback(arg1, arg2, arg3);
|
||||
}
|
||||
@@ -343,7 +350,7 @@ namespace TEngine
|
||||
/// <typeparam name="TArg4"></typeparam>
|
||||
public void Send<TArg1, TArg2, TArg3, TArg4>(int eventType, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4)
|
||||
{
|
||||
if (m_eventTable.TryGetValue(eventType, out var d))
|
||||
if (s_eventTable.TryGetValue(eventType, out var d))
|
||||
{
|
||||
d.Callback(arg1, arg2, arg3, arg4);
|
||||
}
|
||||
@@ -365,7 +372,7 @@ namespace TEngine
|
||||
/// <typeparam name="TArg5"></typeparam>
|
||||
public void Send<TArg1, TArg2, TArg3, TArg4, TArg5>(int eventType, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5)
|
||||
{
|
||||
if (m_eventTable.TryGetValue(eventType, out var d))
|
||||
if (s_eventTable.TryGetValue(eventType, out var d))
|
||||
{
|
||||
d.Callback(arg1, arg2, arg3, arg4, arg5);
|
||||
}
|
||||
|
@@ -2,49 +2,58 @@
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件实体数据。
|
||||
/// </summary>
|
||||
internal class EventEntryData
|
||||
{
|
||||
public object InterfaceWrap;
|
||||
};
|
||||
|
||||
class EventMgr
|
||||
/// <summary>
|
||||
/// 事件管理器。
|
||||
/// </summary>
|
||||
public class EventMgr
|
||||
{
|
||||
private EventDispatcher m_dispatcher = new EventDispatcher();
|
||||
|
||||
/// <summary>
|
||||
/// 封装了调用的代理函数
|
||||
/// </summary>
|
||||
private Dictionary<string, EventEntryData> m_entry = new Dictionary<string, EventEntryData>();
|
||||
|
||||
/// <summary>
|
||||
/// 事件管理器获取接口。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">接口类型。</typeparam>
|
||||
/// <returns>接口实例。</returns>
|
||||
public T GetInterface<T>()
|
||||
{
|
||||
string typeName = typeof(T).FullName;
|
||||
EventEntryData entry;
|
||||
if (m_entry.TryGetValue(typeName, out entry))
|
||||
if (typeName != null && m_entry.TryGetValue(typeName, out var entry))
|
||||
{
|
||||
return (T)entry.InterfaceWrap;
|
||||
}
|
||||
|
||||
return default(T);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册wrap的函数。
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="callerWrap"></param>
|
||||
/// <typeparam name="T">Wrap接口类型。</typeparam>
|
||||
/// <param name="callerWrap">callerWrap接口名字。</param>
|
||||
public void RegWrapInterface<T>(T callerWrap)
|
||||
{
|
||||
string typeName = typeof(T).FullName;
|
||||
|
||||
var entry = new EventEntryData();
|
||||
entry.InterfaceWrap = callerWrap;
|
||||
if (typeName != null)
|
||||
{
|
||||
m_entry.Add(typeName, entry);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分发注册器。
|
||||
/// </summary>
|
||||
public EventDispatcher Dispatcher => m_dispatcher;
|
||||
public EventDispatcher Dispatcher { get; } = new EventDispatcher();
|
||||
}
|
||||
}
|
@@ -7,15 +7,18 @@ namespace TEngine
|
||||
/// </summary>
|
||||
public static class GameEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// 全局事件管理器。
|
||||
/// </summary>
|
||||
private static readonly EventMgr EventMgr = new EventMgr();
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#region 细分的注册接口
|
||||
|
||||
/// <summary>
|
||||
/// 增加事件监听。
|
||||
/// </summary>
|
||||
/// <param name="eventType">事件类型。</param>
|
||||
/// <param name="handler">事件Handler。</param>
|
||||
/// <returns>是否监听成功。</returns>
|
||||
public static bool AddEventListener(int eventType, Action handler)
|
||||
{
|
||||
return EventMgr.Dispatcher.AddEventListener(eventType, handler);
|
||||
|
@@ -84,5 +84,13 @@ namespace TEngine
|
||||
AddEvent(eventType, handler);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddUIEvent<T, U, V, W, X>(int eventType, Action<T, U, V, W, X> handler)
|
||||
{
|
||||
if (GameEvent.AddEventListener(eventType, handler))
|
||||
{
|
||||
AddEvent(eventType, handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -2,29 +2,51 @@
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// 运行时字符串ID。
|
||||
/// </summary>
|
||||
public static class StringId
|
||||
{
|
||||
private static readonly Dictionary<string, int> eventTypeHashMap = new Dictionary<string, int>();
|
||||
private static readonly Dictionary<int, string> eventHashToStringMap = new Dictionary<int, string>();
|
||||
/// <summary>
|
||||
/// Key->字符串 | Value->Id (Table)
|
||||
/// </summary>
|
||||
private static readonly Dictionary<string, int> EventTypeHashMap = new Dictionary<string, int>();
|
||||
/// <summary>
|
||||
/// Key->Id | Value->字符串 (Table)
|
||||
/// </summary>
|
||||
private static readonly Dictionary<int, string> EventHashToStringMap = new Dictionary<int, string>();
|
||||
/// <summary>
|
||||
/// 当前Id。
|
||||
/// </summary>
|
||||
private static int _currentId = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 字符串转HashId。
|
||||
/// </summary>
|
||||
/// <param name="val">字符串Value。</param>
|
||||
/// <returns>HashId。</returns>
|
||||
public static int StringToHash(string val)
|
||||
{
|
||||
if (eventTypeHashMap.TryGetValue(val, out var hashId))
|
||||
if (EventTypeHashMap.TryGetValue(val, out var hashId))
|
||||
{
|
||||
return hashId;
|
||||
}
|
||||
|
||||
hashId = ++_currentId;
|
||||
eventTypeHashMap[val] = hashId;
|
||||
eventHashToStringMap[hashId] = val;
|
||||
EventTypeHashMap[val] = hashId;
|
||||
EventHashToStringMap[hashId] = val;
|
||||
|
||||
return hashId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// HashId转字符串。
|
||||
/// </summary>
|
||||
/// <param name="hash">HashId。</param>
|
||||
/// <returns>字符串。</returns>
|
||||
public static string HashToString(int hash)
|
||||
{
|
||||
if (eventHashToStringMap.TryGetValue(hash, out var value))
|
||||
if (EventHashToStringMap.TryGetValue(hash, out var value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
@@ -38,10 +38,6 @@ namespace TEngine
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否存在对象池。
|
||||
/// </summary>
|
||||
|
Reference in New Issue
Block a user