LSUnitViewSystem.cs 2.3 KB

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