LSUnitViewSystem.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using TrueSync;
  3. using UnityEngine;
  4. namespace ET.Client
  5. {
  6. public static class LSUnitViewSystem
  7. {
  8. public class AwakeSystem: AwakeSystem<LSUnitView, GameObject>
  9. {
  10. protected override void Awake(LSUnitView self, GameObject go)
  11. {
  12. self.GameObject = go;
  13. self.Transform = go.transform;
  14. }
  15. }
  16. public class RollbackSystem: RollbackSystem<LSUnitView>
  17. {
  18. protected override void Rollback(LSUnitView self)
  19. {
  20. LSUnit unit = self.GetUnit();
  21. self.Transform.position = unit.Position.ToVector();
  22. self.Transform.rotation = unit.Rotation.ToQuaternion();
  23. self.t = 0;
  24. self.totalTime = 0;
  25. }
  26. }
  27. public class UpdateSystem: UpdateSystem<LSUnitView>
  28. {
  29. protected override void Update(LSUnitView self)
  30. {
  31. self.Update();
  32. }
  33. }
  34. private static void Update(this LSUnitView self)
  35. {
  36. LSUnit unit = self.GetUnit();
  37. Vector3 unitPos = unit.Position.ToVector();
  38. const float speed = 6f;
  39. float speed2 = speed * self.Room().SpeedMultiply;
  40. if (unitPos != self.Position)
  41. {
  42. float distance = (unitPos - self.Position).magnitude;
  43. self.totalTime = distance / speed2;
  44. self.t = 0;
  45. self.Position = unit.Position.ToVector();
  46. self.Rotation = unit.Rotation.ToQuaternion();
  47. }
  48. LSInput input = unit.GetComponent<LSInputComponent>().LSInput;
  49. if (input.V != TSVector2.zero)
  50. {
  51. self.GetComponent<LSAnimatorComponent>().SetFloatValue("Speed", speed2);
  52. }
  53. else
  54. {
  55. self.GetComponent<LSAnimatorComponent>().SetFloatValue("Speed", 0);
  56. }
  57. self.t += Time.deltaTime;
  58. self.Transform.rotation = Quaternion.Lerp(self.Transform.rotation, self.Rotation, self.t / 1f);
  59. self.Transform.position = Vector3.Lerp(self.Transform.position, self.Position, self.t / self.totalTime);
  60. }
  61. private static LSUnit GetUnit(this LSUnitView self)
  62. {
  63. LSUnit unit = self.Unit;
  64. if (unit != null)
  65. {
  66. return unit;
  67. }
  68. self.Unit = (self.Domain as Room).LSWorld.GetComponent<LSUnitComponent>().GetChild<LSUnit>(self.Id);
  69. return self.Unit;
  70. }
  71. }
  72. }