LSUnitViewSystem.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 UpdateSystem: UpdateSystem<LSUnitView>
  17. {
  18. protected override void Update(LSUnitView self)
  19. {
  20. self.Update();
  21. }
  22. }
  23. private static void Update(this LSUnitView self)
  24. {
  25. LSUnit unit = self.GetUnit();
  26. Vector3 unitPos = unit.Position.ToVector();
  27. const float speed = 6f;
  28. if (unitPos != self.Position)
  29. {
  30. float distance = (unitPos - self.Position).magnitude;
  31. self.totalTime = distance / speed;
  32. self.t = 0;
  33. self.Position = unit.Position.ToVector();
  34. self.Rotation = unit.Rotation.ToQuaternion();
  35. }
  36. LSInput input = unit.GetComponent<LSInputComponent>().LSInput;
  37. if (input.V != TSVector2.zero)
  38. {
  39. self.GetComponent<LSAnimatorComponent>().SetFloatValue("Speed", speed);
  40. }
  41. else
  42. {
  43. self.GetComponent<LSAnimatorComponent>().SetFloatValue("Speed", 0);
  44. }
  45. self.t += Time.deltaTime;
  46. self.Transform.rotation = Quaternion.Lerp(self.Transform.rotation, self.Rotation, self.t / 1f);
  47. self.Transform.position = Vector3.Lerp(self.Transform.position, self.Position, self.t / self.totalTime);
  48. }
  49. private static LSUnit GetUnit(this LSUnitView self)
  50. {
  51. LSUnit unit = self.Unit;
  52. if (unit != null)
  53. {
  54. return unit;
  55. }
  56. self.Unit = (self.Domain as Room).GetComponent<LSWorld>().GetComponent<LSUnitComponent>().GetChild<LSUnit>(self.Id);
  57. return self.Unit;
  58. }
  59. }
  60. }