LSUnitViewSystem.cs 2.3 KB

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