From e4c50c916beb03c23ce57a97a19a86d0f8d667f8 Mon Sep 17 00:00:00 2001 From: Inspiaaa Date: Wed, 30 Aug 2023 22:56:37 +0200 Subject: [PATCH] Create WaitWhileCoroutine --- src/Co.cs | 4 +++ src/Coroutines/WaitWhileCoroutine.cs | 37 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/Coroutines/WaitWhileCoroutine.cs diff --git a/src/Co.cs b/src/Co.cs index 9ac7d51..8ee2e3d 100644 --- a/src/Co.cs +++ b/src/Co.cs @@ -74,6 +74,10 @@ public static WaitDelayCoroutine Wait(float delay) => new WaitDelayCoroutine(delay); + public static WaitWhileCoroutine WaitWhile(Func condition) + => new WaitWhileCoroutine(condition); + + public static WaitForSignalCoroutine WaitForSignal(Godot.Object obj, string signal) => new WaitForSignalCoroutine(obj, signal); diff --git a/src/Coroutines/WaitWhileCoroutine.cs b/src/Coroutines/WaitWhileCoroutine.cs new file mode 100644 index 0000000..14a4233 --- /dev/null +++ b/src/Coroutines/WaitWhileCoroutine.cs @@ -0,0 +1,37 @@ +using System; +using HCoroutines.Util; + +namespace HCoroutines +{ + /// + /// Waits while a certain condition is true. + /// + public class WaitWhileCoroutine : CoroutineBase + { + private Func condition; + + public WaitWhileCoroutine(Func condition) + { + this.condition = condition; + } + + public override void OnEnter() + { + CheckCondition(); + ResumeUpdates(); + } + + public override void Update() + { + CheckCondition(); + } + + private void CheckCondition() + { + if (!this.condition()) + { + Kill(); + } + } + } +} \ No newline at end of file