// #if !COMBAT_SERVER
using System;
using System.Collections.Generic;
using Utility;
namespace Fort23.Core
{
#if !COMBAT_SERVER
///
/// 事件调度器
///
public class EventManager : Singleton
{
public delegate void CustomEventHandler(IEventData e);
private readonly Dictionary _eventHandlers;
public EventManager()
{
_eventHandlers = new Dictionary();
}
///
/// 注册事件,没有处理重复,需要注意
///
///
///
public void AddEventListener(CustomEventType type, CustomEventHandler handle)
{
if (!_eventHandlers.ContainsKey(type))
{
_eventHandlers.Add(type, handle);
}
else
{
_eventHandlers[type] -= handle;
_eventHandlers[type] += handle;
}
}
///
/// 取消注册
///
///
///
public void RemoveEventListener(CustomEventType type, CustomEventHandler handle)
{
if (_eventHandlers.ContainsKey(type))
{
_eventHandlers[type] -= handle;
}
}
///
/// 取消注册所有当前类型的事件
///
///
public void RemoveAllEventListener(CustomEventType type)
{
if (_eventHandlers.ContainsKey(type))
{
_eventHandlers.Remove(type);
}
}
// ReSharper restore Unity.ExpensiveCode
///
/// 事件触发执行的方法,Public方便外界调用,谁执行,谁给执行细节
///
public void Dispatch(CustomEventType type, IEventData eventData, bool isRecycle = true)
{
if (_eventHandlers.ContainsKey(type))
{
if (_eventHandlers[type] != null)
{
_eventHandlers[type](eventData);
}
}
if (isRecycle && eventData != null)
{
eventData?.Recycle();
}
}
}
#endif
}
// #endif