MoveComponent.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using UnityEngine;
  4. namespace ETModel
  5. {
  6. [ObjectSystem]
  7. public class MoveComponentAwakeSystem : AwakeSystem<MoveComponent>
  8. {
  9. public override void Awake(MoveComponent self)
  10. {
  11. self.Awake();
  12. }
  13. }
  14. [ObjectSystem]
  15. public class MoveComponentUpdateSystem : UpdateSystem<MoveComponent>
  16. {
  17. public override void Update(MoveComponent self)
  18. {
  19. self.Update();
  20. }
  21. }
  22. public class MoveComponent : Component
  23. {
  24. private AnimatorComponent animatorComponent;
  25. public long mainSpeed;
  26. public Vector3 speed;
  27. // turn
  28. public Quaternion To;
  29. public Quaternion From;
  30. public float t = float.MaxValue;
  31. public float TurnTime = 0.1f;
  32. public bool IsArrived { get; private set; } = true;
  33. public Vector3 Dest;
  34. public TaskCompletionSource<bool> moveTcs;
  35. public Vector3 Speed
  36. {
  37. get
  38. {
  39. return speed;
  40. }
  41. set
  42. {
  43. speed = value;
  44. animatorComponent?.SetFloatValue("Speed", speed.magnitude);
  45. }
  46. }
  47. public void Awake()
  48. {
  49. this.animatorComponent = this.Entity.GetComponent<AnimatorComponent>();
  50. }
  51. public void Update()
  52. {
  53. UpdateTurn();
  54. if (this.IsArrived)
  55. {
  56. return;
  57. }
  58. if (this.Speed.sqrMagnitude < 0.01f)
  59. {
  60. return;
  61. }
  62. Unit unit = this.GetParent<Unit>();
  63. Vector3 moveVector3 = this.Speed * Time.deltaTime;
  64. float dist = (this.Dest - unit.Position).magnitude;
  65. if (moveVector3.magnitude >= dist || dist < 0.1f)
  66. {
  67. unit.Position = this.Dest;
  68. this.IsArrived = true;
  69. this.moveTcs?.SetResult(true);
  70. return;
  71. }
  72. unit.Position = unit.Position + moveVector3;
  73. }
  74. private void UpdateTurn()
  75. {
  76. //Log.Debug($"update turn: {this.t} {this.TurnTime}");
  77. if (this.t > this.TurnTime)
  78. {
  79. return;
  80. }
  81. this.t += Time.deltaTime;
  82. Quaternion v = Quaternion.Slerp(this.From, this.To, this.t / this.TurnTime);
  83. this.GetParent<Unit>().Rotation = v;
  84. }
  85. public void MoveTo(Vector3 dest, float speedValue)
  86. {
  87. if ((dest - this.GetParent<Unit>().Position).magnitude < 0.1f)
  88. {
  89. this.IsArrived = true;
  90. return;
  91. }
  92. if ((dest - this.Dest).magnitude < 0.1f)
  93. {
  94. return;
  95. }
  96. this.IsArrived = false;
  97. Vector3 spd = dest - this.GetParent<Unit>().Position;
  98. spd = spd.normalized * speedValue;
  99. this.Speed = spd;
  100. this.Dest = dest;
  101. }
  102. public Task<bool> MoveToAsync(Vector3 dest, float speedValue, CancellationToken cancellationToken)
  103. {
  104. if ((dest - this.GetParent<Unit>().Position).magnitude < 0.1f)
  105. {
  106. this.IsArrived = true;
  107. return Task.FromResult(false);
  108. }
  109. if ((dest - this.Dest).magnitude < 0.1f)
  110. {
  111. return Task.FromResult(false);
  112. }
  113. this.moveTcs = new TaskCompletionSource<bool>();
  114. this.IsArrived = false;
  115. Vector3 spd = dest - this.GetParent<Unit>().Position;
  116. spd = spd.normalized * speedValue;
  117. this.Speed = spd;
  118. this.Dest = dest;
  119. cancellationToken.Register(() => this.moveTcs = null);
  120. return this.moveTcs.Task;
  121. }
  122. /// <summary>
  123. /// 停止移动Unit,只停止地面正常移动,不停止击飞等移动
  124. /// </summary>
  125. public void Stop()
  126. {
  127. this.speed = Vector3.zero;
  128. this.animatorComponent?.SetFloatValue("Speed", 0);
  129. }
  130. /// <summary>
  131. /// 改变Unit的朝向
  132. /// </summary>
  133. public void Turn2D(Vector3 dir, float turnTime = 0.1f)
  134. {
  135. Vector3 nexpos = this.GetParent<Unit>().GameObject.transform.position + dir;
  136. Turn(nexpos, turnTime);
  137. }
  138. /// <summary>
  139. /// 改变Unit的朝向
  140. /// </summary>
  141. public void Turn(Vector3 target, float turnTime = 0.1f)
  142. {
  143. Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(this.GetParent<Unit>().Position, target);
  144. this.To = quaternion;
  145. this.From = this.GetParent<Unit>().Rotation;
  146. this.t = 0;
  147. this.TurnTime = turnTime;
  148. }
  149. /// <summary>
  150. /// 改变Unit的朝向
  151. /// </summary>
  152. /// <param name="angle">与X轴正方向的夹角</param>
  153. public void Turn(float angle, float turnTime = 0.1f)
  154. {
  155. Quaternion quaternion = PositionHelper.GetAngleToQuaternion(angle);
  156. this.To = quaternion;
  157. this.From = this.GetParent<Unit>().Rotation;
  158. this.t = 0;
  159. this.TurnTime = turnTime;
  160. }
  161. public void Turn(Quaternion quaternion, float turnTime = 0.1f)
  162. {
  163. this.To = quaternion;
  164. this.From = this.GetParent<Unit>().Rotation;
  165. this.t = 0;
  166. this.TurnTime = turnTime;
  167. }
  168. public void TurnImmediately(Quaternion quaternion)
  169. {
  170. this.GetParent<Unit>().Rotation = quaternion;
  171. }
  172. public void TurnImmediately(Vector3 target)
  173. {
  174. Vector3 nowPos = this.GetParent<Unit>().Position;
  175. if (nowPos == target)
  176. {
  177. return;
  178. }
  179. Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(this.GetParent<Unit>().Position, target);
  180. this.GetParent<Unit>().Rotation = quaternion;
  181. }
  182. public void TurnImmediately(float angle)
  183. {
  184. Quaternion quaternion = PositionHelper.GetAngleToQuaternion(angle);
  185. this.GetParent<Unit>().Rotation = quaternion;
  186. }
  187. public override void Dispose()
  188. {
  189. if (this.IsDisposed)
  190. {
  191. return;
  192. }
  193. base.Dispose();
  194. }
  195. }
  196. }