Просмотр исходного кода

事件系统第一个参数改成Scene,之前的方案第一个参数没有强约束,订阅方会出现跟抛出参数不一致的情况

tanghai 3 лет назад
Родитель
Сommit
1a596f0c79
28 измененных файлов с 91 добавлено и 68 удалено
  1. 1 1
      Codes/Hotfix/Client/Demo/Scene/SceneChangeHelper.cs
  2. 1 1
      Codes/Hotfix/Client/Demo/Unit/UnitFactory.cs
  3. 3 3
      Codes/Hotfix/Server/Demo/Map/AOI/ChangePosition_NotifyAOI.cs
  4. 5 3
      Codes/Hotfix/Server/Demo/Map/Unit/UnitEnterSightRange_NotifyClient.cs
  5. 3 2
      Codes/Hotfix/Server/Demo/Map/Unit/UnitLeaveSightRange_NotifyClient.cs
  6. 2 2
      Codes/Hotfix/Server/Module/AOI/AOIEntitySystem.cs
  7. 2 2
      Codes/Hotfix/Share/Module/Move/MoveComponentSystem.cs
  8. 4 5
      Codes/Hotfix/Share/Module/Numeric/NumericChangeEvent_NotifyWatcher.cs
  9. 1 1
      Codes/HotfixView/Client/Demo/Scene/AfterCreateClientScene_AddComponent.cs
  10. 1 1
      Codes/HotfixView/Client/Demo/Scene/AfterCreateCurrentScene_AddComponent.cs
  11. 1 1
      Codes/HotfixView/Client/Demo/Scene/SceneChangeStart_AddComponent.cs
  12. 1 1
      Codes/HotfixView/Client/Demo/UI/UIHelp/SceneChangeFinishEvent_CreateUIHelp.cs
  13. 1 1
      Codes/HotfixView/Client/Demo/UI/UILobby/LoginFinish_CreateLobbyUI.cs
  14. 1 1
      Codes/HotfixView/Client/Demo/UI/UILogin/AppStartInitFinish_CreateLoginUI.cs
  15. 1 1
      Codes/HotfixView/Client/Demo/UI/UILogin/LoginFinish_RemoveLoginUI.cs
  16. 3 2
      Codes/HotfixView/Client/Demo/Unit/AfterUnitCreate_CreateUnitView.cs
  17. 3 2
      Codes/HotfixView/Client/Demo/Unit/ChangePosition_SyncGameObjectPos.cs
  18. 3 2
      Codes/HotfixView/Client/Demo/Unit/ChangeRotation_SyncGameObjectRotation.cs
  19. 2 0
      Codes/Model/Server/Module/AOI/AOIEventType.cs
  20. 1 0
      Codes/Model/Share/EventType.cs
  21. 10 0
      Codes/Model/Share/Module/Config/ConfigComponent.cs
  22. 2 0
      Codes/Model/Share/Module/Move/MoveEventType.cs
  23. 3 2
      Codes/Model/Share/Module/Numeric/NumericComponent.cs
  24. 2 2
      Codes/Model/Share/Module/Unit/Unit.cs
  25. 2 0
      Codes/Model/Share/Module/Unit/UnitEventType.cs
  26. 1 0
      Unity/Assets/Scripts/Core/Entity/SceneType.cs
  27. 4 4
      Unity/Assets/Scripts/Core/Event/IEvent.cs
  28. 27 28
      Unity/Assets/Scripts/Core/Object/EventSystem.cs

+ 1 - 1
Codes/Hotfix/Client/Demo/Scene/SceneChangeHelper.cs

@@ -18,7 +18,7 @@
             // 等待CreateMyUnit的消息
             WaitType.Wait_CreateMyUnit waitCreateMyUnit = await clientScene.GetComponent<ObjectWait>().Wait<WaitType.Wait_CreateMyUnit>();
             M2C_CreateMyUnit m2CCreateMyUnit = waitCreateMyUnit.Message;
-            Unit unit = Client.UnitFactory.Create(currentScene, m2CCreateMyUnit.Unit);
+            Unit unit = UnitFactory.Create(currentScene, m2CCreateMyUnit.Unit);
             unitComponent.Add(unit);
             
             clientScene.RemoveComponent<AIComponent>();

