UIMoveBezierAction.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using DG.Tweening;
  2. using System;
  3. using UnityEngine;
  4. using XGame.Framework.FGUI;
  5. using XGame.Framework.Interfaces;
  6. using XGame.Framework.Time;
  7. namespace FL.FGUI.Actions
  8. {
  9. /// <summary>
  10. /// MoveLineAction的参数
  11. /// </summary>
  12. public struct UIMoveActionArgs
  13. {
  14. public Vector3 from;
  15. public Vector3 to;
  16. /// <summary>
  17. /// 单位:毫秒
  18. /// </summary>
  19. public int duration;
  20. /// <summary>
  21. /// 延迟,单位: 毫秒
  22. /// </summary>
  23. public int delay;
  24. public float offset;
  25. public Ease ease;
  26. public Action<Vector3> onUpdate;
  27. public Action onComplete;
  28. }
  29. public class UIMoveBezierAction : IAction, IPauseable, ITimeScalable
  30. {
  31. public UIMoveActionArgs args;
  32. private Tween _tween;
  33. private bool _paused;
  34. float ITimeScalable.TimeScale
  35. {
  36. get => _tween?.timeScale ?? 1;
  37. set
  38. {
  39. if (_tween == null)
  40. {
  41. return;
  42. }
  43. _tween.timeScale = value;
  44. }
  45. }
  46. void IReference.Clear()
  47. {
  48. _tween?.Kill();
  49. _tween = null;
  50. args = default;
  51. _paused = false;
  52. }
  53. public void Start(FguiViewModel viewModel)
  54. {
  55. // 贝塞尔曲线运动
  56. viewModel.Pos = args.from;
  57. var timeScale = UnityEngine.Random.Range(0.2f, 0.8f);
  58. var linePoint = Vector3.Lerp(args.from, args.to, timeScale);
  59. var dir = Vector3.Cross(args.to - args.from, Vector3.forward).normalized; //运动方向的法线
  60. var offset = args.offset > 0 ? args.offset : 300;
  61. var disOffset = UnityEngine.Random.Range(-offset, offset);
  62. var center = linePoint + dir * disOffset;
  63. var totalTime = args.duration * 0.001f;
  64. var timeTween = DOTween.To(() => 0, (t) =>
  65. {
  66. var tNormal = t / totalTime;
  67. var pos = args.from.BezierPoint(center, args.to, tNormal);
  68. viewModel.Pos = pos;
  69. args.onUpdate.SafeInvoke(pos);
  70. }, totalTime, totalTime);
  71. if (args.delay > 0)
  72. timeTween.SetDelay(args.delay * 0.001f);
  73. timeTween.SetEase(args.ease == Ease.Unset ? Ease.OutQuad : args.ease);
  74. _tween = timeTween;
  75. timeTween.onComplete = () =>
  76. {
  77. var callback = args.onComplete;
  78. _tween = null;
  79. args = default;
  80. callback?.Invoke();
  81. };
  82. }
  83. void IAction.Start(FguiViewModel viewModel)
  84. {
  85. Start(viewModel);
  86. }
  87. void IPauseable.Pause()
  88. {
  89. if (_tween == null || _paused)
  90. {
  91. return;
  92. }
  93. _paused = true;
  94. _tween.Pause();
  95. }
  96. void IPauseable.Resume()
  97. {
  98. if (_paused)
  99. {
  100. _paused = false;
  101. _tween?.Play();
  102. }
  103. }
  104. }
  105. }