RoomSystem.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace ET
  5. {
  6. [FriendOf(typeof(Room))]
  7. public static class RoomSystem
  8. {
  9. public static Room Room(this Entity entity)
  10. {
  11. return entity.Domain as Room;
  12. }
  13. public static void Init(this Room self, List<LockStepUnitInfo> unitInfos, long startTime)
  14. {
  15. self.StartTime = startTime;
  16. self.Replay.UnitInfos = unitInfos;
  17. self.FixedTimeCounter = new FixedTimeCounter(self.StartTime, 0, LSConstValue.UpdateInterval);
  18. LSWorld lsWorld = self.LSWorld;
  19. lsWorld.AddComponent<LSUnitComponent>();
  20. for (int i = 0; i < unitInfos.Count; ++i)
  21. {
  22. LockStepUnitInfo unitInfo = unitInfos[i];
  23. LSUnitFactory.Init(lsWorld, unitInfo);
  24. self.PlayerIds.Add(unitInfo.PlayerId);
  25. }
  26. }
  27. public static void Update(this Room self, OneFrameInputs oneFrameInputs, int frame)
  28. {
  29. LSWorld lsWorld = self.LSWorld;
  30. // 设置输入到每个LSUnit身上
  31. LSUnitComponent unitComponent = lsWorld.GetComponent<LSUnitComponent>();
  32. foreach (var kv in oneFrameInputs.Inputs)
  33. {
  34. LSUnit lsUnit = unitComponent.GetChild<LSUnit>(kv.Key);
  35. LSInputComponent lsInputComponent = lsUnit.GetComponent<LSInputComponent>();
  36. lsInputComponent.LSInput = kv.Value;
  37. }
  38. lsWorld.Update();
  39. }
  40. public static LSWorld GetLSWorld(this Room self, SceneType sceneType, int frame)
  41. {
  42. MemoryBuffer memoryBuffer = self.FrameBuffer.Snapshot(frame);
  43. memoryBuffer.Seek(0, SeekOrigin.Begin);
  44. LSWorld lsWorld = MongoHelper.Deserialize(typeof (LSWorld), memoryBuffer) as LSWorld;
  45. lsWorld.SceneType = sceneType;
  46. memoryBuffer.Seek(0, SeekOrigin.Begin);
  47. return lsWorld;
  48. }
  49. public static void SaveLSWorld(this Room self, int frame)
  50. {
  51. MemoryBuffer memoryBuffer = self.FrameBuffer.Snapshot(frame);
  52. memoryBuffer.Seek(0, SeekOrigin.Begin);
  53. memoryBuffer.SetLength(0);
  54. if (frame != self.LSWorld.Frame)
  55. {
  56. Log.Error($"lsworld frame diff: {frame} {self.LSWorld.Frame}");
  57. }
  58. MongoHelper.Serialize(self.LSWorld, memoryBuffer);
  59. memoryBuffer.Seek(0, SeekOrigin.Begin);
  60. long hash = memoryBuffer.GetBuffer().Hash(0, (int) memoryBuffer.Length);
  61. self.FrameBuffer.SetHash(frame, hash);
  62. }
  63. // 记录需要存档的数据
  64. public static void Record(this Room self, int frame)
  65. {
  66. if (self.IsReplay)
  67. {
  68. return;
  69. }
  70. OneFrameInputs oneFrameInputs = self.FrameBuffer.FrameInputs(frame);
  71. OneFrameInputs saveInput = new();
  72. oneFrameInputs.CopyTo(saveInput);
  73. self.Replay.FrameInputs.Add(saveInput);
  74. if (frame % LSConstValue.SaveLSWorldFrameCount == 0)
  75. {
  76. MemoryBuffer memoryBuffer = self.FrameBuffer.Snapshot(frame);
  77. byte[] bytes = memoryBuffer.ToArray();
  78. self.Replay.Snapshots.Add(bytes);
  79. }
  80. }
  81. }
  82. }