revert back to using for loops to allow for nested coroutines to work correctly

Closes #6
This commit is contained in:
Ell 2021-03-17 00:38:44 +01:00
parent 448a55081b
commit ffdb5ed8f7
3 changed files with 24 additions and 17 deletions

View file

@ -124,7 +124,7 @@ namespace Coroutine {
/// <inheritdoc /> /// <inheritdoc />
public int CompareTo(ActiveCoroutine other) { public int CompareTo(ActiveCoroutine other) {
return other.Priority.CompareTo(this.Priority); return this.Priority.CompareTo(other.Priority);
} }
} }

View file

@ -67,15 +67,15 @@ namespace Coroutine {
/// </summary> /// </summary>
/// <param name="deltaSeconds">The amount of seconds that have passed since the last time this method was invoked</param> /// <param name="deltaSeconds">The amount of seconds that have passed since the last time this method was invoked</param>
public void Tick(double deltaSeconds) { public void Tick(double deltaSeconds) {
this.tickingCoroutines.RemoveAll(c => { for (var i = this.tickingCoroutines.Count - 1; i >= 0; i--) {
if (c.Tick(deltaSeconds)) { var coroutine = this.tickingCoroutines[i];
return true; if (coroutine.Tick(deltaSeconds)) {
} else if (c.IsWaitingForEvent()) { this.tickingCoroutines.RemoveAt(i);
AddSorted(this.eventCoroutines, c); } else if (coroutine.IsWaitingForEvent()) {
return true; this.tickingCoroutines.RemoveAt(i);
this.eventCoroutines.Add(coroutine);
}
} }
return false;
});
} }
/// <summary> /// <summary>
@ -83,15 +83,15 @@ namespace Coroutine {
/// </summary> /// </summary>
/// <param name="evt">The event to raise</param> /// <param name="evt">The event to raise</param>
public void RaiseEvent(Event evt) { public void RaiseEvent(Event evt) {
this.eventCoroutines.RemoveAll(c => { for (var i = this.eventCoroutines.Count - 1; i >= 0; i--) {
if (c.OnEvent(evt)) { var coroutine = this.eventCoroutines[i];
return true; if (coroutine.OnEvent(evt)) {
} else if (!c.IsWaitingForEvent()) { this.eventCoroutines.RemoveAt(i);
AddSorted(this.tickingCoroutines, c); } else if (!coroutine.IsWaitingForEvent()) {
return true; this.eventCoroutines.RemoveAt(i);
this.tickingCoroutines.Add(coroutine);
}
} }
return false;
});
} }
/// <summary> /// <summary>

View file

@ -38,6 +38,7 @@ namespace Test {
Console.WriteLine("After 1 second " + DateTime.Now); Console.WriteLine("After 1 second " + DateTime.Now);
yield return new Wait(9); yield return new Wait(9);
Console.WriteLine("After 10 seconds " + DateTime.Now); Console.WriteLine("After 10 seconds " + DateTime.Now);
CoroutineHandler.Start(NestedCoroutine());
yield return new Wait(5); yield return new Wait(5);
Console.WriteLine("After 5 more seconds " + DateTime.Now); Console.WriteLine("After 5 more seconds " + DateTime.Now);
yield return new Wait(10); yield return new Wait(10);
@ -66,5 +67,11 @@ namespace Test {
yield break; yield break;
} }
private static IEnumerable<Wait> NestedCoroutine() {
Console.WriteLine("I'm a coroutine that was started from another coroutine!");
yield return new Wait(5);
Console.WriteLine("It's been 5 seconds since a nested coroutine was started, yay!");
}
} }
} }