RoomClientUpdaterSystem.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.IO;
  3. using MongoDB.Bson;
  4. namespace ET.Client
  5. {
  6. [FriendOf(typeof (RoomClientUpdater))]
  7. public static class RoomClientUpdaterSystem
  8. {
  9. public class AwakeSystem: AwakeSystem<RoomClientUpdater>
  10. {
  11. protected override void Awake(RoomClientUpdater self)
  12. {
  13. Room room = self.GetParent<Room>();
  14. self.MyId = room.GetParent<Scene>().GetComponent<PlayerComponent>().MyId;
  15. }
  16. }
  17. [FriendOf(typeof (Room))]
  18. public class UpdateSystem: UpdateSystem<RoomClientUpdater>
  19. {
  20. protected override void Update(RoomClientUpdater self)
  21. {
  22. self.Update();
  23. }
  24. }
  25. private static void Update(this RoomClientUpdater self)
  26. {
  27. Room room = self.GetParent<Room>();
  28. FrameBuffer frameBuffer = room.FrameBuffer;
  29. long timeNow = TimeHelper.ServerFrameTime();
  30. Scene clientScene = room.GetParent<Scene>();
  31. if (timeNow < room.FixedTimeCounter.FrameTime(frameBuffer.PredictionFrame))
  32. {
  33. return;
  34. }
  35. OneFrameMessages oneFrameMessages = GetOneFrameMessages(self, frameBuffer.PredictionFrame);
  36. room.Update(oneFrameMessages, frameBuffer.PredictionFrame);
  37. ++frameBuffer.PredictionFrame;
  38. FrameMessage frameMessage = NetServices.Instance.FetchMessage<FrameMessage>();
  39. frameMessage.Frame = oneFrameMessages.Frame;
  40. frameMessage.Input = self.Input;
  41. clientScene.GetComponent<SessionComponent>().Session.Send(frameMessage);
  42. }
  43. private static OneFrameMessages GetOneFrameMessages(this RoomClientUpdater self, int frame)
  44. {
  45. Room room = self.GetParent<Room>();
  46. FrameBuffer frameBuffer = room.FrameBuffer;
  47. if (frame <= frameBuffer.RealFrame)
  48. {
  49. return frameBuffer.GetFrame(frame);
  50. }
  51. // predict
  52. return GetPredictionOneFrameMessage(self, frame);
  53. }
  54. // 获取预测一帧的消息
  55. private static OneFrameMessages GetPredictionOneFrameMessage(this RoomClientUpdater self, int frame)
  56. {
  57. Room room = self.GetParent<Room>();
  58. OneFrameMessages predictionFrame = room.FrameBuffer.GetFrame(frame);
  59. OneFrameMessages preFrame = room.FrameBuffer.GetFrame(frame - 1);
  60. preFrame?.CopyTo(predictionFrame);
  61. predictionFrame.Frame = frame;
  62. predictionFrame.Inputs[self.MyId] = self.Input;
  63. return predictionFrame;
  64. }
  65. }
  66. }