LSUnitViewSystem.cs 2.4 KB

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