MoveLineAction.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /// MoveLineAction的参数
  10. /// </summary>
  11. public struct MoveActionArgs
  12. {
  13. public Vector3 from;
  14. public Vector3 to;
  15. /// <summary>
  16. /// 单位:毫秒
  17. /// </summary>
  18. public int duration;
  19. /// <summary>
  20. /// 延迟,单位: 毫秒
  21. /// </summary>
  22. public int delay;
  23. public Ease ease;
  24. public Action<Vector3> onUpdate;
  25. public Action onComplete;
  26. }
  27. /// <summary>
  28. /// 直线移动
  29. /// Dotween.DOMove
  30. /// </summary>
  31. public class MoveLineAction : IAction, IPauseable, ITimeScalable
  32. {
  33. public MoveActionArgs args;
  34. private Tween _tween;
  35. private bool _paused;
  36. float ITimeScalable.TimeScale
  37. {
  38. get => _tween?.timeScale ?? 1;
  39. set
  40. {
  41. if (_tween == null)
  42. {
  43. return;
  44. }
  45. _tween.timeScale = value;
  46. }
  47. }
  48. void IReference.Clear()
  49. {
  50. args = default;
  51. _tween?.Kill();
  52. _tween = null;
  53. _paused = false;
  54. }
  55. public void Start(Transform transform)
  56. {
  57. transform.position = args.from;
  58. _tween = transform.DOMove(args.to, args.duration * 0.001f);
  59. if (args.delay > 0)
  60. {
  61. _tween.SetDelay(args.delay * 0.001f);
  62. }
  63. _tween.SetEase(args.ease == Ease.Unset ? Ease.Linear : args.ease);
  64. if (args.onUpdate != null)
  65. {
  66. _tween.onUpdate += () =>
  67. {
  68. args.onUpdate.SafeInvoke(transform.position);
  69. };
  70. }
  71. _tween.onComplete += () =>
  72. {
  73. var callback = args.onComplete;
  74. _tween = null;
  75. args = default;
  76. callback.SafeInvoke();
  77. };
  78. }
  79. void IPauseable.Pause()
  80. {
  81. if (_tween == null || _paused)
  82. {
  83. return;
  84. }
  85. _paused = true;
  86. _tween.Pause();
  87. }
  88. void IPauseable.Resume()
  89. {
  90. if (_paused)
  91. {
  92. _paused = false;
  93. _tween?.Play();
  94. }
  95. }
  96. }
  97. }