TurnComponentSystem.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using UnityEngine;
  2. namespace ET
  3. {
  4. public class TurnComponentUpdateSystem : UpdateSystem<TurnComponent>
  5. {
  6. public override void Update(TurnComponent self)
  7. {
  8. self.Update();
  9. }
  10. }
  11. public static class TurnComponentSystem
  12. {
  13. public static void Update(this TurnComponent self)
  14. {
  15. self.UpdateTurn();
  16. }
  17. private static void UpdateTurn(this TurnComponent self)
  18. {
  19. //Log.Debug($"update turn: {this.t} {this.TurnTime}");
  20. if (self.t > self.TurnTime)
  21. {
  22. return;
  23. }
  24. self.t += Time.deltaTime;
  25. Quaternion v = Quaternion.Slerp(self.From, self.To, self.t / self.TurnTime);
  26. self.GetParent<Unit>().Rotation = v;
  27. }
  28. /// <summary>
  29. /// 改变Unit的朝向
  30. /// </summary>
  31. public static void Turn2D(this TurnComponent self, Vector3 dir, float turnTime = 0.1f)
  32. {
  33. Vector3 nexpos = self.GetParent<Unit>().GameObject.transform.position + dir;
  34. self.Turn(nexpos, turnTime);
  35. }
  36. /// <summary>
  37. /// 改变Unit的朝向
  38. /// </summary>
  39. public static void Turn(this TurnComponent self, Vector3 target, float turnTime = 0.1f)
  40. {
  41. Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(self.GetParent<Unit>().Position, target);
  42. self.To = quaternion;
  43. self.From = self.GetParent<Unit>().Rotation;
  44. self.t = 0;
  45. self.TurnTime = turnTime;
  46. }
  47. /// <summary>
  48. /// 改变Unit的朝向
  49. /// </summary>
  50. /// <param name="angle">与X轴正方向的夹角</param>
  51. public static void Turn(this TurnComponent self, float angle, float turnTime = 0.1f)
  52. {
  53. Quaternion quaternion = PositionHelper.GetAngleToQuaternion(angle);
  54. self.To = quaternion;
  55. self.From = self.GetParent<Unit>().Rotation;
  56. self.t = 0;
  57. self.TurnTime = turnTime;
  58. }
  59. public static void Turn(this TurnComponent self, Quaternion quaternion, float turnTime = 0.1f)
  60. {
  61. self.To = quaternion;
  62. self.From = self.GetParent<Unit>().Rotation;
  63. self.t = 0;
  64. self.TurnTime = turnTime;
  65. }
  66. public static void TurnImmediately(this TurnComponent self, Quaternion quaternion)
  67. {
  68. self.GetParent<Unit>().Rotation = quaternion;
  69. }
  70. public static void TurnImmediately(this TurnComponent self, Vector3 target)
  71. {
  72. Vector3 nowPos = self.GetParent<Unit>().Position;
  73. if (nowPos == target)
  74. {
  75. return;
  76. }
  77. Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(self.GetParent<Unit>().Position, target);
  78. self.GetParent<Unit>().Rotation = quaternion;
  79. }
  80. public static void TurnImmediately(this TurnComponent self, float angle)
  81. {
  82. Quaternion quaternion = PositionHelper.GetAngleToQuaternion(angle);
  83. self.GetParent<Unit>().Rotation = quaternion;
  84. }
  85. }
  86. }