From cf1b3c2db041a3aba6bd3a8685a03f1862566201 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 20 Mar 2021 02:00:46 +0100 Subject: [PATCH] added a convenience method that accepts a TimeSpan for Tick --- Coroutine/CoroutineHandler.cs | 6 +++++- Coroutine/CoroutineHandlerInstance.cs | 9 +++++++++ Example/Example.cs | 2 +- README.md | 7 +++++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Coroutine/CoroutineHandler.cs b/Coroutine/CoroutineHandler.cs index a5673f1..2e1d6b6 100644 --- a/Coroutine/CoroutineHandler.cs +++ b/Coroutine/CoroutineHandler.cs @@ -30,10 +30,14 @@ namespace Coroutine { return Instance.InvokeLater(wait, action, name, priority); } - /// + /// public static void Tick(double deltaSeconds) { Instance.Tick(deltaSeconds); } + /// + public static void Tick(TimeSpan delta) { + Instance.Tick(delta); + } /// public static void RaiseEvent(Event evt) { diff --git a/Coroutine/CoroutineHandlerInstance.cs b/Coroutine/CoroutineHandlerInstance.cs index 1392370..8dc613f 100644 --- a/Coroutine/CoroutineHandlerInstance.cs +++ b/Coroutine/CoroutineHandlerInstance.cs @@ -82,6 +82,15 @@ namespace Coroutine { }); } + /// + /// Ticks this coroutine handler, causing all time-based s to be ticked. + /// This is a convenience method that calls , but accepts a instead of an amount of seconds. + /// + /// The time that has passed since the last time this method was invoked + public void Tick(TimeSpan delta) { + this.Tick(delta.TotalSeconds); + } + /// /// Raises the given event, causing all event-based s to be updated. /// diff --git a/Example/Example.cs b/Example/Example.cs index a77e1a7..d1c86ba 100644 --- a/Example/Example.cs +++ b/Example/Example.cs @@ -26,7 +26,7 @@ namespace Example { var lastTime = DateTime.Now; while (true) { var currTime = DateTime.Now; - CoroutineHandler.Tick((currTime - lastTime).TotalSeconds); + CoroutineHandler.Tick(currTime - lastTime); lastTime = currTime; Thread.Sleep(1); } diff --git a/README.md b/README.md index 6493425..d2b6d34 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,12 @@ Additionally, Coroutine provides the following features: # How to Use ## 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 var lastTime = DateTime.Now; while (true) { var currTime = DateTime.Now; - CoroutineHandler.Tick((currTime - lastTime).TotalSeconds); + CoroutineHandler.Tick(currTime - lastTime); lastTime = currTime; Thread.Sleep(1); } @@ -59,11 +59,14 @@ private static IEnumerator WaitForTestEvent() { 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`: ```cs 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 For additional examples, take a look at the [Example class](https://github.com/Ellpeck/Coroutine/blob/master/Example/Example.cs). \ No newline at end of file