tanghai 2 лет назад
Родитель
Сommit
575e2d2f83


Разница между файлами не показана из-за своего большого размера
+ 803 - 83
Unity/Assets/Bundles/UI/LockStep/UILSRoom.prefab


+ 21 - 11
Unity/Assets/Scripts/Hotfix/Client/LockStep/LSReplayUpdaterSystem.cs

@@ -18,22 +18,32 @@ namespace ET.Client
         {
             Room room = self.GetParent<Room>();
             long timeNow = TimeHelper.ServerFrameTime();
-            
-            if (timeNow < room.FixedTimeCounter.FrameTime(room.AuthorityFrame + 1))
+
+            int old = room.AuthorityFrame;
+            for (int i = 0; i < 10; ++i)
             {
-                return;
-            }
+                if (room.AuthorityFrame + 1 >= room.Replay.FrameInputs.Count)
+                {
+                    break;
+                }
+                
+                if (timeNow < room.FixedTimeCounter.FrameTime(room.AuthorityFrame + 1))
+                {
+                    break;
+                }
+
+                ++room.AuthorityFrame;
 
-            ++room.AuthorityFrame;
+                OneFrameInputs oneFrameInputs = room.Replay.FrameInputs[room.AuthorityFrame];
+            
+                room.Update(oneFrameInputs, room.AuthorityFrame);
+                room.SpeedMultiply = i + 1;
+            }
 
-            if (room.AuthorityFrame >= room.Replay.FrameInputs.Count)
+            if (room.AuthorityFrame > old)
             {
-                return;
+                Log.Debug($"111111111111111111 replay update: {old} {room.AuthorityFrame}");
             }
-            
-            OneFrameInputs oneFrameInputs = room.Replay.FrameInputs[room.AuthorityFrame];
-            
-            room.Update(oneFrameInputs, room.AuthorityFrame);
         }
     }
 }

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

@@ -82,5 +82,35 @@ namespace ET
             byte[] bytes = MemoryPackHelper.Serialize(room.Replay);
             File.WriteAllBytes(path, bytes);
         }
+        
+        public static void JumpReplay(Room room, int frame)
+        {
+            if (!room.IsReplay)
+            {
+                return;
+            }
+
+            if (frame >= room.Replay.FrameInputs.Count)
+            {
+                frame = room.Replay.FrameInputs.Count - 1;
+            }
+            
+            int snapshotIndex = frame / LSConstValue.SaveLSWorldFrameCount;
+            Log.Debug($"jump replay start {room.AuthorityFrame} {frame} {snapshotIndex}");
+            if (snapshotIndex != room.AuthorityFrame / LSConstValue.SaveLSWorldFrameCount || frame < room.AuthorityFrame)
+            {
+                room.LSWorld.Dispose();
+                // 回滚
+                byte[] memoryBuffer = room.Replay.Snapshots[snapshotIndex];
+                LSWorld lsWorld = MongoHelper.Deserialize(typeof (LSWorld), memoryBuffer, 0, memoryBuffer.Length) as LSWorld;
+                room.LSWorld = lsWorld;
+                room.AuthorityFrame = snapshotIndex * LSConstValue.SaveLSWorldFrameCount;
+                RunRollbackSystem(room);
+            }
+            
+            room.FixedTimeCounter.Reset(TimeHelper.ServerFrameTime() - frame * LSConstValue.UpdateInterval, 0);
+
+            Log.Debug($"jump replay finish {frame}");
+        }
     }
 }

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

@@ -83,10 +83,13 @@ namespace ET
             OneFrameInputs saveInput = new();
             oneFrameInputs.CopyTo(saveInput);
             self.Replay.FrameInputs.Add(saveInput);
+            Log.Debug($"111111111111111111111111: {frame}");
             if (frame % LSConstValue.SaveLSWorldFrameCount == 0)
             {
                 MemoryBuffer memoryBuffer = self.FrameBuffer.Snapshot(frame);
-                self.Replay.Snapshots.Add(memoryBuffer.ToArray());   
+                byte[] bytes = memoryBuffer.ToArray();
+                self.Replay.Snapshots.Add(bytes);
+                Log.Debug($"1111111111111111111111112: {frame} {bytes.Length}");
             }
         }
     }

