1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 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<CombatEventManager>
- {
- public delegate void CombatEventHandler(IEventData e);
- private readonly Dictionary<CombatEventType, CombatEventHandler> _eventHandlers =
- new Dictionary<CombatEventType, CombatEventHandler>();
- public CombatEventManager()
- {
- _eventHandlers = new Dictionary<CombatEventType, CombatEventHandler>();
- }
- protected override void ProDispose()
- {
- _eventHandlers.Clear();
- }
- /// <summary>
- /// 注册事件,没有处理重复,需要注意
- /// </summary>
- /// <param name="type"></param>
- /// <param name="handle"></param>
- public void AddEventListener(CombatEventType type, CombatEventHandler handle)
- {
- if (!_eventHandlers.ContainsKey(type))
- {
- _eventHandlers.Add(type, handle);
- }
- else
- {
- _eventHandlers[type] -= handle;
- _eventHandlers[type] += handle;
- }
- }
- /// <summary>
- /// 取消注册
- /// </summary>
- /// <param name="type"></param>
- /// <param name="handle"></param>
- public void RemoveEventListener(CombatEventType type, CombatEventHandler handle)
- {
- if (_eventHandlers.ContainsKey(type))
- {
- _eventHandlers[type] -= handle;
- }
- }
- /// <summary>
- /// 事件触发执行的方法,Public方便外界调用,谁执行,谁给执行细节
- /// </summary>
- 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();
- }
- }
- }
- }
|