LSUnitViewSystem.cs 2.3 KB

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