+ 6 - 3
Unity/Assets/Scripts/HotfixView/Client/LockStep/LSSceneInitFinish_Finish.cs

@@ -8,9 +8,12 @@ namespace ET.Client
             Room room = clientScene.GetComponent<Room>();
             
             room.AddComponent<LSCameraComponent>();
-            
-            room.AddComponent<LSOperaComponent>();
-            
+
+            if (!room.IsReplay)
+            {
+                room.AddComponent<LSOperaComponent>();
+            }
+
             await UIHelper.Remove(clientScene, UIType.UILSLobby);
             await ETTask.CompletedTask;
         }

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

@@ -15,6 +15,18 @@ namespace ET.Client
             }
         }
         
+        public class RollbackSystem: RollbackSystem<LSUnitView>
+        {
+            protected override void Rollback(LSUnitView self)
+            {
+                LSUnit unit = self.GetUnit();
+                self.Transform.position = unit.Position.ToVector();
+                self.Transform.rotation = unit.Rotation.ToQuaternion();
+                self.t = 0;
+                self.totalTime = 0;
+            }
+        }
+        
         public class UpdateSystem: UpdateSystem<LSUnitView>
         {
             protected override void Update(LSUnitView self)
@@ -28,12 +40,13 @@ namespace ET.Client
             LSUnit unit = self.GetUnit();
 
             Vector3 unitPos = unit.Position.ToVector();
-            const float speed = 6f;  
+            const float speed = 6f;
+            float speed2 = 6 * self.Room().SpeedMultiply;
             
             if (unitPos != self.Position)
             {
                 float distance = (unitPos - self.Position).magnitude;
-                self.totalTime = distance / speed;
+                self.totalTime = distance / speed2;
                 self.t = 0;
                 self.Position = unit.Position.ToVector();
                 self.Rotation = unit.Rotation.ToQuaternion();
@@ -42,7 +55,7 @@ namespace ET.Client
             LSInput input = unit.GetComponent<LSInputComponent>().LSInput;
             if (input.V != TSVector2.zero)
             {
-                self.GetComponent<LSAnimatorComponent>().SetFloatValue("Speed", speed);
+                self.GetComponent<LSAnimatorComponent>().SetFloatValue("Speed", speed2);
             }
             else
             {

+ 1 - 1
Unity/Assets/Scripts/HotfixView/Client/LockStep/UI/UILSLobby/UILSLobbyComponentSystem.cs

@@ -36,7 +36,7 @@ namespace ET.Client
             byte[] bytes = File.ReadAllBytes(self.replayPath.text);
             
             Replay replay = MemoryPackHelper.Deserialize(typeof (Replay), bytes, 0, bytes.Length) as Replay;
-            
+            Log.Debug($"start replay: {replay.Snapshots.Count} {replay.FrameInputs.Count} {replay.UnitInfos.Count}");
             LSSceneChangeHelper.SceneChangeToReplay(self.ClientScene(), replay).Coroutine();
         }
     }

+ 50 - 7
Unity/Assets/Scripts/HotfixView/Client/LockStep/UI/UILSRoom/UILSRoomComponentSystem.cs

@@ -1,3 +1,4 @@
+using System;
 using UnityEngine;
 using UnityEngine.UI;
 
@@ -10,20 +11,62 @@ namespace ET.Client
             protected override void Awake(UILSRoomComponent self)
             {
                 ReferenceCollector rc = self.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
-                self.saveReplay = rc.Get<GameObject>("SaveReplay");
-                self.saveName = rc.Get<GameObject>("SaveName").GetComponent<InputField>();
-				
-                self.saveReplay.GetComponent<Button>().onClick.AddListener(()=> { self.OnSaveReplay().Coroutine(); });
+                GameObject replay = rc.Get<GameObject>("Replay");
+                GameObject play = rc.Get<GameObject>("Play");
+                
+                Room room = self.Room();
+                if (room.IsReplay)
+                {
+                    replay.SetActive(true);
+                    play.SetActive(false);
+                    self.totalFrame = rc.Get<GameObject>("framecount").GetComponent<Text>();
+                    self.frameText = rc.Get<GameObject>("progress").GetComponent<Text>();
+                    self.jumpToField = rc.Get<GameObject>("jumpToCount").GetComponent<InputField>();
+                    self.jump = rc.Get<GameObject>("jump").GetComponent<Button>();
+                    self.jump.onClick.AddListener(self.JumpReplay);
+                    
+                    self.totalFrame.text = self.Room().Replay.FrameInputs.Count.ToString();
+                }
+                else
+                {
+                    replay.SetActive(false);
+                    play.SetActive(true);
+                    self.saveReplay = rc.Get<GameObject>("SaveReplay").GetComponent<Button>();
+                    self.saveName = rc.Get<GameObject>("SaveName").GetComponent<InputField>();
+                    self.saveReplay.onClick.AddListener(self.OnSaveReplay);
+                }
             }
         }
 
