EventDispatcher.cs 1019 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.Collections.Generic;
  3. using ObjectEventAction = System.Action<CommonAI.Zone.ObjectEvent>;
  4. namespace ET
  5. {
  6. public class EventDispatcher
  7. {
  8. private Dictionary<Type, List<ObjectEventAction>> _list = new();
  9. public EventDispatcher() { }
  10. public ObjectEventAction AddListener<T>(ObjectEventAction action)
  11. {
  12. List<ObjectEventAction> acts;
  13. if (_list.TryGetValue(typeof(T), out acts))
  14. {
  15. acts.Add(action);
  16. }
  17. else
  18. {
  19. _list.Add(typeof(T), new List<ObjectEventAction> { action });
  20. }
  21. return action;
  22. }
  23. public void Notfify(CommonAI.Zone.ObjectEvent ev)
  24. {
  25. List<ObjectEventAction> acts;
  26. if (_list.TryGetValue(ev.GetType(), out acts))
  27. {
  28. foreach (var act in acts)
  29. {
  30. act.Invoke(ev);
  31. }
  32. }
  33. }
  34. }
  35. }