using System; using System.Collections.Generic; namespace Coroutine { /// /// A reference to a currently running coroutine. /// This is returned by . /// public class ActiveCoroutine { private readonly IEnumerator enumerator; private Wait current; /// /// This property stores whether or not this active coroutine is finished. /// A coroutine is finished if all of its waits have passed, or if it . /// public bool IsFinished { get; private set; } /// /// This property stores whether or not this active coroutine was cancelled using . /// public bool WasCanceled { get; private set; } /// /// An event that gets fired when this active coroutine finishes or gets cancelled. /// When this event is called, is always true. /// public event FinishCallback OnFinished; internal ActiveCoroutine(IEnumerator enumerator) { this.enumerator = enumerator; } /// /// Cancels this coroutine, causing all subsequent s and any code in between to be skipped. /// /// Whether the cancellation was successful, or this coroutine was already cancelled or finished public bool Cancel() { if (this.IsFinished || this.WasCanceled) return false; this.WasCanceled = true; this.IsFinished = true; this.OnFinished?.Invoke(this); return true; } internal bool Tick(double deltaSeconds) { if (!this.WasCanceled) { if (this.current.Tick(deltaSeconds)) this.MoveNext(); } return this.IsFinished; } internal bool OnEvent(Event evt) { if (!this.WasCanceled) { if (this.current.OnEvent(evt)) this.MoveNext(); } return this.IsFinished; } internal bool MoveNext() { if (!this.enumerator.MoveNext()) { this.IsFinished = true; this.OnFinished?.Invoke(this); return false; } this.current = this.enumerator.Current; return true; } internal bool IsWaitingForEvent() { return this.current.IsWaitingForEvent(); } /// /// A delegate method used by . /// /// The coroutine that finished public delegate void FinishCallback(ActiveCoroutine coroutine); } }