diff --git a/src/CoRunMode.cs b/src/CoRunMode.cs new file mode 100644 index 0000000..32242c5 --- /dev/null +++ b/src/CoRunMode.cs @@ -0,0 +1,26 @@ +namespace HCoroutines; + +/// +/// The RunMode determines when the coroutine is run and whether it can be paused. +/// +public enum CoRunMode { + /// + /// The RunMode is inherited from the parent coroutine. If there is no parent, Pausable is used. + /// + Inherit, + + /// + /// The coroutine is paused when the game is paused. + /// + Pausable, + + /// + /// The coroutine only runs when the game is paused. + /// + WhenPaused, + + /// + /// The coroutine always runs, regardless of whether the game is paused is or not. + /// + Always +} \ No newline at end of file diff --git a/src/CoroutineBase.cs b/src/CoroutineBase.cs index f69c8d8..312adb3 100644 --- a/src/CoroutineBase.cs +++ b/src/CoroutineBase.cs @@ -27,27 +27,44 @@ public class CoroutineBase /// Determines whether the Update() method is called during process frames or physics frames. /// public CoProcessMode ProcessMode { get; private set; } - + + /// + /// Determines the pause behaviour of this coroutine. + /// + public CoRunMode RunMode { get; private set; } + public void StartCoroutine(CoroutineBase coroutine) { coroutine.Manager = Manager; coroutine.Parent = this; AddChild(coroutine); - coroutine.OnEnter(); + coroutine.Init(); } /// - /// Called when the coroutine starts. + /// Initializes the coroutine once it has been added to the active coroutine hierarchy. /// - public virtual void OnEnter() + public void Init() { if (this.ProcessMode == CoProcessMode.Inherit) { this.ProcessMode = Parent?.ProcessMode ?? CoProcessMode.Normal; - } + } + + if (this.RunMode == CoRunMode.Inherit) + { + this.RunMode = Parent?.RunMode ?? CoRunMode.Pausable; + } + + OnEnter(); } + /// + /// Called when the coroutine starts. + /// + public virtual void OnEnter() { } + /// /// Called every frame if the coroutine is playing. /// diff --git a/src/CoroutineManager.cs b/src/CoroutineManager.cs index d2995e4..df92278 100644 --- a/src/CoroutineManager.cs +++ b/src/CoroutineManager.cs @@ -20,7 +20,7 @@ public partial class CoroutineManager : Node public void StartCoroutine(CoroutineBase coroutine) { coroutine.Manager = this; - coroutine.OnEnter(); + coroutine.Init(); } public void ActivateCoroutine(CoroutineBase coroutine)