All delegate events are refactored to use the Event<TSender> and Event<TSender, TArgument> for performance issues regarding delegate events creating garbage, also this gives us better control on event invocation since C# Delegates did also create unnecessary garbage during Delegate.DynamicInvoke
43 lines
1.1 KiB
C#
43 lines
1.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
public class CoroutineManager : UniverseObject
|
|
{
|
|
private readonly List<IEnumerator> enumerators = [];
|
|
|
|
public IEnumerator StartCoroutine(IEnumerator enumerator)
|
|
{
|
|
enumerators.Add(enumerator);
|
|
return enumerator;
|
|
}
|
|
|
|
public void StopCoroutine(IEnumerator enumerator)
|
|
{
|
|
enumerators.Remove(enumerator);
|
|
}
|
|
|
|
protected override void OnEnteringUniverse(IUniverse universe)
|
|
{
|
|
universe.OnUpdate.AddListener(OnUpdate);
|
|
}
|
|
|
|
protected override void OnExitingUniverse(IUniverse universe)
|
|
{
|
|
universe.OnUpdate.RemoveListener(OnUpdate);
|
|
}
|
|
|
|
private void OnUpdate(IUniverse sender, IUniverse.UpdateArguments args)
|
|
{
|
|
for (int i = enumerators.Count - 1; i >= 0; i--)
|
|
{
|
|
if (enumerators[i].Current is ICoroutineYield coroutineYield && coroutineYield.Yield())
|
|
continue;
|
|
|
|
if (!enumerators[i].MoveNext())
|
|
enumerators.RemoveAt(i);
|
|
}
|
|
}
|
|
}
|