MoveComponent.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using DG.Tweening;
  2. using FL.Battle.Actions;
  3. using System;
  4. using UnityEngine;
  5. using XGame.Framework;
  6. using XGame.Framework.Components;
  7. using XGame.Framework.Map;
  8. using XGame.Framework.Time;
  9. namespace FL.Battle.Components
  10. {
  11. public interface IMoveContext
  12. {
  13. /// <summary>
  14. /// 所有者的根节点
  15. /// </summary>
  16. Transform OwnerTr { get; }
  17. }
  18. public enum EMoveType
  19. {
  20. /// <summary>
  21. /// 直线运动
  22. /// DoTween.DOMove
  23. /// </summary>
  24. Line = 0,
  25. /// <summary>
  26. /// 贝塞尔曲线轨迹
  27. /// </summary>
  28. Bezier,
  29. /// <summary>
  30. /// 抛物线
  31. /// </summary>
  32. Parabola,
  33. /// <summary>
  34. /// AnimationCurve曲线
  35. /// </summary>
  36. Curve,
  37. }
  38. /// <summary>
  39. /// 移动参数
  40. /// </summary>
  41. public struct MoveArgs
  42. {
  43. public Vector3 target;
  44. public float speed;
  45. /// <summary>
  46. /// 时间缩放值,默认需要传1
  47. /// </summary>
  48. public float timeScale;
  49. public EMoveType moveType;
  50. public Ease ease;
  51. /// <summary>
  52. /// 延迟,单位: 毫秒
  53. /// </summary>
  54. public int delay;
  55. //public Action<Vector3> onUpdate;
  56. public Action onComplete;
  57. /// <summary>
  58. /// 曲线
  59. /// EMoveType.Curve时有效
  60. /// </summary>
  61. public AnimationCurve curve;
  62. /// <summary>
  63. /// 曲线的偏移量
  64. /// EMoveType.Curve时有效
  65. /// </summary>
  66. public Vector3 curveOffset;
  67. }
  68. /// <summary>
  69. /// DoTween.DOLocalMoveY
  70. /// </summary>
  71. public class MoveComponent : Component<IMoveContext>, IParallelTarget, IPauseable, ITimeScalable
  72. {
  73. private IAction _action;
  74. public Vector3 MoveDir { get; private set; }
  75. public bool IsMoving => _action != null;
  76. Vector3 IParallelTarget.Position => Context.OwnerTr.position;
  77. public float TimeScale
  78. {
  79. get
  80. {
  81. if (_action is ITimeScalable scalable)
  82. {
  83. return scalable.TimeScale;
  84. }
  85. return 1;
  86. }
  87. set
  88. {
  89. if (_action is ITimeScalable scalable)
  90. {
  91. scalable.TimeScale = value;
  92. }
  93. }
  94. }
  95. public void MoveTo(MoveArgs args)
  96. {
  97. var position = Context.OwnerTr.position;
  98. var duration = Mathf.RoundToInt(Vector3.Distance(args.target, position) * 1000 / args.speed);
  99. MoveDir = Vector3.Normalize(args.target - position);
  100. void OnCompleted()
  101. {
  102. ObjectPool.Recycle(_action);
  103. _action = null;
  104. MoveDir = Vector3.zero;
  105. args.onComplete.SafeInvoke();
  106. }
  107. var actionArgs = new MoveActionArgs()
  108. {
  109. from = position,
  110. to = args.target,
  111. duration = duration,
  112. delay = args.delay,
  113. ease = args.ease,
  114. //onUpdate = args.onUpdate,
  115. onComplete = OnCompleted
  116. };
  117. if (args.moveType is EMoveType.Line)
  118. {
  119. var move = ObjectPool.Acquire<MoveLineAction>();
  120. _action = move;
  121. move.args = actionArgs;
  122. move.Start(Context.OwnerTr);
  123. }
  124. else if (args.moveType is EMoveType.Bezier)
  125. {
  126. var move = ObjectPool.Acquire<MoveBezierAction>();
  127. _action = move;
  128. move.args = actionArgs;
  129. move.Start(Context.OwnerTr, false);
  130. }
  131. else if (args.moveType is EMoveType.Curve)
  132. {
  133. var move = ObjectPool.Acquire<MoveCurveAction>();
  134. _action = move;
  135. move.args = actionArgs;
  136. move.curve = args.curve;
  137. move.curveOffset = args.curveOffset;
  138. move.Start(Context.OwnerTr);
  139. }
  140. else
  141. {
  142. var move = ObjectPool.Acquire<MoveParabolaAction>();
  143. _action = move;
  144. move.args = actionArgs;
  145. move.Start(Context.OwnerTr);
  146. }
  147. if (args.timeScale != 1)
  148. {
  149. TimeScale = args.timeScale;
  150. }
  151. }
  152. public void Pause()
  153. {
  154. (_action as IPauseable)?.Pause();
  155. }
  156. public void Resume()
  157. {
  158. (_action as IPauseable)?.Resume();
  159. }
  160. public void StopMove()
  161. {
  162. if (_action != null)
  163. {
  164. ObjectPool.Recycle(_action);
  165. _action = null;
  166. MoveDir = Vector3.zero;
  167. }
  168. }
  169. protected override void OnDispose()
  170. {
  171. StopMove();
  172. }
  173. }
  174. }