Created
October 10, 2019 02:51
-
-
Save almyu/8f6e3e7bf0c6d4d1e798b7c7c7fb59c7 to your computer and use it in GitHub Desktop.
Coroutine action for NPBehave
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NPBehave; | |
using System.Collections; | |
public class CoroutineAction : Task | |
{ | |
System.Func<IEnumerator> starter; | |
IEnumerator coro; | |
public CoroutineAction(System.Func<IEnumerator> starter) : base("CoroutineAction") { | |
this.starter = starter; | |
this.coro = null; | |
} | |
protected override void DoStart() { | |
coro = starter(); | |
if (!coro.MoveNext()) Stopped(true); | |
else if (coro.Current == null) RootNode.Clock.AddUpdateObserver(OnUpdate); | |
else Stopped(false); | |
} | |
void OnUpdate() { | |
if (!coro.MoveNext()) { | |
RootNode.Clock.RemoveUpdateObserver(OnUpdate); | |
Stopped(true); | |
} | |
else if (coro.Current != null) { | |
RootNode.Clock.RemoveUpdateObserver(OnUpdate); | |
Stopped(false); | |
} | |
} | |
protected override void DoStop() { | |
coro = null; | |
RootNode.Clock.RemoveUpdateObserver(OnUpdate); | |
Stopped(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment