Game_CodeMM/Assets/00_scripts/Events/EventHandlerSvc.cs
2025-11-18 01:08:04 +08:00

86 lines
3.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class EventHandlerSvc
{
private readonly Dictionary<(EventData.EVENT_REGISTER_CARD_ENUM, Delegate), Action<object>> delegateMap = new(); // 委托地址, 用于取消注册
private readonly Dictionary<EventData.EVENT_REGISTER_CARD_ENUM, Action<object>> broadcastActions = new(); // 广播事件地址
// private readonly Dictionary<CardOSData.EVENT_REGISTER_CARD_ENUM, Dictionary<int, Action<object>>> EVENT_BROADCAST = new(); // 单播事件事件
private bool GetNeedDebugLog() => CombatScenarioEventOS.Instance.GetNeedDebugLog();
private readonly Dictionary<EventData.EVENT_REGISTER_CARD_ENUM, Type> actionBindMap = new()
{
{ EventData.EVENT_REGISTER_CARD_ENUM.EVENT_LET_CARD_DRAW_CARD, typeof(EventStruct.STRUCT_EVENT_DRAW_CARD) },
{ EventData.EVENT_REGISTER_CARD_ENUM.EVENT_LET_OS_SHUFFLE_CARD, typeof(EventStruct.STRUCT_EVENT_SHUFFLE_CARD) },
{ EventData.EVENT_REGISTER_CARD_ENUM.EVENT_LET_CARD_DROP_SELF, typeof(EventStruct.STRUCT_EVENT_DROP_CARD) },
{ EventData.EVENT_REGISTER_CARD_ENUM.EVENT_LET_OS_END_TURN, typeof(bool) }
};
private bool CHECK_BIND_DATACLASS(EventData.EVENT_REGISTER_CARD_ENUM eventName, Type t, string msgName)
{
if (!actionBindMap.ContainsKey(eventName))
{
if (GetNeedDebugLog()) Debug.LogError($"{msgName}: 未绑定数据类型[{eventName}]");
return false;
}
if (actionBindMap[eventName] != t)
{
if (GetNeedDebugLog()) Debug.LogError($"{msgName}: 数据类型不匹配[{eventName}]=>期望数据类型[{actionBindMap[eventName]}]");
return false;
}
return true;
}
public void EVENT_REGISTER<T>(EventData.EVENT_REGISTER_CARD_ENUM eventName, Action<T> callback) where T : struct
{
if (!CHECK_BIND_DATACLASS(eventName, typeof(T), "注册广播事件失败")) return;
broadcastActions.TryAdd(eventName, null);
Action<object> wrapper = (obj) => callback((T)obj);
delegateMap[(eventName, callback)] = wrapper; // 保存委托映射
broadcastActions[eventName] += wrapper;
if (GetNeedDebugLog()) Debug.Log($"注册广播事件: [{eventName}]");
}
public void EVENT_UNREGISTER<T>(EventData.EVENT_REGISTER_CARD_ENUM eventName, Action<T> callback) where T : struct
{
if (!CHECK_BIND_DATACLASS(eventName, typeof(T), "注销广播事件失败")) return;
if (!broadcastActions.ContainsKey(eventName))
{
if (GetNeedDebugLog()) Debug.LogWarning($"注销广播事件失败: 未创建的广播事件[{eventName}]");
return;
}
if (delegateMap.TryGetValue((eventName, callback), out var wrapper))
{
broadcastActions[eventName] -= wrapper;
delegateMap.Remove((eventName, callback));
if (GetNeedDebugLog()) Debug.Log($"注销广播事件: [{eventName}]");
}
else
{
if (GetNeedDebugLog()) Debug.LogWarning($"注销广播事件失败: 未找到匹配的委托映射用于注销[{eventName}]");
}
}
public void EVENT_TRIGGER(EventData.EVENT_REGISTER_CARD_ENUM evetName, object data)
{
if (!CHECK_BIND_DATACLASS(evetName, data.GetType(), "触发消息失败")) return;
if (broadcastActions.TryGetValue(evetName, out var action))
{
if (GetNeedDebugLog()) Debug.Log($"触发广播事件: [{evetName}]");
action?.Invoke(data);
}
else
{
if (GetNeedDebugLog()) Debug.LogWarning($"触发广播事件错误: 未找到事件[{evetName}]");
}
}
}