MoveParabolaAction.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /// 中心点位置固定的贝塞尔曲线
  11. /// </summary>
  12. internal class MoveParabolaAction : IAction, IPauseable, ITimeScalable
  13. {
  14. public MoveActionArgs args;
  15. private Tween _tween;
  16. private bool _paused;
  17. float ITimeScalable.TimeScale
  18. {
  19. get => _tween?.timeScale ?? 1;
  20. set
  21. {
  22. if (_tween == null)
  23. {
  24. return;
  25. }
  26. _tween.timeScale = value;
  27. }
  28. }
  29. void IReference.Clear()
  30. {
  31. args = default;
  32. _tween?.Kill();
  33. _tween = null;
  34. _paused = false;
  35. }
  36. public void Start(Transform transform)
  37. {
  38. transform.position = args.from;
  39. var totalTime = args.duration * 0.001f;
  40. var center = Vector3.Lerp(args.from, args.to, 0.5f);
  41. // z轴向相机偏移
  42. center.z -= 8;
  43. _tween = DOTween.To(() => 0, (t) =>
  44. {
  45. var tNormal = t / totalTime;
  46. var pos = args.from.BezierPoint(center, args.to, tNormal);
  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.OutQuad : args.ease);
  55. _tween.onComplete += () =>
  56. {
  57. var callback = args.onComplete;
  58. _tween = null;
  59. args = default;
  60. callback.SafeInvoke();
  61. };
  62. }
  63. void IPauseable.Pause()
  64. {
  65. if (_tween == null || _paused)
  66. {
  67. return;
  68. }
  69. _paused = true;
  70. _tween.Pause();
  71. }
  72. void IPauseable.Resume()
  73. {
  74. if (_paused)
  75. {
  76. _paused = false;
  77. _tween?.Play();
  78. }
  79. }
  80. }
  81. }