Game_CodeMM/Assets/00_scripts/Events/EventHandlerSvc.cs

85 lines
3.7 KiB
C#
Raw Normal View History

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;
2025-11-18 01:08:04 +08:00
public class EventHandlerSvc
2025-11-12 18:23:37 +08:00
{
2025-11-18 02:11:55 +08:00
private readonly Dictionary<(EventData.EVENT_REGISTER_EVENT_ENUM, Delegate), Action<object>> delegateMap = new(); // 委托地址, 用于取消注册
private readonly Dictionary<EventData.EVENT_REGISTER_EVENT_ENUM, Action<object>> broadcastActions = new(); // 广播事件地址
2025-11-14 14:53:29 +08:00
2025-11-13 23:17:53 +08:00
// private readonly Dictionary<CardOSData.EVENT_REGISTER_CARD_ENUM, Dictionary<int, Action<object>>> EVENT_BROADCAST = new(); // 单播事件事件
2025-11-12 18:23:37 +08:00
2025-11-18 01:08:04 +08:00
private bool GetNeedDebugLog() => CombatScenarioEventOS.Instance.GetNeedDebugLog();
2025-11-13 23:17:53 +08:00
2025-11-18 02:11:55 +08:00
private readonly Dictionary<EventData.EVENT_REGISTER_EVENT_ENUM, Type> actionBindMap = new()
2025-11-12 18:23:37 +08:00
{
2025-11-18 02:11:55 +08:00
{ EventData.EVENT_REGISTER_EVENT_ENUM.EVENT_LET_CARD_DRAW_CARD, typeof(EventStruct.STRUCT_EVENT_DRAW_CARD) },
{ EventData.EVENT_REGISTER_EVENT_ENUM.EVENT_LET_OS_DEAL_CARD, typeof(EventStruct.STRUCT_EVENT_DEAL_CARD) },
{ EventData.EVENT_REGISTER_EVENT_ENUM.EVENT_LET_CARD_DROP_SELF, typeof(EventStruct.STRUCT_EVENT_DROP_CARD) },
{ EventData.EVENT_REGISTER_EVENT_ENUM.EVENT_LET_OS_END_TURN, typeof(bool) }
2025-11-12 18:23:37 +08:00
};
2025-11-18 02:11:55 +08:00
private bool CHECK_BIND_DATACLASS(EventData.EVENT_REGISTER_EVENT_ENUM eventName, Type t, string msgName)
2025-11-12 18:23:37 +08:00
{
2025-11-13 23:17:53 +08:00
if (!actionBindMap.ContainsKey(eventName))
2025-11-12 18:23:37 +08:00
{
2025-11-14 14:53:29 +08:00
if (GetNeedDebugLog()) Debug.LogError($"{msgName}: 未绑定数据类型[{eventName}]");
2025-11-13 23:17:53 +08:00
return false;
2025-11-12 18:23:37 +08:00
}
2025-11-13 23:17:53 +08:00
if (actionBindMap[eventName] != t)
2025-11-12 18:23:37 +08:00
{
2025-11-14 14:53:29 +08:00
if (GetNeedDebugLog()) Debug.LogError($"{msgName}: 数据类型不匹配[{eventName}]=>期望数据类型[{actionBindMap[eventName]}]");
2025-11-13 23:17:53 +08:00
return false;
2025-11-12 18:23:37 +08:00
}
2025-11-13 23:17:53 +08:00
return true;
2025-11-13 02:58:03 +08:00
}
2025-11-18 02:11:55 +08:00
public void EVENT_REGISTER<T>(EventData.EVENT_REGISTER_EVENT_ENUM eventName, Action<T> callback) where T : struct
2025-11-13 02:58:03 +08:00
{
2025-11-13 23:17:53 +08:00
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;
2025-11-14 14:53:29 +08:00
if (GetNeedDebugLog()) Debug.Log($"注册广播事件: [{eventName}]");
2025-11-13 23:17:53 +08:00
}
2025-11-18 02:11:55 +08:00
public void EVENT_UNREGISTER<T>(EventData.EVENT_REGISTER_EVENT_ENUM eventName, Action<T> callback) where T : struct
2025-11-13 23:17:53 +08:00
{
if (!CHECK_BIND_DATACLASS(eventName, typeof(T), "注销广播事件失败")) return;
if (!broadcastActions.ContainsKey(eventName))
2025-11-13 02:58:03 +08:00
{
2025-11-14 14:53:29 +08:00
if (GetNeedDebugLog()) Debug.LogWarning($"注销广播事件失败: 未创建的广播事件[{eventName}]");
2025-11-13 02:58:03 +08:00
return;
}
2025-11-13 23:17:53 +08:00
if (delegateMap.TryGetValue((eventName, callback), out var wrapper))
2025-11-13 02:58:03 +08:00
{
2025-11-13 23:17:53 +08:00
broadcastActions[eventName] -= wrapper;
delegateMap.Remove((eventName, callback));
2025-11-14 14:53:29 +08:00
if (GetNeedDebugLog()) Debug.Log($"注销广播事件: [{eventName}]");
2025-11-13 02:58:03 +08:00
}
2025-11-13 23:17:53 +08:00
else
2025-11-13 02:58:03 +08:00
{
2025-11-14 14:53:29 +08:00
if (GetNeedDebugLog()) Debug.LogWarning($"注销广播事件失败: 未找到匹配的委托映射用于注销[{eventName}]");
2025-11-13 02:58:03 +08:00
}
2025-11-12 18:23:37 +08:00
}
2025-11-18 02:11:55 +08:00
public void EVENT_TRIGGER(EventData.EVENT_REGISTER_EVENT_ENUM evetName, object data)
2025-11-12 18:23:37 +08:00
{
2025-11-13 23:17:53 +08:00
if (!CHECK_BIND_DATACLASS(evetName, data.GetType(), "触发消息失败")) return;
if (broadcastActions.TryGetValue(evetName, out var action))
2025-11-12 18:23:37 +08:00
{
2025-11-14 14:53:29 +08:00
if (GetNeedDebugLog()) Debug.Log($"触发广播事件: [{evetName}]");
2025-11-13 23:17:53 +08:00
action?.Invoke(data);
2025-11-12 18:23:37 +08:00
}
2025-11-13 23:17:53 +08:00
else
2025-11-12 18:23:37 +08:00
{
2025-11-14 14:53:29 +08:00
if (GetNeedDebugLog()) Debug.LogWarning($"触发广播事件错误: 未找到事件[{evetName}]");
2025-11-12 18:23:37 +08:00
}
}
}