using System; using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; public class CardEventSvc { private readonly Dictionary>> eventActions = new(); private readonly Dictionary eventBindMap = new() { { CardOSData.EVENT_REGISTER_CARD_ENUM.EVENT_LET_ME_DRAW_CARD, typeof(CardOSData.STRUCT_EVENT_DRAW_CARD) }, { CardOSData.EVENT_REGISTER_CARD_ENUM.EVENT_LET_OS_DEAL_CARD_FINISH, typeof(int) }, }; public void EVENT_REGISTER(CardOSData.EVENT_REGISTER_CARD_ENUM event_name, Action callback) where T : struct { if (!eventBindMap.ContainsKey(event_name)) { Debug.LogError($"注册事件错误: 未绑定的数据类型{event_name}"); return; } if (eventBindMap[event_name] != typeof(T)) { Debug.LogError($"注册事件错误: 数据类型不匹配{event_name}"); return; } if (!eventActions.ContainsKey(event_name)) eventActions[event_name] = new List>(); void wrapper(object obj) => callback((T)obj); eventActions[event_name].Add(wrapper); Debug.Log($"注册事件: {event_name}"); } public void EVENT_TRIGGER(CardOSData.EVENT_REGISTER_CARD_ENUM event_name, object data) { if (!eventBindMap.ContainsKey(event_name)) { Debug.LogError($"触发事件错误: 未绑定的数据类型{event_name}"); return; } if (data == null || data.GetType() != eventBindMap[event_name]) { Debug.LogError($"触发事件错误: 数据类型不匹配{event_name}"); return; } if (!eventActions.ContainsKey(event_name)) { Debug.LogError($"触发事件错误: 未找到事件"); return; } eventActions[event_name].ForEach(action => action.Invoke(data)); Debug.Log($"触发事件: {event_name}"); } // public void EVENT_UNREGISTER(CardEventData.EVENT_REGISTER_CARD_ENUM event_name, Action callback) // { // if (eventActions.TryGetValue(event_name, out var callback_list)) // { // callback_list.Remove(callback); // if (callback_list.Count == 0) // eventActions.Remove(event_name); // Debug.Log($"取消注册事件: {event_name}"); // } // } // ============ 触发事件 ============ // public void EVENT_REGISTER(CardEventData.EVENT_REGISTER_CARD_ENUM event_name, Action callback) where T : struct // { // if (!eventMap.TryGetValue(typeof(T), out var eventQueue)) // { // Debug.LogError($"未找到与类型 {typeof(T).Name} 匹配的事件字典!"); // return; // } // // register_event(); // // void register_event() // { // if (!eventQueue.ContainsKey(event_name)) // eventQueue[event_name] = callback; // else // eventQueue[event_name] += callback; // print($"注册事件: {event_name}"); // } // } // // // ============= 取消注册 ============= // public void EVENT_UNREGISTER(EVENT_REGISTER_CARD_ENUM event_name, Action callback) // { // void unregister_event(Dictionary> eventQueue) // { // if (!eventQueue.ContainsKey(event_name)) return; // eventQueue[event_name] -= callback; // print($"取消注册事件: {event_name}"); // } // } // // // ============= 触发事件 ============= // public void EVENT_RAISE(EVENT_REGISTER_CARD_ENUM event_name, object args) // { // void raise_event(Dictionary> eventQueue) // { // if (eventQueue.ContainsKey(event_name)) // { // print($"触发事件: {event_name}"); // eventQueue[event_name]?.Invoke(args); // } // else // print($"未找到事件: {event_name}"); // } // } // // ************************************* }