using System; using System.Collections.Generic; namespace TEngineCore { public class ECSEventCmpt : ECSComponent { private Dictionary m_eventDic = new Dictionary(); #region AddEventListener public void AddEventListener(int eventid, Action action) { if (m_eventDic.ContainsKey(eventid)) { (m_eventDic[eventid] as EcsEventInfo).actions += action; } else { m_eventDic.Add(eventid, new EcsEventInfo(action)); } } public void AddEventListener(int eventid, Action action) { if (m_eventDic.ContainsKey(eventid)) { (m_eventDic[eventid] as EcsEventInfo).actions += action; } else { m_eventDic.Add(eventid, new EcsEventInfo(action)); } } public void AddEventListener(int eventid, Action action) { if (m_eventDic.ContainsKey(eventid)) { (m_eventDic[eventid] as EcsEventInfo).actions += action; } else { m_eventDic.Add(eventid, new EcsEventInfo(action)); } } public void AddEventListener(int eventid, Action action) { if (m_eventDic.ContainsKey(eventid)) { (m_eventDic[eventid] as EcsEventInfo).actions += action; } else { m_eventDic.Add(eventid, new EcsEventInfo(action)); } } #endregion #region RemoveEventListener public void RemoveEventListener(int eventid, Action action) { if (action == null) { return; } if (m_eventDic.ContainsKey(eventid)) { (m_eventDic[eventid] as EcsEventInfo).actions -= action; } } public void RemoveEventListener(int eventid, Action action) { if (action == null) { return; } if (m_eventDic.ContainsKey(eventid)) { (m_eventDic[eventid] as EcsEventInfo).actions -= action; } } public void RemoveEventListener(int eventid, Action action) { if (action == null) { return; } if (m_eventDic.ContainsKey(eventid)) { (m_eventDic[eventid] as EcsEventInfo).actions -= action; } } public void RemoveEventListener(int eventid, Action action) { if (action == null) { return; } if (m_eventDic.ContainsKey(eventid)) { (m_eventDic[eventid] as EcsEventInfo).actions -= action; } } #endregion #region Send public void Send(int eventid, T info) { if (m_eventDic.ContainsKey(eventid)) { var EcsEventInfo = (m_eventDic[eventid] as EcsEventInfo); if (EcsEventInfo != null) { EcsEventInfo.actions.Invoke(info); } } } public void Send(int eventid, T info, U info2) { if (m_eventDic.ContainsKey(eventid)) { var EcsEventInfo = (m_eventDic[eventid] as EcsEventInfo); if (EcsEventInfo != null) { EcsEventInfo.actions.Invoke(info, info2); } } } public void Send(int eventid, T info, U info2, W info3) { if (m_eventDic.ContainsKey(eventid)) { var EcsEventInfo = (m_eventDic[eventid] as EcsEventInfo); if (EcsEventInfo != null) { EcsEventInfo.actions.Invoke(info, info2, info3); } } } /// /// 事件触发 无参 /// /// public void Send(int eventid) { if (m_eventDic.ContainsKey(eventid)) { var EcsEventInfo = (m_eventDic[eventid] as EcsEventInfo); if (EcsEventInfo != null) { EcsEventInfo.actions.Invoke(); } } } #endregion #region Clear public void Clear() { m_eventDic.Clear(); } #endregion #region 生命周期 public override void OnDestroy() { Clear(); } public override void Awake() { Entity.Event = this; } #endregion } #region EcsEventInfo internal interface IEcsEcsEventInfo { } public class EcsEventInfo : IEcsEcsEventInfo { public Action actions; public EcsEventInfo(Action action) { actions += action; } } public class EcsEventInfo : IEcsEcsEventInfo { public Action actions; public EcsEventInfo(Action action) { actions += action; } } public class EcsEventInfo : IEcsEcsEventInfo { public Action actions; public EcsEventInfo(Action action) { actions += action; } } public class EcsEventInfo : IEcsEcsEventInfo { public Action actions; public EcsEventInfo(Action action) { actions += action; } } #endregion }