diff --git a/Coroutine/ActiveCoroutine.cs b/Coroutine/ActiveCoroutine.cs index 5604593..17a95a9 100644 --- a/Coroutine/ActiveCoroutine.cs +++ b/Coroutine/ActiveCoroutine.cs @@ -124,7 +124,7 @@ namespace Coroutine { /// public int CompareTo(ActiveCoroutine other) { - return other.Priority.CompareTo(this.Priority); + return this.Priority.CompareTo(other.Priority); } } diff --git a/Coroutine/CoroutineHandlerInstance.cs b/Coroutine/CoroutineHandlerInstance.cs index 8b8fe46..4aa2f24 100644 --- a/Coroutine/CoroutineHandlerInstance.cs +++ b/Coroutine/CoroutineHandlerInstance.cs @@ -67,15 +67,15 @@ namespace Coroutine { /// /// The amount of seconds that have passed since the last time this method was invoked 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; - }); + } } /// @@ -83,15 +83,15 @@ namespace Coroutine { /// /// The event to raise 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; - }); + } } /// diff --git a/Test/Example.cs b/Test/Example.cs index cd774f0..a1dc1c2 100644 --- a/Test/Example.cs +++ b/Test/Example.cs @@ -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 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!"); + } + } } \ No newline at end of file