OneFrameInputsHandler.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. namespace ET.Client
  3. {
  4. [MessageHandler(SceneType.LockStep)]
  5. public class OneFrameInputsHandler: AMHandler<OneFrameInputs>
  6. {
  7. protected override async ETTask Run(Session session, OneFrameInputs input)
  8. {
  9. Room room = session.DomainScene().GetComponent<Room>();
  10. FrameBuffer frameBuffer = room.FrameBuffer;
  11. int frame = room.AuthorityFrame + 1;
  12. ++room.AuthorityFrame;
  13. // 服务端返回的消息比预测的还早
  14. if (room.AuthorityFrame > room.PredictionFrame)
  15. {
  16. OneFrameInputs authorityFrame = frameBuffer.FrameInputs(room.AuthorityFrame);
  17. input.CopyTo(authorityFrame);
  18. return;
  19. }
  20. // 服务端返回来的消息,跟预测消息对比
  21. OneFrameInputs predictionInput = frameBuffer.FrameInputs(frame);
  22. // 对比失败有两种可能,
  23. // 1是别人的输入预测失败,这种很正常,
  24. // 2 自己的输入对比失败,这种情况是自己发送的消息比服务器晚到了,服务器使用了你的上一次输入
  25. // 回滚重新预测的时候,自己的输入不用变化
  26. if (input != predictionInput)
  27. {
  28. input.CopyTo(predictionInput);
  29. // 回滚到frameBuffer.AuthorityFrame
  30. LSHelper.Rollback(room, room.AuthorityFrame);
  31. }
  32. else
  33. {
  34. room.Record(frame);
  35. }
  36. // 回收消息,减少GC
  37. NetServices.Instance.RecycleMessage(input);
  38. await ETTask.CompletedTask;
  39. }
  40. }
  41. }