2024-10-26 13:46:22 +03:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using Syntriax.Engine.Core.Abstract;
|
|
|
|
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
|
|
|
|
public class CoroutineManager : HierarchyObjectBase
|
|
|
|
{
|
2024-10-29 22:45:08 +03:00
|
|
|
private readonly List<IEnumerator> enumerators = [];
|
2024-10-26 13:46:22 +03:00
|
|
|
|
|
|
|
public IEnumerator CreateCoroutine(IEnumerator enumerator)
|
|
|
|
{
|
|
|
|
enumerators.Add(enumerator);
|
|
|
|
return enumerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void StopCoroutine(IEnumerator enumerator)
|
|
|
|
{
|
|
|
|
enumerators.Remove(enumerator);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnEnteringHierarchy(IGameManager gameManager)
|
|
|
|
{
|
|
|
|
gameManager.OnUpdate += OnUpdate;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnExitingHierarchy(IGameManager gameManager)
|
|
|
|
{
|
|
|
|
gameManager.OnUpdate -= OnUpdate;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnUpdate(IGameManager sender, EngineTime time)
|
|
|
|
{
|
|
|
|
for (int i = enumerators.Count - 1; i >= 0; i--)
|
2024-11-02 22:27:04 +03:00
|
|
|
{
|
|
|
|
if (enumerators[i].Current is ICoroutineYield coroutineYield && coroutineYield.Yield())
|
|
|
|
continue;
|
|
|
|
|
2024-10-26 13:46:22 +03:00
|
|
|
if (!enumerators[i].MoveNext())
|
|
|
|
enumerators.RemoveAt(i);
|
2024-11-02 22:27:04 +03:00
|
|
|
}
|
2024-10-26 13:46:22 +03:00
|
|
|
}
|
|
|
|
}
|