1234567891011121314151617181920212223242526272829303132333435363738 |
- using System;
- using System.Collections.Generic;
- using ObjectEventAction = System.Action<CommonAI.Zone.ObjectEvent>;
- namespace ET
- {
- public class EventDispatcher
- {
- private Dictionary<Type, List<ObjectEventAction>> _list = new();
- public EventDispatcher() { }
- public ObjectEventAction AddListener<T>(ObjectEventAction action)
- {
- List<ObjectEventAction> acts;
- if (_list.TryGetValue(typeof(T), out acts))
- {
- acts.Add(action);
- }
- else
- {
- _list.Add(typeof(T), new List<ObjectEventAction> { action });
- }
- return action;
- }
- public void Notfify(CommonAI.Zone.ObjectEvent ev)
- {
- List<ObjectEventAction> acts;
- if (_list.TryGetValue(ev.GetType(), out acts))
- {
- foreach (var act in acts)
- {
- act.Invoke(ev);
- }
- }
- }
- }
- }
|