123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- 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.Recycle();
- }
- }
- }
- }
|