MoveComponent.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace Model
  4. {
  5. [ObjectSystem]
  6. public class MoveComponentAwakeSystem : AwakeSystem<MoveComponent>
  7. {
  8. public override void Awake(MoveComponent self)
  9. {
  10. self.Awake();
  11. }
  12. }
  13. [ObjectSystem]
  14. public class MoveComponentUpdateSystem : UpdateSystem<MoveComponent>
  15. {
  16. public override void Update(MoveComponent self)
  17. {
  18. self.Update();
  19. }
  20. }
  21. public class Speed
  22. {
  23. public long Id;
  24. public Vector3 Value;
  25. public Speed()
  26. {
  27. }
  28. public Speed(long id)
  29. {
  30. this.Id = id;
  31. }
  32. }
  33. public class MoveComponent : Component
  34. {
  35. private AnimatorComponent animatorComponent;
  36. public long mainSpeed;
  37. public Dictionary<long, Speed> speeds = new Dictionary<long, Speed>();
  38. // turn
  39. public Quaternion To;
  40. public Quaternion From;
  41. public float t = float.MaxValue;
  42. public float TurnTime = 0.1f;
  43. public bool IsArrived { get; private set; } = true;
  44. public bool hasDest;
  45. public Vector3 Dest;
  46. public Vector3 MainSpeed
  47. {
  48. get
  49. {
  50. Speed speed;
  51. if (!this.speeds.TryGetValue(this.mainSpeed, out speed))
  52. {
  53. speed = new Speed(this.mainSpeed);
  54. this.speeds.Add(speed.Id, speed);
  55. }
  56. return speed.Value;
  57. }
  58. set
  59. {
  60. Speed speed;
  61. if (!this.speeds.TryGetValue(this.mainSpeed, out speed))
  62. {
  63. speed = new Speed(this.mainSpeed);
  64. this.speeds.Add(speed.Id, speed);
  65. }
  66. speed.Value = value;
  67. animatorComponent?.SetFloatValue("Speed", speed.Value.magnitude);
  68. }
  69. }
  70. public Vector3 Speed
  71. {
  72. get
  73. {
  74. Vector3 speed = new Vector3();
  75. foreach (Speed sp in this.speeds.Values)
  76. {
  77. speed += sp.Value;
  78. }
  79. return speed;
  80. }
  81. }
  82. public void Awake()
  83. {
  84. this.mainSpeed = this.AddSpeed(new Vector3());
  85. this.animatorComponent = this.Entity.GetComponent<AnimatorComponent>();
  86. }
  87. public void Update()
  88. {
  89. UpdateTurn();
  90. if (this.IsArrived)
  91. {
  92. return;
  93. }
  94. if (this.Speed == Vector3.zero)
  95. {
  96. return;
  97. }
  98. Unit unit = this.GetParent<Unit>();
  99. Vector3 moveVector3 = this.Speed * Time.deltaTime;
  100. if (this.hasDest)
  101. {
  102. float dist = (this.Dest - unit.Position).magnitude;
  103. if (moveVector3.magnitude >= dist || dist < 0.1f)
  104. {
  105. unit.Position = this.Dest;
  106. this.IsArrived = true;
  107. return;
  108. }
  109. }
  110. unit.Position = unit.Position + moveVector3;
  111. }
  112. private void UpdateTurn()
  113. {
  114. //Log.Debug($"update turn: {this.t} {this.TurnTime}");
  115. if (this.t > this.TurnTime)
  116. {
  117. return;
  118. }
  119. this.t += Time.deltaTime;
  120. Quaternion v = Quaternion.Slerp(this.From, this.To, this.t / this.TurnTime);
  121. this.GetParent<Unit>().Rotation = v;
  122. }
  123. public void MoveToDest(Vector3 dest, float speedValue)
  124. {
  125. if ((dest - this.GetParent<Unit>().Position).magnitude < 0.1f)
  126. {
  127. this.IsArrived = true;
  128. return;
  129. }
  130. this.IsArrived = false;
  131. this.hasDest = true;
  132. Vector3 speed = dest - this.GetParent<Unit>().Position;
  133. speed = speed.normalized * speedValue;
  134. this.MainSpeed = speed;
  135. this.Dest = dest;
  136. }
  137. public void MoveToDir(Vector3 dir)
  138. {
  139. this.IsArrived = false;
  140. this.hasDest = false;
  141. this.MainSpeed = dir;
  142. }
  143. public long AddSpeed(Vector3 spd)
  144. {
  145. Speed speed = new Speed() { Value = spd };
  146. this.speeds.Add(speed.Id, speed);
  147. return speed.Id;
  148. }
  149. public Speed GetSpeed(long id)
  150. {
  151. Speed speed;
  152. this.speeds.TryGetValue(id, out speed);
  153. return speed;
  154. }
  155. public void RemoveSpeed(long id)
  156. {
  157. Speed speed;
  158. if (!this.speeds.TryGetValue(id, out speed))
  159. {
  160. return;
  161. }
  162. this.speeds.Remove(id);
  163. }
  164. /// <summary>
  165. /// 停止移动Unit,只停止地面正常移动,不停止击飞等移动
  166. /// </summary>
  167. public void Stop()
  168. {
  169. this.speeds.Clear();
  170. this.animatorComponent?.SetFloatValue("Speed", 0);
  171. }
  172. /// <summary>
  173. /// 改变Unit的朝向
  174. /// </summary>
  175. public void Turn2D(Vector3 dir, float turnTime = 0.1f)
  176. {
  177. Vector3 nexpos = this.GetParent<Unit>().GameObject.transform.position + dir;
  178. Turn(nexpos, turnTime);
  179. }
  180. /// <summary>
  181. /// 改变Unit的朝向
  182. /// </summary>
  183. public void Turn(Vector3 target, float turnTime = 0.1f)
  184. {
  185. Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(this.GetParent<Unit>().Position, target);
  186. this.To = quaternion;
  187. this.From = this.GetParent<Unit>().Rotation;
  188. this.t = 0;
  189. this.TurnTime = turnTime;
  190. }
  191. /// <summary>
  192. /// 改变Unit的朝向
  193. /// </summary>
  194. /// <param name="angle">与X轴正方向的夹角</param>
  195. public void Turn(float angle, float turnTime = 0.1f)
  196. {
  197. Quaternion quaternion = PositionHelper.GetAngleToQuaternion(angle);
  198. this.To = quaternion;
  199. this.From = this.GetParent<Unit>().Rotation;
  200. this.t = 0;
  201. this.TurnTime = turnTime;
  202. }
  203. public void Turn(Quaternion quaternion, float turnTime = 0.1f)
  204. {
  205. this.To = quaternion;
  206. this.From = this.GetParent<Unit>().Rotation;
  207. this.t = 0;
  208. this.TurnTime = turnTime;
  209. }
  210. public void TurnImmediately(Quaternion quaternion)
  211. {
  212. this.GetParent<Unit>().Rotation = quaternion;
  213. }
  214. public void TurnImmediately(Vector3 target)
  215. {
  216. Vector3 nowPos = this.GetParent<Unit>().Position;
  217. if (nowPos == target)
  218. {
  219. return;
  220. }
  221. Quaternion quaternion = PositionHelper.GetVector3ToQuaternion(this.GetParent<Unit>().Position, target);
  222. this.GetParent<Unit>().Rotation = quaternion;
  223. }
  224. public void TurnImmediately(float angle)
  225. {
  226. Quaternion quaternion = PositionHelper.GetAngleToQuaternion(angle);
  227. this.GetParent<Unit>().Rotation = quaternion;
  228. }
  229. public override void Dispose()
  230. {
  231. if (this.IsDisposed)
  232. {
  233. return;
  234. }
  235. base.Dispose();
  236. }
  237. }
  238. }