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

View file

@ -38,6 +38,7 @@ namespace Test {
Console.WriteLine("After 1 second " + DateTime.Now);
yield return new Wait(9);
Console.WriteLine("After 10 seconds " + DateTime.Now);
CoroutineHandler.Start(NestedCoroutine());
yield return new Wait(5);
Console.WriteLine("After 5 more seconds " + DateTime.Now);
yield return new Wait(10);
@ -66,5 +67,11 @@ namespace Test {
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!");
}
}
}