feat: ICoroutineYield for Delaying Coroutines
This commit is contained in:
parent
eb445604e8
commit
cb60c71184
8
Engine.Core/Abstract/ICoroutineYield.cs
Normal file
8
Engine.Core/Abstract/ICoroutineYield.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
namespace Syntriax.Engine.Core.Abstract;
|
||||||
|
|
||||||
|
public interface ICoroutineYield
|
||||||
|
{
|
||||||
|
bool Yield();
|
||||||
|
}
|
@ -33,7 +33,12 @@ public class CoroutineManager : HierarchyObjectBase
|
|||||||
private void OnUpdate(IGameManager sender, EngineTime time)
|
private void OnUpdate(IGameManager sender, EngineTime time)
|
||||||
{
|
{
|
||||||
for (int i = enumerators.Count - 1; i >= 0; i--)
|
for (int i = enumerators.Count - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if (enumerators[i].Current is ICoroutineYield coroutineYield && coroutineYield.Yield())
|
||||||
|
continue;
|
||||||
|
|
||||||
if (!enumerators[i].MoveNext())
|
if (!enumerators[i].MoveNext())
|
||||||
enumerators.RemoveAt(i);
|
enumerators.RemoveAt(i);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
12
Engine.Core/CoroutineYield.cs
Normal file
12
Engine.Core/CoroutineYield.cs
Normal 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();
|
||||||
|
}
|
@ -36,9 +36,14 @@ public class PhysicsCoroutineManager : HierarchyObjectBase
|
|||||||
private void OnPhysicsStep(IPhysicsEngine2D sender, float stepDeltaTime)
|
private void OnPhysicsStep(IPhysicsEngine2D sender, float stepDeltaTime)
|
||||||
{
|
{
|
||||||
for (int i = enumerators.Count - 1; i >= 0; i--)
|
for (int i = enumerators.Count - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if (enumerators[i].Current is ICoroutineYield coroutineYield && coroutineYield.Yield())
|
||||||
|
continue;
|
||||||
|
|
||||||
if (!enumerators[i].MoveNext())
|
if (!enumerators[i].MoveNext())
|
||||||
enumerators.RemoveAt(i);
|
enumerators.RemoveAt(i);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected override void OnExitingHierarchy(IGameManager gameManager)
|
protected override void OnExitingHierarchy(IGameManager gameManager)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user