From a07da656c7d12dcc968ab07b059b58d27479d90e Mon Sep 17 00:00:00 2001 From: Inspiaaa Date: Thu, 5 Sep 2024 15:33:01 +0200 Subject: [PATCH] Add option to set run/process mode in Co --- src/Co.cs | 156 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 115 insertions(+), 41 deletions(-) diff --git a/src/Co.cs b/src/Co.cs index d888ce5..b199701 100644 --- a/src/Co.cs +++ b/src/Co.cs @@ -36,55 +36,104 @@ private static Coroutine[] GetCoroutines(IEnumerator[] enumerators) public static void Run(CoroutineBase coroutine) => CoroutineManager.Instance.StartCoroutine(coroutine); - public static Coroutine Run(IEnumerator coroutine) + public static Coroutine Run( + IEnumerator coroutine, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) { - Coroutine co = new Coroutine(coroutine); + Coroutine co = new Coroutine(coroutine, processMode, runMode); CoroutineManager.Instance.StartCoroutine(co); return co; } - public static Coroutine Run(Func creator) - => Run(creator()); + public static Coroutine Run( + Func creator, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) + { + Coroutine co = new Coroutine(creator(), processMode, runMode); + CoroutineManager.Instance.StartCoroutine(co); + return co; + } - public static Coroutine Run(Func creator) + public static Coroutine Run( + Func creator, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) { - Coroutine coroutine = new Coroutine(creator); + Coroutine coroutine = new Coroutine(creator, processMode, runMode); CoroutineManager.Instance.StartCoroutine(coroutine); return coroutine; } - public static Coroutine Coroutine(IEnumerator enumerator) - => new Coroutine(enumerator); + public static Coroutine Coroutine( + IEnumerator enumerator, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) + => new Coroutine(enumerator, processMode, runMode); - public static Coroutine Coroutine(Func creator) - => new Coroutine(creator); + public static Coroutine Coroutine( + Func creator, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) + => new Coroutine(creator, processMode, runMode); public static ParallelCoroutine Parallel(params IEnumerator[] enumerators) => new ParallelCoroutine(GetCoroutines(enumerators)); + + public static ParallelCoroutine Parallel( + CoProcessMode processMode, + CoRunMode runMode, + params IEnumerator[] enumerators) + => new ParallelCoroutine(processMode, runMode, GetCoroutines(enumerators)); public static ParallelCoroutine Parallel(params CoroutineBase[] coroutines) => new ParallelCoroutine(coroutines); + + public static ParallelCoroutine Parallel( + CoProcessMode processMode, + CoRunMode runMode, + params CoroutineBase[] coroutines) + => new ParallelCoroutine(processMode, runMode, coroutines); public static SequentialCoroutine Sequence(params IEnumerator[] enumerators) => new SequentialCoroutine(GetCoroutines(enumerators)); + + public static SequentialCoroutine Sequence( + CoProcessMode processMode, + CoRunMode runMode, + params IEnumerator[] enumerators) + => new SequentialCoroutine(processMode, runMode, GetCoroutines(enumerators)); public static SequentialCoroutine Sequence(params CoroutineBase[] coroutines) => new SequentialCoroutine(coroutines); + + public static SequentialCoroutine Sequence( + CoProcessMode processMode, + CoRunMode runMode, + params CoroutineBase[] coroutines) + => new SequentialCoroutine(processMode, runMode, coroutines); - public static WaitDelayCoroutine Wait(float delay) - => new WaitDelayCoroutine(delay); + public static WaitDelayCoroutine Wait(float delay, CoRunMode runMode = CoRunMode.Inherit) + => new WaitDelayCoroutine(delay, runMode); - public static WaitWhileCoroutine WaitWhile(Func condition) - => new WaitWhileCoroutine(condition); + public static WaitWhileCoroutine WaitWhile( + Func condition, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) + => new WaitWhileCoroutine(condition, processMode, runMode); - public static WaitUntilCoroutine WaitUntil(Func condition) - => new WaitUntilCoroutine(condition); + public static WaitUntilCoroutine WaitUntil( + Func condition, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) + => new WaitUntilCoroutine(condition, processMode, runMode); public static WaitForSignalCoroutine WaitForSignal(GodotObject obj, string signal) @@ -92,42 +141,67 @@ public static WaitForSignalCoroutine WaitForSignal(GodotObject obj, string signa - public static RepeatCoroutine Repeat(int times, Func creator) - => new RepeatCoroutine(times, creator); + public static RepeatCoroutine Repeat( + int times, + Func creator, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) + => new RepeatCoroutine(times, creator, processMode, runMode); - public static RepeatCoroutine Repeat(int times, Func creator) - => new RepeatCoroutine(times, coroutine => creator()); + public static RepeatCoroutine Repeat( + int times, + Func creator, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) + => new RepeatCoroutine(times, creator, processMode, runMode); - public static RepeatCoroutine Repeat(int times, Func creator) - => new RepeatCoroutine(times, coroutine => new Coroutine(creator(coroutine))); + public static RepeatCoroutine Repeat( + int times, + Func creator, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) + => new RepeatCoroutine(times, coroutine => new Coroutine(creator(coroutine)), processMode, runMode); - public static RepeatCoroutine Repeat(int times, Func creator) - => new RepeatCoroutine(times, coroutine => new Coroutine(creator())); + public static RepeatCoroutine Repeat( + int times, + Func creator, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) + => new RepeatCoroutine(times, () => new Coroutine(creator()), processMode, runMode); - public static RepeatCoroutine RepeatInfinitely(Func creator) - => new RepeatCoroutine(-1, creator); + public static RepeatCoroutine RepeatInfinitely( + Func creator, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) + => new RepeatCoroutine(-1, creator, processMode, runMode); - public static RepeatCoroutine RepeatInfinitely(Func creator) - => new RepeatCoroutine(-1, coroutine => creator()); + public static RepeatCoroutine RepeatInfinitely( + Func creator, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) + => new RepeatCoroutine(-1, creator, processMode, runMode); - public static RepeatCoroutine RepeatInfinitely(Func creator) - => new RepeatCoroutine(-1, coroutine => new Coroutine(creator(coroutine))); + public static RepeatCoroutine RepeatInfinitely( + Func creator, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) + => new RepeatCoroutine(-1, coroutine => new Coroutine(creator(coroutine)), processMode, runMode); - public static RepeatCoroutine RepeatInfinitely(Func creator) - => new RepeatCoroutine(-1, coroutine => new Coroutine(creator())); + public static RepeatCoroutine RepeatInfinitely( + Func creator, + CoProcessMode processMode = CoProcessMode.Inherit, + CoRunMode runMode = CoRunMode.Inherit) + => new RepeatCoroutine(-1, () => new Coroutine(creator()), processMode, runMode); - public static TweenCoroutine Tween(Func createTween) - => new TweenCoroutine(createTween); - - // TODO: Create additional methods for creating Tween Coroutines (e.g. create Tween on Manager, setup via Action, - // or bound to a node) + public static TweenCoroutine Tween(Action setupTween) + => new TweenCoroutine(setupTween); - public static AwaitCoroutine Await(Task task) - => new AwaitCoroutine(task); + public static AwaitCoroutine Await(Task task, CoRunMode runMode = CoRunMode.Inherit) + => new AwaitCoroutine(task, runMode); - public static AwaitCoroutine Await(Task task) - => new AwaitCoroutine(task); + public static AwaitCoroutine Await(Task task, CoRunMode runMode = CoRunMode.Inherit) + => new AwaitCoroutine(task, runMode); } \ No newline at end of file