-        private static async ETTask OnSaveReplay(this UILSRoomComponent self)
+        public class UpdateSystem: UpdateSystem<UILSRoomComponent>
+        {
+            protected override void Update(UILSRoomComponent self)
+            {
+                Room room = self.Room();
+                if (room.IsReplay)
+                {
+                    if (self.frame == room.AuthorityFrame)
+                    {
+                        return;
+                    }
+
+                    self.frame = room.AuthorityFrame;
+                    self.frameText.text = room.AuthorityFrame.ToString();
+                }
+            }
+        }
+
+        private static void OnSaveReplay(this UILSRoomComponent self)
         {
             string name = self.saveName.text;
             
             LSHelper.SaveReplay(self.Room(), name);
-
-            await ETTask.CompletedTask;
+        }
+        
+        private static void JumpReplay(this UILSRoomComponent self)
+        {
+            int toFrame = int.Parse(self.jumpToField.text);
+            LSHelper.JumpReplay(self.Room(), toFrame);
         }
     }
 }

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

@@ -3,10 +3,10 @@ namespace ET
     public class FixedTimeCounter
     {
         private long startTime;
-        private long startFrame;
+        private int startFrame;
         public int Interval { get; private set; }
 
-        public FixedTimeCounter(long startTime, long startFrame, int interval)
+        public FixedTimeCounter(long startTime, int startFrame, int interval)
         {
             this.startTime = startTime;
             this.startFrame = startFrame;
@@ -24,5 +24,11 @@ namespace ET
         {
             return this.startTime + (frame - this.startFrame) * this.Interval;
         }
+        
+        public void Reset(long time, int frame)
+        {
+            this.startTime = time;
+            this.startFrame = frame;
+        }
     }
 }

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

@@ -46,5 +46,7 @@ namespace ET
         }
 
         public bool IsReplay { get; set; }
+        
+        public int SpeedMultiply { get; set; }
     }
 }

+ 1 - 1
Unity/Assets/Scripts/ModelView/Client/LockStep/LSUnitView.cs

@@ -3,7 +3,7 @@ using UnityEngine;
 namespace ET
 {
     [ChildOf(typeof(LSUnitViewComponent))]
-    public class LSUnitView: Entity, IAwake<GameObject>, IUpdate
+    public class LSUnitView: Entity, IAwake<GameObject>, IUpdate, IRollback
     {
         public GameObject GameObject { get; set; }
         public Transform Transform { get; set; }

+ 7 - 3
Unity/Assets/Scripts/ModelView/Client/LockStep/UI/UILSRoom/UILSRoomComponent.cs

@@ -3,10 +3,14 @@ using UnityEngine.UI;
 
 namespace ET.Client
 {
-    public class UILSRoomComponent: Entity, IAwake
+    public class UILSRoomComponent: Entity, IAwake, IUpdate
     {
-        public GameObject saveReplay;
-
+        public Button saveReplay;
         public InputField saveName;
+        public Text totalFrame;
+        public Text frameText;
+        public InputField jumpToField;
+        public Button jump;
+        public int frame;
     }
 }

Некоторые файлы не были показаны из-за большого количества измененных файлов