Przeglądaj źródła

目前只需要一个LSWorld,所以做成组件挂载Room上即可,像守望先锋那种搞两个World也没问题,那样就需要做成Room的Child了

tanghai 2 lat temu
rodzic
commit
bc4cff361c

+ 1 - 1
Unity/Assets/Resources/GlobalConfig.asset

@@ -12,5 +12,5 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 36527db572638af47b03c805671cba75, type: 3}
   m_Name: GlobalConfig
   m_EditorClassIdentifier: 
-  CodeMode: 1
+  CodeMode: 3
   BuildType: 2

+ 2 - 1
Unity/Assets/Scripts/Hotfix/Client/LockStep/LSSceneChangeHelper.cs

@@ -19,7 +19,8 @@ namespace ET.Client
             // 等待Room2C_EnterMap消息
             WaitType.Wait_Room2C_Start waitRoom2CStart = await clientScene.GetComponent<ObjectWait>().Wait<WaitType.Wait_Room2C_Start>();
 
-            room.LSWorld = new LSWorld(SceneType.LockStepClient);
+            LSWorld lsWorld = new(SceneType.LockStepClient);
+            room.AddComponent(lsWorld);
             room.Init(waitRoom2CStart.Message);
             
             room.AddComponent<RoomClientUpdater>();

+ 2 - 1
Unity/Assets/Scripts/Hotfix/Server/LockStep/Map/RoomManagerComponentSystem.cs

@@ -13,7 +13,8 @@ namespace ET.Server
             
             room.AddComponent<RoomServerComponent, Match2Map_GetRoom>(request);
 
-            room.LSWorld = new LSWorld(SceneType.LockStepServer);
+            LSWorld lsWorld = new(SceneType.LockStepServer);
+            room.AddComponent(lsWorld);
 
             room.AddComponent<MailBoxComponent, MailboxType>(MailboxType.UnOrderMessageDispatcher);
             

+ 2 - 2
Unity/Assets/Scripts/Hotfix/Share/LockStep/LSHelper.cs

@@ -8,11 +8,11 @@ namespace ET
         public static void Rollback(Room room, int frame)
         {
             Log.Debug($"roll back start {frame}");
-            room.LSWorld.Dispose();
+            room.RemoveComponent<LSWorld>();
             FrameBuffer frameBuffer = room.FrameBuffer;
             
             // 回滚
-            room.LSWorld = frameBuffer.GetLSWorld(frame);
+            room.AddComponent(frameBuffer.GetLSWorld(frame));
             OneFrameMessages realFrameMessage = frameBuffer[frame];
             // 执行RealFrame
             room.Update(realFrameMessage, frame);

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Share/LockStep/LSUnitFactory.cs

@@ -12,7 +12,7 @@
 
 			lsUnit.AddComponent<LSInputComponent>();
 	        
-	        EventSystem.Instance.Publish(lsUnit.DomainScene(), new EventType.LSAfterUnitCreate() {LsUnit = lsUnit});
+	        EventSystem.Instance.Publish(lsUnit.LSWorld(), new EventType.LSAfterUnitCreate() {LsUnit = lsUnit});
             return lsUnit;
         }
     }

+ 6 - 5
Unity/Assets/Scripts/Hotfix/Share/LockStep/RoomSystem.cs

@@ -11,11 +11,12 @@ namespace ET
             
             self.FixedTimeCounter = new FixedTimeCounter(self.StartTime, 0, LSConstValue.UpdateInterval);
 
-            self.LSWorld.AddComponent<LSUnitComponent>();
+            LSWorld lsWorld = self.GetComponent<LSWorld>();
+            lsWorld.AddComponent<LSUnitComponent>();
             for (int i = 0; i < room2CStart.UnitInfo.Count; ++i)
             {
                 LockStepUnitInfo unitInfo = room2CStart.UnitInfo[i];
-                LSUnitFactory.Init(self.LSWorld, unitInfo);
+                LSUnitFactory.Init(lsWorld, unitInfo);
                 self.PlayerIds.Add(unitInfo.PlayerId);
             }
         }
@@ -23,11 +24,11 @@ namespace ET
 
         public static void Update(this Room self, OneFrameMessages oneFrameMessages, int frame)
         {
+            LSWorld lsWorld = self.GetComponent<LSWorld>();
             // 保存当前帧场景数据
-            self.FrameBuffer.SaveLSWorld(frame, self.LSWorld);
+            self.FrameBuffer.SaveLSWorld(frame, lsWorld);
             
             // 设置输入到每个LSUnit身上
-            LSWorld lsWorld = self.LSWorld;
             LSUnitComponent unitComponent = lsWorld.GetComponent<LSUnitComponent>();
             foreach (var kv in oneFrameMessages.Inputs)
             {
@@ -36,7 +37,7 @@ namespace ET
                 lsInputComponent.LSInput = kv.Value;
             }
             
-            lsWorld.Updater.Update();
+            lsWorld.Update();
         }
     }
 }

