added a convenience method that accepts a TimeSpan for Tick

This commit is contained in:
Ell 2021-03-20 02:00:46 +01:00
parent 9f4d43d730
commit cf1b3c2db0
4 changed files with 20 additions and 4 deletions

View file

@ -30,10 +30,14 @@ namespace Coroutine {
return Instance.InvokeLater(wait, action, name, priority); return Instance.InvokeLater(wait, action, name, priority);
} }
/// <inheritdoc cref="CoroutineHandlerInstance.Tick"/> /// <inheritdoc cref="CoroutineHandlerInstance.Tick(double)"/>
public static void Tick(double deltaSeconds) { public static void Tick(double deltaSeconds) {
Instance.Tick(deltaSeconds); Instance.Tick(deltaSeconds);
} }
/// <inheritdoc cref="CoroutineHandlerInstance.Tick(TimeSpan)"/>
public static void Tick(TimeSpan delta) {
Instance.Tick(delta);
}
/// <inheritdoc cref="CoroutineHandlerInstance.RaiseEvent"/> /// <inheritdoc cref="CoroutineHandlerInstance.RaiseEvent"/>
public static void RaiseEvent(Event evt) { public static void RaiseEvent(Event evt) {

View file

@ -82,6 +82,15 @@ namespace Coroutine {
}); });
} }
/// <summary>
/// Ticks this coroutine handler, causing all time-based <see cref="Wait"/>s to be ticked.
/// This is a convenience method that calls <see cref="Tick(double)"/>, but accepts a <see cref="TimeSpan"/> instead of an amount of seconds.
/// </summary>
/// <param name="delta">The time that has passed since the last time this method was invoked</param>
public void Tick(TimeSpan delta) {
this.Tick(delta.TotalSeconds);
}
/// <summary> /// <summary>
/// Raises the given event, causing all event-based <see cref="Wait"/>s to be updated. /// Raises the given event, causing all event-based <see cref="Wait"/>s to be updated.
/// </summary> /// </summary>

View file

@ -26,7 +26,7 @@ namespace Example {
var lastTime = DateTime.Now; var lastTime = DateTime.Now;
while (true) { while (true) {
var currTime = DateTime.Now; var currTime = DateTime.Now;
CoroutineHandler.Tick((currTime - lastTime).TotalSeconds); CoroutineHandler.Tick(currTime - lastTime);
lastTime = currTime; lastTime = currTime;
Thread.Sleep(1); Thread.Sleep(1);
} }

View file

@ -15,12 +15,12 @@ Additionally, Coroutine provides the following features:
# How to Use # How to Use
## Setting up the CoroutineHandler ## Setting up the CoroutineHandler
The `CoroutineHandler` is the place where coroutines get executed. For this to occur, the `Tick` method needs to be called continuously. The `Tick` method takes a single parameter which represents the amount of seconds since the last time it was called. It can either be called in your application's existing update loop or as follows. The `CoroutineHandler` is the place where coroutines get executed. For this to occur, the `Tick` method needs to be called continuously. The `Tick` method takes a single parameter which represents the amount of time since the last time it was called. It can either be called in your application's existing update loop or as follows.
```cs ```cs
var lastTime = DateTime.Now; var lastTime = DateTime.Now;
while (true) { while (true) {
var currTime = DateTime.Now; var currTime = DateTime.Now;
CoroutineHandler.Tick((currTime - lastTime).TotalSeconds); CoroutineHandler.Tick(currTime - lastTime);
lastTime = currTime; lastTime = currTime;
Thread.Sleep(1); Thread.Sleep(1);
} }
@ -59,11 +59,14 @@ private static IEnumerator<Wait> WaitForTestEvent() {
Console.WriteLine("Test event received"); Console.WriteLine("Test event received");
} }
``` ```
Of course, having time-based waits and event-based waits in the same coroutine is also supported.
To actually cause the event to be raised, causing all currently waiting coroutines to be continued, simply call `RaiseEvent`: To actually cause the event to be raised, causing all currently waiting coroutines to be continued, simply call `RaiseEvent`:
```cs ```cs
CoroutineHandler.RaiseEvent(TestEvent); CoroutineHandler.RaiseEvent(TestEvent);
``` ```
Note that, since `Tick` is an important lifecycle method, it has to be [called continuously](#Setting-up-the-CoroutineHandler) even if only event-based coroutines are used.
## Additional Examples ## Additional Examples
For additional examples, take a look at the [Example class](https://github.com/Ellpeck/Coroutine/blob/master/Example/Example.cs). For additional examples, take a look at the [Example class](https://github.com/Ellpeck/Coroutine/blob/master/Example/Example.cs).