|
@@ -0,0 +1,52 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using ObjectEventAction = System.Action<CommonAI.Zone.ObjectEvent>;
|
|
|
+
|
|
|
+namespace ET
|
|
|
+{
|
|
|
+
|
|
|
+ [FriendOf(typeof(EventDispatcherComponent))]
|
|
|
+ public static class EventDispatcherComponentSystem
|
|
|
+ {
|
|
|
+ [ObjectSystem]
|
|
|
+ public class EventDispatcherComponentAwakeSystem : AwakeSystem<EventDispatcherComponent>
|
|
|
+ {
|
|
|
+ protected override void Awake(EventDispatcherComponent self)
|
|
|
+ {
|
|
|
+ self.list.Clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ObjectEventAction AddListener<T>(this EventDispatcherComponent self, ObjectEventAction action)
|
|
|
+ {
|
|
|
+ List<ObjectEventAction> acts;
|
|
|
+ if(self.list.TryGetValue(typeof(T), out acts))
|
|
|
+ {
|
|
|
+ acts.Add(action);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ self.list.Add(typeof(T), new List<ObjectEventAction> { action });
|
|
|
+ }
|
|
|
+ return action;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Notfify(this EventDispatcherComponent self, CommonAI.Zone.ObjectEvent ev)
|
|
|
+ {
|
|
|
+ List<ObjectEventAction> acts;
|
|
|
+ if (self.list.TryGetValue(ev.GetType(), out acts))
|
|
|
+ {
|
|
|
+ foreach(var act in acts)
|
|
|
+ {
|
|
|
+ act.Invoke(ev);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ [ComponentOf]
|
|
|
+ public class EventDispatcherComponent : Entity, IAwake
|
|
|
+ {
|
|
|
+ public Dictionary<Type, List<ObjectEventAction>> list = new();
|
|
|
+ }
|
|
|
+}
|