+ 1 - 1
Codes/Hotfix/Client/Demo/Unit/UnitFactory.cs

@@ -41,7 +41,7 @@ namespace ET.Client
 
 	        unit.AddComponent<XunLuoPathComponent>();
 	        
-	        Game.EventSystem.Publish(unit, new EventType.AfterUnitCreate());
+	        Game.EventSystem.Publish(unit.DomainScene(), new EventType.AfterUnitCreate() {Unit = unit});
             return unit;
         }
     }

+ 3 - 3
Codes/Hotfix/Server/Demo/Map/AOI/ChangePosition_NotifyAOI.cs

@@ -3,12 +3,12 @@
 namespace ET.Server
 {
     [Event(SceneType.Map)]
-    public class ChangePosition_NotifyAOI: AEvent<Unit, ET.EventType.ChangePosition>
+    public class ChangePosition_NotifyAOI: AEvent<ET.EventType.ChangePosition>
     {
-        protected override async ETTask Run(Unit unit, ET.EventType.ChangePosition args)
+        protected override async ETTask Run(Scene scene, ET.EventType.ChangePosition args)
         {
+            Unit unit = args.Unit;
             Vector3 oldPos = args.OldPos;
-            
             int oldCellX = (int) (oldPos.x * 1000) / AOIManagerComponent.CellSize;
             int oldCellY = (int) (oldPos.z * 1000) / AOIManagerComponent.CellSize;
             int newCellX = (int) (unit.Position.x * 1000) / AOIManagerComponent.CellSize;

+ 5 - 3
Codes/Hotfix/Server/Demo/Map/Unit/UnitEnterSightRange_NotifyClient.cs

@@ -2,11 +2,11 @@
 {
     // 进入视野通知
     [Event(SceneType.Map)]
-    public class UnitEnterSightRange_NotifyClient: AEvent<AOIEntity, EventType.UnitEnterSightRange>
+    public class UnitEnterSightRange_NotifyClient: AEvent<EventType.UnitEnterSightRange>
     {
-        protected override async ETTask Run(AOIEntity a, EventType.UnitEnterSightRange args)
+        protected override async ETTask Run(Scene scene, EventType.UnitEnterSightRange args)
         {
-            await ETTask.CompletedTask;
+            AOIEntity a = args.A;
             AOIEntity b = args.B;
             if (a.Id == b.Id)
             {
@@ -22,6 +22,8 @@
             Unit ub = b.GetParent<Unit>();
 
             MessageHelper.NoticeUnitAdd(ua, ub);
+            
+            await ETTask.CompletedTask;
         }
     }
 }

+ 3 - 2
Codes/Hotfix/Server/Demo/Map/Unit/UnitLeaveSightRange_NotifyClient.cs

@@ -2,11 +2,12 @@
 {
     // 离开视野
     [Event(SceneType.Map)]
-    public class UnitLeaveSightRange_NotifyClient: AEvent<AOIEntity, EventType.UnitLeaveSightRange>
+    public class UnitLeaveSightRange_NotifyClient: AEvent<EventType.UnitLeaveSightRange>
     {
-        protected override async ETTask Run(AOIEntity a, EventType.UnitLeaveSightRange args)
+        protected override async ETTask Run(Scene scene, EventType.UnitLeaveSightRange args)
         {
             await ETTask.CompletedTask;
+            AOIEntity a = args.A;
             AOIEntity b = args.B;
             if (a.Unit.Type != UnitType.Player)
             {

+ 2 - 2
Codes/Hotfix/Server/Module/AOI/AOIEntitySystem.cs

@@ -136,7 +136,7 @@ namespace ET.Server
                     enter.BeSeeUnits.Add(self.Id, self);
                 }
             }
-            Game.EventSystem.Publish(self, new EventType.UnitEnterSightRange() { B = enter });
+            Game.EventSystem.Publish(self.DomainScene(), new EventType.UnitEnterSightRange() { A = self, B = enter });
         }
 
         // leave离开self视野
@@ -164,7 +164,7 @@ namespace ET.Server
                 leave.BeSeePlayers.Remove(self.Id);
             }
 
-            Game.EventSystem.Publish(self, new EventType.UnitLeaveSightRange { B = leave });
+            Game.EventSystem.Publish(self.DomainScene(), new EventType.UnitLeaveSightRange { A = self, B = leave });
         }
 
         /// <summary>

+ 2 - 2
Codes/Hotfix/Share/Module/Move/MoveComponentSystem.cs

@@ -95,7 +95,7 @@ namespace ET
             self.Speed = speed;
             self.tcs = ETTask<bool>.Create(true);
 
-            Game.EventSystem.Publish(self.GetParent<Unit>(), new EventType.MoveStart());
+            Game.EventSystem.Publish(self.DomainScene(), new EventType.MoveStart() {Unit = self.GetParent<Unit>()});
             
             self.StartMove();
             
@@ -117,7 +117,7 @@ namespace ET
 
             if (moveRet)
             {
-                Game.EventSystem.Publish(self.GetParent<Unit>(), new EventType.MoveStop());
+                Game.EventSystem.Publish(self.DomainScene(), new EventType.MoveStop() {Unit = self.GetParent<Unit>()});
             }
             return moveRet;
         }

+ 4 - 5
Codes/Hotfix/Share/Module/Numeric/NumericChangeEvent_NotifyWatcher.cs

@@ -1,13 +1,12 @@
 namespace ET
 {
 	// 分发数值监听
-	[Event(SceneType.Map)]  // 服务端Map需要分发
-	[Event(SceneType.Current)] // 客户端CurrentScene也要分发
-	public class NumericChangeEvent_NotifyWatcher: AEvent<Unit, EventType.NumbericChange>
+	[Event(SceneType.None)]  // 服务端Map需要分发, 客户端CurrentScene也要分发
+	public class NumericChangeEvent_NotifyWatcher: AEvent<EventType.NumbericChange>
 	{
-		protected override async ETTask Run(Unit unit, EventType.NumbericChange args)
+		protected override async ETTask Run(Scene scene, EventType.NumbericChange args)
 		{
-			NumericWatcherComponent.Instance.Run(unit, args);
+			NumericWatcherComponent.Instance.Run(args.Unit, args);
 			await ETTask.CompletedTask;
 		}
 	}

+ 1 - 1
Codes/HotfixView/Client/Demo/Scene/AfterCreateClientScene_AddComponent.cs

@@ -1,7 +1,7 @@
 namespace ET.Client
 {
     [Event(SceneType.Client)]
-    public class AfterCreateClientScene_AddComponent: AEvent<Scene, EventType.AfterCreateClientScene>
+    public class AfterCreateClientScene_AddComponent: AEvent<EventType.AfterCreateClientScene>
     {
         protected override async ETTask Run(Scene scene, EventType.AfterCreateClientScene args)
         {

+ 1 - 1
Codes/HotfixView/Client/Demo/Scene/AfterCreateCurrentScene_AddComponent.cs

@@ -1,7 +1,7 @@
 namespace ET.Client
 {
     [Event(SceneType.Current)]
-    public class AfterCreateCurrentScene_AddComponent: AEvent<Scene, EventType.AfterCreateCurrentScene>
+    public class AfterCreateCurrentScene_AddComponent: AEvent<EventType.AfterCreateCurrentScene>
     {
         protected override async ETTask Run(Scene scene, EventType.AfterCreateCurrentScene args)
         {

+ 1 - 1
Codes/HotfixView/Client/Demo/Scene/SceneChangeStart_AddComponent.cs

@@ -3,7 +3,7 @@ using UnityEngine.SceneManagement;
 namespace ET.Client
 {
     [Event(SceneType.Client)]
-    public class SceneChangeStart_AddComponent: AEvent<Scene, EventType.SceneChangeStart>
+    public class SceneChangeStart_AddComponent: AEvent<EventType.SceneChangeStart>
     {
         protected override async ETTask Run(Scene scene, EventType.SceneChangeStart args)
         {

+ 1 - 1
Codes/HotfixView/Client/Demo/UI/UIHelp/SceneChangeFinishEvent_CreateUIHelp.cs

@@ -1,7 +1,7 @@
 namespace ET.Client
 {
     [Event(SceneType.Current)]
-    public class SceneChangeFinishEvent_CreateUIHelp : AEvent<Scene, EventType.SceneChangeFinish>
+    public class SceneChangeFinishEvent_CreateUIHelp : AEvent<EventType.SceneChangeFinish>
     {
         protected override async ETTask Run(Scene scene, EventType.SceneChangeFinish args)
         {

+ 1 - 1
Codes/HotfixView/Client/Demo/UI/UILobby/LoginFinish_CreateLobbyUI.cs

@@ -1,7 +1,7 @@
 namespace ET.Client
 {
 	[Event(SceneType.Client)]
-	public class LoginFinish_CreateLobbyUI: AEvent<Scene, EventType.LoginFinish>
+	public class LoginFinish_CreateLobbyUI: AEvent<EventType.LoginFinish>
 	{
 		protected override async ETTask Run(Scene scene, EventType.LoginFinish args)
 		{

+ 1 - 1
Codes/HotfixView/Client/Demo/UI/UILogin/AppStartInitFinish_CreateLoginUI.cs

@@ -1,7 +1,7 @@
 namespace ET.Client
 {
 	[Event(SceneType.Client)]
-	public class AppStartInitFinish_CreateLoginUI: AEvent<Scene, EventType.AppStartInitFinish>
+	public class AppStartInitFinish_CreateLoginUI: AEvent<EventType.AppStartInitFinish>
 	{
 		protected override async ETTask Run(Scene scene, EventType.AppStartInitFinish args)
 		{

+ 1 - 1
Codes/HotfixView/Client/Demo/UI/UILogin/LoginFinish_RemoveLoginUI.cs

@@ -1,7 +1,7 @@
 namespace ET.Client
 {
 	[Event(SceneType.Client)]
-	public class LoginFinish_RemoveLoginUI: AEvent<Scene, EventType.LoginFinish>
+	public class LoginFinish_RemoveLoginUI: AEvent<EventType.LoginFinish>
 	{
 		protected override async ETTask Run(Scene scene, EventType.LoginFinish args)
 		{

+ 3 - 2
Codes/HotfixView/Client/Demo/Unit/AfterUnitCreate_CreateUnitView.cs

@@ -3,10 +3,11 @@
 namespace ET.Client
 {
     [Event(SceneType.Current)]
-    public class AfterUnitCreate_CreateUnitView: AEvent<Unit, EventType.AfterUnitCreate>
+    public class AfterUnitCreate_CreateUnitView: AEvent<EventType.AfterUnitCreate>
     {
-        protected override async ETTask Run(Unit unit, EventType.AfterUnitCreate args)
+        protected override async ETTask Run(Scene scene, EventType.AfterUnitCreate args)
         {
+            Unit unit = args.Unit;
             // Unit View层
             // 这里可以改成异步加载,demo就不搞了
             GameObject bundleGameObject = (GameObject)ResourcesComponent.Instance.GetAsset("Unit.unity3d", "Unit");

+ 3 - 2
Codes/HotfixView/Client/Demo/Unit/ChangePosition_SyncGameObjectPos.cs

@@ -3,10 +3,11 @@
 namespace ET.Client
 {
     [Event(SceneType.Current)]
-    public class ChangePosition_SyncGameObjectPos: AEvent<Unit, EventType.ChangePosition>
+    public class ChangePosition_SyncGameObjectPos: AEvent<EventType.ChangePosition>
     {
-        protected override async ETTask Run(Unit unit, EventType.ChangePosition args)
+        protected override async ETTask Run(Scene scene, EventType.ChangePosition args)
         {
+            Unit unit = args.Unit;
             GameObjectComponent gameObjectComponent = unit.GetComponent<GameObjectComponent>();
             if (gameObjectComponent == null)
             {

+ 3 - 2
Codes/HotfixView/Client/Demo/Unit/ChangeRotation_SyncGameObjectRotation.cs

@@ -3,10 +3,11 @@ using UnityEngine;
 namespace ET.Client
 {
     [Event(SceneType.Current)]
-    public class ChangeRotation_SyncGameObjectRotation: AEvent<Unit, EventType.ChangeRotation>
+    public class ChangeRotation_SyncGameObjectRotation: AEvent<EventType.ChangeRotation>
     {
-        protected override async ETTask Run(Unit unit, EventType.ChangeRotation args)
+        protected override async ETTask Run(Scene scene, EventType.ChangeRotation args)
         {
+            Unit unit = args.Unit;
             GameObjectComponent gameObjectComponent = unit.GetComponent<GameObjectComponent>();
             if (gameObjectComponent == null)
             {

+ 2 - 0
Codes/Model/Server/Module/AOI/AOIEventType.cs

@@ -4,11 +4,13 @@
 	{
 		public struct UnitEnterSightRange
 		{
+			public AOIEntity A;
 			public AOIEntity B;
 		}
 
 		public struct UnitLeaveSightRange
 		{
+			public AOIEntity A;
 			public AOIEntity B;
 		}
 	}

+ 1 - 0
Codes/Model/Share/EventType.cs

@@ -55,6 +55,7 @@ namespace ET
 
         public struct AfterUnitCreate
         {
+            public Unit Unit;
         }
     }
 }

+ 10 - 0
Codes/Model/Share/Module/Config/ConfigComponent.cs

@@ -9,6 +9,16 @@ namespace ET
     [ComponentOf(typeof(Scene))]
     public class ConfigComponent: Entity, IAwake, IDestroy
     {
+        public struct GetAllConfigBytes
+        {
+            
+        }
+        
+        public struct GetOneConfigBytes
+        {
+            public string ConfigName;
+        }
+        
         public static ConfigComponent Instance;
 		
         public Dictionary<Type, object> AllConfig = new Dictionary<Type, object>();

+ 2 - 0
Codes/Model/Share/Module/Move/MoveEventType.cs

@@ -4,10 +4,12 @@
     {
         public struct MoveStart
         {
+            public Unit Unit;
         }
 
         public struct MoveStop
         {
+            public Unit Unit;
         }
     }
 }

+ 3 - 2
Codes/Model/Share/Module/Numeric/NumericComponent.cs

@@ -60,8 +60,8 @@ namespace ET
 
             if (isPublicEvent)
             {
-                Game.EventSystem.Publish(self.GetParent<Unit>(),
-                    new EventType.NumbericChange() { New = value, Old = oldValue, NumericType = numericType });
+                Game.EventSystem.Publish(self.DomainScene(),
+                    new EventType.NumbericChange() { Unit = self.GetParent<Unit>(), New = value, Old = oldValue, NumericType = numericType });
             }
         }
 
@@ -93,6 +93,7 @@ namespace ET
     {
         public struct NumbericChange
         {
+            public Unit Unit;
             public int NumericType;
             public long Old;
             public long New;

+ 2 - 2
Codes/Model/Share/Module/Unit/Unit.cs

@@ -24,7 +24,7 @@ namespace ET
             {
                 Vector3 oldPos = this.position;
                 this.position = value;
-                Game.EventSystem.Publish(this, new EventType.ChangePosition() { OldPos = oldPos });
+                Game.EventSystem.Publish(this.DomainScene(), new EventType.ChangePosition() { Unit = this, OldPos = oldPos });
             }
         }
 
@@ -45,7 +45,7 @@ namespace ET
             set
             {
                 this.rotation = value;
-                Game.EventSystem.Publish(this, new EventType.ChangeRotation());
+                Game.EventSystem.Publish(this.DomainScene(), new EventType.ChangeRotation() { Unit = this });
             }
         }
     }

+ 2 - 0
Codes/Model/Share/Module/Unit/UnitEventType.cs

@@ -6,11 +6,13 @@ namespace ET
     {
         public struct ChangePosition
         {
+            public Unit Unit;
             public Vector3 OldPos;
         }
 
         public struct ChangeRotation
         {
+            public Unit Unit;
         }
     }
 }

+ 1 - 0
Unity/Assets/Scripts/Core/Entity/SceneType.cs

@@ -2,6 +2,7 @@
 {
 	public enum SceneType
 	{
+		None = -1,
 		Process = 0,
 		Manager = 1,
 		Realm = 2,

+ 4 - 4
Unity/Assets/Scripts/Core/Event/IEvent.cs

@@ -7,20 +7,20 @@ namespace ET
 		Type GetEventType();
 	}
 	
-	public abstract class AEvent<E, A>: IEvent where E: Entity where A: struct
+	public abstract class AEvent<A>: IEvent where A: struct
 	{
 		public Type GetEventType()
 		{
 			return typeof (A);
 		}
 
-		protected abstract ETTask Run(E entity, A a);
+		protected abstract ETTask Run(Scene scene, A a);
 
-		public async ETTask Handle(E entity, A a)
+		public async ETTask Handle(Scene scene, A a)
 		{
 			try
 			{
-				await Run(entity, a);
+				await Run(scene, a);
 			}
 			catch (Exception e)
 			{

+ 27 - 28
Unity/Assets/Scripts/Core/Object/EventSystem.cs

@@ -668,7 +668,7 @@ namespace ET
             twoQueue.Swap();
         }
 
-        public async ETTask PublishAsync<E, T>(E entity, T a) where E: Entity where T : struct
+        public async ETTask PublishAsync<T>(Scene scene, T a) where T : struct
         {
             List<EventInfo> iEvents;
             if (!this.allEvents.TryGetValue(typeof(T), out iEvents))
@@ -676,36 +676,35 @@ namespace ET
                 return;
             }
 
-            using (ListComponent<ETTask> list = ListComponent<ETTask>.Create())
+            using ListComponent<ETTask> list = ListComponent<ETTask>.Create();
+            
+            foreach (EventInfo eventInfo in iEvents)
             {
-                foreach (EventInfo eventInfo in iEvents)
-                {
-                    if (entity.DomainScene().SceneType != eventInfo.SceneType)
-                    {
-                        continue;
-                    }
-                    
-                    if (!(eventInfo.IEvent is AEvent<E, T> aEvent))
-                    {
-                        Log.Error($"event error: {eventInfo.IEvent.GetType().Name}");
-                        continue;
-                    }
-
-                    list.Add(aEvent.Handle(entity, a));
-                }
-
-                try
+                if (scene.SceneType != eventInfo.SceneType && eventInfo.SceneType != SceneType.None)
                 {
-                    await ETTaskHelper.WaitAll(list);
+                    continue;
                 }
-                catch (Exception e)
+                    
+                if (!(eventInfo.IEvent is AEvent<T> aEvent))
                 {
-                    Log.Error(e);
+                    Log.Error($"event error: {eventInfo.IEvent.GetType().Name}");
+                    continue;
                 }
+
+                list.Add(aEvent.Handle(scene, a));
+            }
+
+            try
+            {
+                await ETTaskHelper.WaitAll(list);
+            }
+            catch (Exception e)
+            {
+                Log.Error(e);
             }
         }
 
-        public void Publish<E, T>(E entity, T a) where E: Entity where T : struct
+        public void Publish<T>(Scene scene, T a)where T : struct
         {
             List<EventInfo> iEvents;
             if (!this.allEvents.TryGetValue(typeof (T), out iEvents))
@@ -713,25 +712,25 @@ namespace ET
                 return;
             }
 
-            SceneType sceneType = entity.DomainScene().SceneType;
+            SceneType sceneType = scene.SceneType;
             foreach (EventInfo eventInfo in iEvents)
             {
-                if (sceneType != eventInfo.SceneType)
+                if (sceneType != eventInfo.SceneType && eventInfo.SceneType != SceneType.None)
                 {
                     continue;
                 }
 
                 
-                if (!(eventInfo.IEvent is AEvent<E, T> aEvent))
+                if (!(eventInfo.IEvent is AEvent<T> aEvent))
                 {
                     Log.Error($"event error: {eventInfo.IEvent.GetType().Name}");
                     continue;
                 }
                 
-                aEvent.Handle(entity, a).Coroutine();
+                aEvent.Handle(scene, a).Coroutine();
             }
         }
-        
+
         public void Callback(int type)
         {
             if (this.allCallbacks[type] is not IAction action)