MoveCurveAction.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using DG.Tweening;
  2. using System;
  3. using UnityEngine;
  4. using XGame.Framework.Interfaces;
  5. using XGame.Framework.Time;
  6. namespace FL.Battle.Actions
  7. {
  8. /// <summary>
  9. /// 曲线移动
  10. /// </summary>
  11. internal class MoveCurveAction : IAction, IPauseable, ITimeScalable
  12. {
  13. public MoveActionArgs args;
  14. public AnimationCurve curve;
  15. public Vector3 curveOffset;
  16. private Tween _tween;
  17. private bool _paused;
  18. float ITimeScalable.TimeScale
  19. {
  20. get => _tween?.timeScale ?? 1;
  21. set
  22. {
  23. if (_tween == null)
  24. {
  25. return;
  26. }
  27. _tween.timeScale = value;
  28. }
  29. }
  30. void IReference.Clear()
  31. {
  32. args = default;
  33. curve = null;
  34. curveOffset = Vector3.zero;
  35. _tween?.Kill();
  36. _tween = null;
  37. _paused = false;
  38. }
  39. public void Start(Transform transform)
  40. {
  41. transform.position = args.from;
  42. var totalTime = args.duration * 0.001f;
  43. _tween = DOTween.To(() => 0, (t) =>
  44. {
  45. var tNormal = t / totalTime;
  46. var pos = Vector3.Lerp(args.from, args.to, tNormal) + curve.Evaluate(tNormal) * curveOffset;
  47. transform.position = pos;
  48. args.onUpdate.SafeInvoke(pos);
  49. }, totalTime, totalTime);
  50. if (args.delay > 0)
  51. {
  52. _tween.SetDelay(args.delay * 0.001f);
  53. }
  54. _tween.SetEase(args.ease == Ease.Unset ? Ease.Linear : args.ease);
  55. _tween.onComplete += () =>
  56. {
  57. var callback = args.onComplete;
  58. _tween = null;
  59. args = default;
  60. curve = null;
  61. curveOffset = Vector3.zero;
  62. callback.SafeInvoke();
  63. };
  64. }
  65. void IPauseable.Pause()
  66. {
  67. if (_tween == null || _paused)
  68. {
  69. return;
  70. }
  71. _paused = true;
  72. _tween.Pause();
  73. }
  74. void IPauseable.Resume()
  75. {
  76. if (_paused)
  77. {
  78. _paused = false;
  79. _tween?.Play();
  80. }
  81. }
  82. }
  83. }