From 2025d6762a07bd639d7a316a6ea4fe0c52eb62d4 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 20 Nov 2019 11:30:28 +0100 Subject: [PATCH] made waits structs and added invokelater --- Coroutine/Coroutine.cs | 6 +++--- Coroutine/CoroutineHandler.cs | 11 ++++++++++- Coroutine/IWait.cs | 18 ++++++++++++++++++ Coroutine/Wait.cs | 24 ------------------------ Coroutine/WaitEvent.cs | 12 +++++++++--- Coroutine/WaitSeconds.cs | 12 +++++++++--- Test/Example.cs | 33 ++++++++++++++++----------------- 7 files changed, 65 insertions(+), 51 deletions(-) create mode 100644 Coroutine/IWait.cs delete mode 100644 Coroutine/Wait.cs diff --git a/Coroutine/Coroutine.cs b/Coroutine/Coroutine.cs index ab276de..369950b 100644 --- a/Coroutine/Coroutine.cs +++ b/Coroutine/Coroutine.cs @@ -1,11 +1,11 @@ using System.Collections.Generic; namespace Coroutine { - internal class Coroutine { + internal struct Coroutine { - private readonly IEnumerator enumerator; + private readonly IEnumerator enumerator; - public Coroutine(IEnumerator enumerator) { + public Coroutine(IEnumerator enumerator) { this.enumerator = enumerator; this.enumerator.MoveNext(); } diff --git a/Coroutine/CoroutineHandler.cs b/Coroutine/CoroutineHandler.cs index ec50c5e..bd07c20 100644 --- a/Coroutine/CoroutineHandler.cs +++ b/Coroutine/CoroutineHandler.cs @@ -7,7 +7,7 @@ namespace Coroutine { private static readonly List TickingCoroutines = new List(); private static readonly List EventCoroutines = new List(); - public static void Start(IEnumerator coroutine) { + public static void Start(IEnumerator coroutine) { var inst = new Coroutine(coroutine); var type = inst.GetCurrentType(); if (type == WaitType.Tick) @@ -16,6 +16,10 @@ namespace Coroutine { EventCoroutines.Add(inst); } + public static void InvokeLater(IWait wait, Action action) { + Start(InvokeLaterImpl(wait, action)); + } + public static void Tick(double deltaSeconds) { for (var i = TickingCoroutines.Count - 1; i >= 0; i--) { var coroutine = TickingCoroutines[i]; @@ -40,5 +44,10 @@ namespace Coroutine { } } + private static IEnumerator InvokeLaterImpl(IWait wait, Action action) { + yield return wait; + action(); + } + } } \ No newline at end of file diff --git a/Coroutine/IWait.cs b/Coroutine/IWait.cs new file mode 100644 index 0000000..1e8263d --- /dev/null +++ b/Coroutine/IWait.cs @@ -0,0 +1,18 @@ +namespace Coroutine { + public interface IWait { + + WaitType GetWaitType(); + + bool Tick(double deltaSeconds); + + bool OnEvent(Event evt); + + } + + public enum WaitType { + + Tick, + Event + + } +} \ No newline at end of file diff --git a/Coroutine/Wait.cs b/Coroutine/Wait.cs deleted file mode 100644 index ebfba88..0000000 --- a/Coroutine/Wait.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; - -namespace Coroutine { - public abstract class Wait { - - public abstract WaitType GetWaitType(); - - public virtual bool Tick(double deltaSeconds) { - throw new NotSupportedException(); - } - - public virtual bool OnEvent(Event evt) { - throw new NotSupportedException(); - } - - } - - public enum WaitType { - - Tick, - Event - - } -} \ No newline at end of file diff --git a/Coroutine/WaitEvent.cs b/Coroutine/WaitEvent.cs index af78286..e55df97 100644 --- a/Coroutine/WaitEvent.cs +++ b/Coroutine/WaitEvent.cs @@ -1,5 +1,7 @@ +using System; + namespace Coroutine { - public class WaitEvent : Wait { + public struct WaitEvent : IWait { private readonly Event evt; @@ -7,11 +9,15 @@ namespace Coroutine { this.evt = evt; } - public override WaitType GetWaitType() { + public WaitType GetWaitType() { return WaitType.Event; } - public override bool OnEvent(Event evt) { + public bool Tick(double deltaSeconds) { + throw new NotSupportedException(); + } + + public bool OnEvent(Event evt) { return evt == this.evt; } diff --git a/Coroutine/WaitSeconds.cs b/Coroutine/WaitSeconds.cs index e5a4c7c..9933979 100644 --- a/Coroutine/WaitSeconds.cs +++ b/Coroutine/WaitSeconds.cs @@ -1,5 +1,7 @@ +using System; + namespace Coroutine { - public class WaitSeconds : Wait { + public struct WaitSeconds : IWait { private double seconds; @@ -7,14 +9,18 @@ namespace Coroutine { this.seconds = seconds; } - public override WaitType GetWaitType() { + public WaitType GetWaitType() { return WaitType.Tick; } - public override bool Tick(double deltaSeconds) { + public bool Tick(double deltaSeconds) { this.seconds -= deltaSeconds; return this.seconds <= 0; } + public bool OnEvent(Event evt) { + throw new NotSupportedException(); + } + } } \ No newline at end of file diff --git a/Test/Example.cs b/Test/Example.cs index 2ea7040..81d60b0 100644 --- a/Test/Example.cs +++ b/Test/Example.cs @@ -10,10 +10,14 @@ namespace Test { public static void Main() { CoroutineHandler.Start(WaitSeconds()); - CoroutineHandler.Start(RaiseTestEvent()); - CoroutineHandler.Start(WaitForTestEvent()); CoroutineHandler.Start(PrintEvery5Seconds()); + CoroutineHandler.InvokeLater(new WaitSeconds(10), () => { + Console.WriteLine("Raising test event"); + CoroutineHandler.RaiseEvent(TestEvent); + }); + CoroutineHandler.InvokeLater(new WaitEvent(TestEvent), () => Console.WriteLine("Test event received")); + var lastTime = DateTime.Now; while (true) { var currTime = DateTime.Now; @@ -23,28 +27,23 @@ namespace Test { } } - private static IEnumerator WaitSeconds() { + private static IEnumerator WaitSeconds() { Console.WriteLine("First thing " + DateTime.Now); yield return new WaitSeconds(1); Console.WriteLine("After 1 second " + DateTime.Now); - yield return new WaitSeconds(5); - Console.WriteLine("After 5 seconds " + DateTime.Now); - yield return new WaitSeconds(10); + yield return new WaitSeconds(9); Console.WriteLine("After 10 seconds " + DateTime.Now); - } - - private static IEnumerator RaiseTestEvent() { + yield return new WaitSeconds(5); + Console.WriteLine("After 5 more seconds " + DateTime.Now); yield return new WaitSeconds(10); - Console.WriteLine("Raising test event"); - CoroutineHandler.RaiseEvent(TestEvent); + Console.WriteLine("After 10 more seconds " + DateTime.Now); + + yield return new WaitSeconds(20); + Console.WriteLine("Done"); + Environment.Exit(0); } - private static IEnumerator WaitForTestEvent() { - yield return new WaitEvent(TestEvent); - Console.WriteLine("Test event received"); - } - - private static IEnumerator PrintEvery5Seconds() { + private static IEnumerator PrintEvery5Seconds() { while (true) { yield return new WaitSeconds(10); Console.WriteLine("The time is " + DateTime.Now);