perf: tween pooling
This commit is contained in:
parent
1b123a3cc0
commit
76ad60fad3
@ -69,6 +69,15 @@ internal class Tween : ITween
|
||||
|
||||
internal void Reset()
|
||||
{
|
||||
OnStarted.Clear();
|
||||
OnPaused.Clear();
|
||||
OnResumed.Clear();
|
||||
OnCancelled.Clear();
|
||||
OnCompleted.Clear();
|
||||
OnEnded.Clear();
|
||||
OnUpdated.Clear();
|
||||
OnDeltaUpdated.Clear();
|
||||
|
||||
_counter = 0f;
|
||||
Progress = 0f;
|
||||
State = TweenState.Idle;
|
||||
|
@ -8,17 +8,36 @@ namespace Syntriax.Engine.Systems.Tween;
|
||||
public class TweenManager : UniverseObject, ITweenManager
|
||||
{
|
||||
private CoroutineManager coroutineManager = null!;
|
||||
private readonly Queue<Tween> queue = new();
|
||||
|
||||
private readonly Dictionary<ITween, IEnumerator> runningCoroutines = [];
|
||||
|
||||
public ITween StartTween(float duration, ITweenManager.TweenSetCallback? setCallback = null)
|
||||
{
|
||||
Tween tween = new(duration);
|
||||
Tween tween = Get(duration);
|
||||
tween.OnUpdated.AddListener(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.Reset();
|
||||
queue.Enqueue(tween);
|
||||
}
|
||||
|
||||
private IEnumerator RunTween(Tween tween)
|
||||
{
|
||||
tween.State = TweenState.Playing;
|
||||
@ -40,6 +59,7 @@ public class TweenManager : UniverseObject, ITweenManager
|
||||
}
|
||||
|
||||
runningCoroutines.Remove(tween);
|
||||
Return(tween);
|
||||
}
|
||||
|
||||
public void CancelTween(ITween tween)
|
||||
|
Loading…
x
Reference in New Issue
Block a user