diff --git a/Engine.Systems/Tween/TweenManager.cs b/Engine.Systems/Tween/TweenManager.cs index 0a38228..7bd052d 100644 --- a/Engine.Systems/Tween/TweenManager.cs +++ b/Engine.Systems/Tween/TweenManager.cs @@ -8,17 +8,36 @@ namespace Syntriax.Engine.Systems.Tween; public class TweenManager : UniverseObject, ITweenManager { private CoroutineManager coroutineManager = null!; + private readonly Queue queue = new(); private readonly Dictionary runningCoroutines = []; public ITween StartTween(float duration, ITweenManager.TweenSetCallback? setCallback = null) { - Tween tween = new(duration); + Tween tween = Get(duration); tween.OnUpdated += tween => setCallback?.Invoke(tween.Value); runningCoroutines.Add(tween, coroutineManager.StartCoroutine(RunTween(tween))); return tween; } + private Tween Get(float duration) + { + if (!queue.TryDequeue(out Tween? result)) + result = new(duration); + + result.Duration = duration; + return result; + } + + private void Return(Tween tween) + { + if (queue.Contains(tween)) + return; + + tween.Wipe(); + queue.Enqueue(tween); + } + private IEnumerator RunTween(Tween tween) { tween.State = TweenState.Playing;