Преглед изворни кода

增加一个通用的消息分发器组件(当消息类型很多时,避免大量的if-else,提高消息分发效率)

大爷 пре 2 година
родитељ
комит
88e2917d2d

+ 38 - 0
Unity/Assets/Scripts/Codes/Model/Share/EventDispatcher.cs

@@ -0,0 +1,38 @@
+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);
+                }
+            }
+        }
+    }
+}

+ 11 - 0
Unity/Assets/Scripts/Codes/Model/Share/EventDispatcher.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 129c6b6e76e61c0498bdfba74242cd7a
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 52 - 0
Unity/Assets/Scripts/Codes/Model/Share/EventDispatcherComponent.cs

@@ -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();
+    }
+}

+ 11 - 0
Unity/Assets/Scripts/Codes/Model/Share/EventDispatcherComponent.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 3194cd0e383dc3a44a454ed432cb8c6e
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: