2025-11-12 18:23:37 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
2025-11-13 01:18:02 +08:00
|
|
|
using Unity.VisualScripting;
|
2025-11-12 18:23:37 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class CardEventSvc
|
|
|
|
|
{
|
2025-11-12 18:35:43 +08:00
|
|
|
private readonly Dictionary<CardOSData.EVENT_REGISTER_CARD_ENUM, List<Action<object>>> eventActions = new();
|
2025-11-12 18:23:37 +08:00
|
|
|
|
2025-11-12 18:35:43 +08:00
|
|
|
private readonly Dictionary<CardOSData.EVENT_REGISTER_CARD_ENUM, Type> eventBindMap = new()
|
2025-11-12 18:23:37 +08:00
|
|
|
{
|
2025-11-13 01:18:02 +08:00
|
|
|
{ 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) },
|
2025-11-13 02:58:03 +08:00
|
|
|
{ CardOSData.EVENT_REGISTER_CARD_ENUM.EVENT_LET_ME_DROP_CARD, typeof(CardOSData.STRUCT_EVENT_DROP_CARD) }
|
2025-11-12 18:23:37 +08:00
|
|
|
};
|
|
|
|
|
|
2025-11-12 18:35:43 +08:00
|
|
|
public void EVENT_REGISTER<T>(CardOSData.EVENT_REGISTER_CARD_ENUM event_name, Action<T> callback) where T : struct
|
2025-11-12 18:23:37 +08:00
|
|
|
{
|
|
|
|
|
if (!eventBindMap.ContainsKey(event_name))
|
|
|
|
|
{
|
2025-11-13 02:58:03 +08:00
|
|
|
Debug.LogError($"申请注册事件错误: 未绑定的数据类型{event_name}");
|
2025-11-12 18:23:37 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (eventBindMap[event_name] != typeof(T))
|
|
|
|
|
{
|
2025-11-13 02:58:03 +08:00
|
|
|
Debug.LogError($"申请注册事件错误: 数据类型不匹配{event_name}");
|
2025-11-12 18:23:37 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!eventActions.ContainsKey(event_name))
|
|
|
|
|
eventActions[event_name] = new List<Action<object>>();
|
|
|
|
|
void wrapper(object obj) => callback((T)obj);
|
|
|
|
|
eventActions[event_name].Add(wrapper);
|
2025-11-13 02:58:03 +08:00
|
|
|
Debug.Log($"申请注册事件: {event_name}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void EVENT_UNREGISTER<T>(CardOSData.EVENT_REGISTER_CARD_ENUM event_name, Action<T> 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}");
|
2025-11-12 18:23:37 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-12 18:35:43 +08:00
|
|
|
public void EVENT_TRIGGER(CardOSData.EVENT_REGISTER_CARD_ENUM event_name, object data)
|
2025-11-12 18:23:37 +08:00
|
|
|
{
|
|
|
|
|
if (!eventBindMap.ContainsKey(event_name))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"触发事件错误: 未绑定的数据类型{event_name}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data == null || data.GetType() != eventBindMap[event_name])
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"触发事件错误: 数据类型不匹配{event_name}");
|
2025-11-13 01:18:02 +08:00
|
|
|
return;
|
2025-11-12 18:23:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!eventActions.ContainsKey(event_name))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"触发事件错误: 未找到事件");
|
2025-11-13 01:18:02 +08:00
|
|
|
return;
|
2025-11-12 18:23:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eventActions[event_name].ForEach(action => action.Invoke(data));
|
|
|
|
|
Debug.Log($"触发事件: {event_name}");
|
|
|
|
|
}
|
|
|
|
|
}
|