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) }, { CardOSData.EVENT_REGISTER_CARD_ENUM.EVENT_LET_ME_DROP_CARD, typeof(CardOSData.STRUCT_EVENT_DROP_CARD) } }; 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_UNREGISTER(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)) { Debug.LogError($"取消注册事件错误: 未找到事件"); return; } void wrapper(object obj) => callback((T)obj); eventActions[event_name].Remove(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}"); } }