using System; using System.Collections.Generic; using Fort23.Core; using Fort23.UTool; #if !COMBAT_SERVER using UnityEngine; #endif using Utility; namespace Common.Utility.CombatEvent { public class CombatEventManager : Singleton { public delegate void CombatEventHandler(IEventData e); private readonly Dictionary _eventHandlers = new Dictionary(); public CombatEventManager() { _eventHandlers = new Dictionary(); } protected override void ProDispose() { _eventHandlers.Clear(); } /// /// 注册事件,没有处理重复,需要注意 /// /// /// public void AddEventListener(CombatEventType type, CombatEventHandler handle) { if (!_eventHandlers.ContainsKey(type)) { _eventHandlers.Add(type, handle); } else { _eventHandlers[type] -= handle; _eventHandlers[type] += handle; } } /// /// 取消注册 /// /// /// public void RemoveEventListener(CombatEventType type, CombatEventHandler handle) { if (_eventHandlers.ContainsKey(type)) { _eventHandlers[type] -= handle; } } /// /// 事件触发执行的方法,Public方便外界调用,谁执行,谁给执行细节 /// public void Dispatch(CombatEventType type, IEventData eventData, bool isRecycle = true) { if (_eventHandlers.ContainsKey(type)) { if (_eventHandlers[type] != null) { Delegate[] allDelegate = _eventHandlers[type].GetInvocationList(); for (int i = 0; i < allDelegate.Length; i++) { try { allDelegate[i]?.DynamicInvoke(eventData); } catch (Exception e) { LogTool.Error(e); } } } } if (isRecycle && eventData != null) { eventData.Recycle(); } } } }