using System; using System.Collections.Generic; using UnityEngine; public class EventSvcHandler { private bool GetNeedDebugLog() => CombatScenarioEventOS.Instance.GetNeedDebugLog(); private readonly Dictionary actionBroadcast = new(); // 广播事件地址 // ================================ // 1. 定义事件类型 // ================================ private readonly Dictionary actionBindMap = new() // 事件类型类型绑定 { // ========== 系统事件 ========== { EVENT_ENUM.EVENT_LET_OS_DEAL_CARD, typeof(EVENT_STRUCT.STRUCT_EVENT_OS_DEAL_CARD) }, // 系统发牌事件 // ========== 卡牌事件 ========== { EVENT_ENUM.EVENT_LET_CARD_DRAW_CARD, typeof(EVENT_STRUCT.STRUCT_EVENT_CARD_DRAW_CARD) } // 卡牌抽牌事件 }; private bool GET_DEFINE_EVENT(EVENT_ENUM eventName, Type t, string msg) { if (!actionBindMap.TryGetValue(eventName, out var value)) { Debug.LogError($"{msg}: 未绑定的数据类型[{eventName}]"); return false; } if (value == t) return true; Debug.LogError($"{msg}: 未响应的数据类型[{eventName}]"); return false; } // ================================ // 2. 注册回调,强类型检查 // ================================ public void EVENT_REGISTER(EVENT_ENUM eventName, Action callback) { if (!GET_DEFINE_EVENT(eventName, typeof(T), "注册事件失败")) return; actionBroadcast.TryAdd(eventName, null); actionBroadcast[eventName] = (Action)actionBroadcast[eventName] + callback; if (GetNeedDebugLog()) Debug.Log($"注册事件成功: [{eventName}]"); } // ================================ // 3. 注销回调 // ================================ public void EVENT_UNREGISTER(EVENT_ENUM eventName, Action callback) { if (!GET_DEFINE_EVENT(eventName, typeof(T), "注销事件失败")) return; actionBroadcast[eventName] = (Action)actionBroadcast[eventName] - callback; if (actionBroadcast[eventName] == null) actionBroadcast.Remove(eventName); if (GetNeedDebugLog()) Debug.Log($"注销事件成功: [{eventName}]"); } // ================================ // 4. 触发事件, 强类型检查 // ================================ public void EVENT_DISPATCH(EVENT_ENUM eventName, T param) { if (!GET_DEFINE_EVENT(eventName, typeof(T), "触发事件失败")) return; if (actionBroadcast.TryGetValue(eventName, out var action)) { (action as Action)?.Invoke(param); if (GetNeedDebugLog()) Debug.Log($"触发广播事件: [{eventName}]"); } else { Debug.LogWarning($"触发广播事件错误: 未找到事件[{eventName}]"); } } }