diff --git a/Coroutine/ActiveCoroutine.cs b/Coroutine/ActiveCoroutine.cs index d2fde70..88dcecb 100644 --- a/Coroutine/ActiveCoroutine.cs +++ b/Coroutine/ActiveCoroutine.cs @@ -2,19 +2,38 @@ 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; } - public FinishCallback OnFinished; + /// + /// 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; @@ -54,6 +73,10 @@ namespace Coroutine { return this.current.IsWaitingForEvent(); } + /// + /// A delegate method used by . + /// + /// The coroutine that finished public delegate void FinishCallback(ActiveCoroutine coroutine); } diff --git a/Coroutine/Coroutine.csproj b/Coroutine/Coroutine.csproj index 611ce41..f3aba4b 100644 --- a/Coroutine/Coroutine.csproj +++ b/Coroutine/Coroutine.csproj @@ -1,6 +1,7 @@  netstandard2.0 + true diff --git a/Coroutine/CoroutineHandler.cs b/Coroutine/CoroutineHandler.cs index 7acbe67..4e5fa8c 100644 --- a/Coroutine/CoroutineHandler.cs +++ b/Coroutine/CoroutineHandler.cs @@ -3,30 +3,40 @@ using System.Collections.Generic; using System.Linq; namespace Coroutine { + /// + /// This class can be used for static coroutine handling of any kind. + /// Note that it uses an underlying object for management. + /// public static class CoroutineHandler { private static readonly CoroutineHandlerInstance Instance = new CoroutineHandlerInstance(); + /// public static ActiveCoroutine Start(IEnumerable coroutine) { return Instance.Start(coroutine); } + /// public static ActiveCoroutine Start(IEnumerator coroutine) { return Instance.Start(coroutine); } + /// public static void InvokeLater(Wait wait, Action action) { Instance.InvokeLater(wait, action); } + /// public static void Tick(double deltaSeconds) { Instance.Tick(deltaSeconds); } + /// public static void RaiseEvent(Event evt) { Instance.RaiseEvent(evt); } + /// public static IEnumerable GetActiveCoroutines() { return Instance.GetActiveCoroutines(); } diff --git a/Coroutine/CoroutineHandlerInstance.cs b/Coroutine/CoroutineHandlerInstance.cs index a55760c..c5e3256 100644 --- a/Coroutine/CoroutineHandlerInstance.cs +++ b/Coroutine/CoroutineHandlerInstance.cs @@ -3,15 +3,30 @@ using System.Collections.Generic; using System.Linq; namespace Coroutine { + /// + /// An object of this class can be used to start, tick and otherwise manage active s as well as their s. + /// Note that a static implementation of this can be found in . + /// public class CoroutineHandlerInstance { private readonly List tickingCoroutines = new List(); private readonly List eventCoroutines = new List(); + /// + /// Starts the given coroutine, returning a object for management. + /// Note that this calls to get the enumerator. + /// + /// The coroutine to start + /// An active coroutine object representing this coroutine public ActiveCoroutine Start(IEnumerable coroutine) { return this.Start(coroutine.GetEnumerator()); } + /// + /// Starts the given coroutine, returning a object for management. + /// + /// The coroutine to start + /// An active coroutine object representing this coroutine public ActiveCoroutine Start(IEnumerator coroutine) { var inst = new ActiveCoroutine(coroutine); if (inst.MoveNext()) { @@ -24,10 +39,20 @@ namespace Coroutine { return inst; } + /// + /// Causes the given action to be invoked after the given . + /// This is equivalent to a coroutine that waits for the given wait and then executes the given . + /// + /// The wait to wait for + /// The action to execute after waiting public void InvokeLater(Wait wait, Action action) { this.Start(InvokeLaterImpl(wait, action)); } + /// + /// Ticks this coroutine handler, causing all time-based s to be ticked. + /// + /// The amount of seconds that have passed since the last time this method was invoked public void Tick(double deltaSeconds) { for (var i = this.tickingCoroutines.Count - 1; i >= 0; i--) { var coroutine = this.tickingCoroutines[i]; @@ -40,6 +65,10 @@ namespace Coroutine { } } + /// + /// Raises the given event, causing all event-based s to be updated. + /// + /// The event to raise public void RaiseEvent(Event evt) { for (var i = this.eventCoroutines.Count - 1; i >= 0; i--) { var coroutine = this.eventCoroutines[i]; @@ -52,6 +81,10 @@ namespace Coroutine { } } + /// + /// Returns a list of all currently active objects under this handler. + /// + /// All active coroutines public IEnumerable GetActiveCoroutines() { return this.tickingCoroutines.Concat(this.eventCoroutines); } diff --git a/Coroutine/Event.cs b/Coroutine/Event.cs index 3ad04d6..6480254 100644 --- a/Coroutine/Event.cs +++ b/Coroutine/Event.cs @@ -1,7 +1,9 @@ namespace Coroutine { + /// + /// An event is any kind of action that a can listen for. + /// Note that, by default, events don't have a custom implementation. + /// public class Event { - - } } \ No newline at end of file diff --git a/Coroutine/Wait.cs b/Coroutine/Wait.cs index 04defa3..a283e35 100644 --- a/Coroutine/Wait.cs +++ b/Coroutine/Wait.cs @@ -1,21 +1,37 @@ using System; namespace Coroutine { + /// + /// Represents either an amount of time, or an that is being waited for by an . + /// public struct Wait { private readonly Event evt; private double seconds; + /// + /// Creates a new wait that waits for the given . + /// + /// The event to wait for public Wait(Event evt) { this.evt = evt; this.seconds = 0; } + /// + /// Creates a new wait that waits for the given amount of seconds. + /// + /// The amount of seconds to wait for public Wait(double seconds) { this.seconds = seconds; this.evt = null; } + /// + /// Creates a new wait that waits for the given . + /// Note that the exact value may be slightly different, since waits operate in rather than ticks. + /// + /// The time span to wait for public Wait(TimeSpan time) : this(time.TotalSeconds) { }