+ 1 - 1
Unity/Assets/Scripts/HotfixView/Client/LockStep/LSUnitViewSystem.cs

@@ -45,7 +45,7 @@ namespace ET.Client
                 return unit;
             }
 
-            self.Unit = (self.Domain as Room).LSWorld.GetComponent<LSUnitComponent>().GetChild<LSUnit>(self.Id);
+            self.Unit = (self.Domain as Room).GetComponent<LSWorld>().GetComponent<LSUnitComponent>().GetChild<LSUnit>(self.Id);
             return self.Unit;
         }
     }

+ 2 - 2
Unity/Assets/Scripts/Model/Share/LockStep/LSEntity.cs

@@ -47,7 +47,7 @@ namespace ET
 
         protected override void RegisterSystem()
         {
-            LSWorld lsWorld = this.DomainScene();
+            LSWorld lsWorld = this.LSWorld();
             TypeSystems.OneTypeSystems oneTypeSystems = LSSington.Instance.GetOneTypeSystems(this.GetType());
             if (oneTypeSystems == null)
             {
@@ -56,7 +56,7 @@ namespace ET
 
             if (oneTypeSystems.QueueFlag[LSQueneUpdateIndex.LSUpdate])
             {
-                lsWorld.Updater.Add(this);
+                lsWorld.AddToUpdater(this);
             }
         }
     }

+ 4 - 8
Unity/Assets/Scripts/Model/Share/LockStep/LSUpdater.cs

@@ -1,21 +1,17 @@
 using System;
 using System.Collections.Generic;
-using MongoDB.Bson.Serialization.Attributes;
 
 namespace ET
 {
     public class LSUpdater
     {
-        [BsonIgnore]
-        public LSWorld Parent { get; set; }
-        
-        private SortedSet<long> updateIds = new();
+        private readonly SortedSet<long> updateIds = new();
 
-        private Dictionary<long, EntityRef<LSEntity>> lsEntities = new();
+        private readonly Dictionary<long, EntityRef<LSEntity>> lsEntities = new();
 
-        private Queue<long> addUpdateIds = new();
+        private readonly Queue<long> addUpdateIds = new();
 
-        private Queue<long> removeUpdateIds = new();
+        private readonly Queue<long> removeUpdateIds = new();
 
         public void Update()
         {

+ 16 - 25
Unity/Assets/Scripts/Model/Share/LockStep/LSWorld.cs

@@ -7,42 +7,26 @@ namespace ET
 {
     public static class LSWorldSystem
     {
-        [ObjectSystem]
-        public class LSSceneAwakeSystem: AwakeSystem<LSWorld>
-        {
-            protected override void Awake(LSWorld self)
-            {
-                self.Updater.Parent = self;
-            }
-        }
-        
-        public class DeserializeSystem: DeserializeSystem<LSWorld>
-        {
-            protected override void Deserialize(LSWorld self)
-            {
-                self.Updater.Parent = self;
-            }
-        }
-
-        public static LSWorld DomainScene(this LSEntity entity)
+        public static LSWorld LSWorld(this LSEntity entity)
         {
             return entity.Domain as LSWorld;
         }
 
         public static long GetId(this LSEntity entity)
         {
-            return entity.DomainScene().GetId();
+            return entity.LSWorld().GetId();
         }
         
         public static TSRandom GetRandom(this LSEntity entity)
         {
-            return entity.DomainScene().Random;
+            return entity.LSWorld().Random;
         }
     }
 
     [EnableMethod]
     [ChildOf]
-    public class LSWorld: LSEntity, IAwake, IScene, IDeserialize
+    [ComponentOf]
+    public class LSWorld: LSEntity, IAwake, IScene
     {
         public LSWorld()
         {
@@ -54,13 +38,10 @@ namespace ET
 
             this.SceneType = sceneType;
             
-            this.Updater.Parent = this;
-            
             Log.Info($"LSScene create: {this.Id} {this.InstanceId}");
         }
 
-        [BsonElement]
-        public LSUpdater Updater = new();
+        private readonly LSUpdater updater = new();
 
         [BsonElement]
         private long idGenerator;
@@ -73,5 +54,15 @@ namespace ET
         public TSRandom Random { get; set; }
         
         public SceneType SceneType { get; set; }
+
+        public void AddToUpdater(LSEntity lsEntity)
+        {
+            this.updater.Add(lsEntity);
+        }
+
+        public void Update()
+        {
+            this.updater.Update();
+        }
     }
 }

+ 0 - 15
Unity/Assets/Scripts/Model/Share/LockStep/Room.cs

@@ -10,21 +10,6 @@ namespace ET
         public SceneType SceneType { get; set; } = SceneType.Room;
         public string Name { get; set; }
         
-        private EntityRef<LSWorld> lsWorld;
-        
-        public LSWorld LSWorld
-        {
-            get
-            {
-                return this.lsWorld;
-            }
-            set
-            {
-                this.AddChild(value);
-                this.lsWorld = value;
-            }
-        }
-
         public long StartTime { get; set; }
 
         public FrameBuffer FrameBuffer { get; } = new();