LSHelper.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using ET.Client;
  2. namespace ET
  3. {
  4. public static class LSHelper
  5. {
  6. public static void RunRollbackSystem(Entity entity)
  7. {
  8. if (entity is LSEntity)
  9. {
  10. return;
  11. }
  12. LSSington.Instance.Rollback(entity);
  13. if (entity.ComponentsCount() > 0)
  14. {
  15. foreach (var kv in entity.Components)
  16. {
  17. RunRollbackSystem(kv.Value);
  18. }
  19. }
  20. if (entity.ChildrenCount() > 0)
  21. {
  22. foreach (var kv in entity.Children)
  23. {
  24. RunRollbackSystem(kv.Value);
  25. }
  26. }
  27. }
  28. // 回滚
  29. public static void Rollback(Room room, int frame)
  30. {
  31. Log.Debug($"roll back start {frame}");
  32. room.LSWorld.Dispose();
  33. FrameBuffer frameBuffer = room.FrameBuffer;
  34. // 回滚
  35. room.LSWorld = room.GetLSWorld(frame);
  36. OneFrameInputs authorityFrameInput = frameBuffer.FrameInputs(frame);
  37. // 执行RealFrame
  38. room.Update(authorityFrameInput, frame);
  39. // 重新执行预测的帧
  40. for (int i = room.AuthorityFrame + 1; i <= room.PredictionFrame; ++i)
  41. {
  42. OneFrameInputs oneFrameInputs = frameBuffer.FrameInputs(i);
  43. LSHelper.CopyOtherInputsTo(room, authorityFrameInput, oneFrameInputs); // 重新预测消息
  44. room.Update(oneFrameInputs, i);
  45. }
  46. RunRollbackSystem(room);
  47. Log.Debug($"roll back finish {frame}");
  48. }
  49. // 重新调整预测消息,只需要调整其他玩家的输入
  50. public static void CopyOtherInputsTo(Room room, OneFrameInputs from, OneFrameInputs to)
  51. {
  52. long myId = room.GetComponent<RoomClientUpdater>().MyId;
  53. foreach (var kv in from.Inputs)
  54. {
  55. if (kv.Key == myId)
  56. {
  57. continue;
  58. }
  59. to.Inputs[kv.Key] = kv.Value;
  60. }
  61. }
  62. }
  63. }