feat: ICoroutineYield for Delaying Coroutines

This commit is contained in:
Syntriax 2024-11-02 22:27:04 +03:00
parent eb445604e8
commit cb60c71184
4 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,8 @@
using System.Collections;
namespace Syntriax.Engine.Core.Abstract;
public interface ICoroutineYield
{
bool Yield();
}

View File

@ -33,7 +33,12 @@ public class CoroutineManager : HierarchyObjectBase
private void OnUpdate(IGameManager sender, EngineTime time)
{
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);
}
}
}

View File

@ -0,0 +1,12 @@
using System;
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core;
public class CoroutineYield(Func<bool> condition) : ICoroutineYield
{
private readonly Func<bool> condition = condition;
public bool Yield() => condition.Invoke();
}

View File

@ -36,8 +36,13 @@ public class PhysicsCoroutineManager : HierarchyObjectBase
private void OnPhysicsStep(IPhysicsEngine2D sender, float stepDeltaTime)
{
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);
}
}
protected override void OnExitingHierarchy(IGameManager gameManager)