Skip to content

Commit

Permalink
Create base of support for pause
Browse files Browse the repository at this point in the history
- Create an enum for different run modes that determine whether a coroutine can be paused and when it runs.
- Refactor the CoroutineBase code by adding a new Init() method which internally calls OnEnter(). This allows the virtual OnEnter() method to be empty, so that subclasses need not call the base class implementation.
  • Loading branch information
Inspiaaa committed Sep 5, 2024
1 parent 39a99d6 commit 5123e67
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
26 changes: 26 additions & 0 deletions src/CoRunMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace HCoroutines;

/// <summary>
/// The RunMode determines when the coroutine is run and whether it can be paused.
/// </summary>
public enum CoRunMode {
/// <summary>
/// The RunMode is inherited from the parent coroutine. If there is no parent, Pausable is used.
/// </summary>
Inherit,

/// <summary>
/// The coroutine is paused when the game is paused.
/// </summary>
Pausable,

/// <summary>
/// The coroutine only runs when the game is paused.
/// </summary>
WhenPaused,

/// <summary>
/// The coroutine always runs, regardless of whether the game is paused is or not.
/// </summary>
Always
}
27 changes: 22 additions & 5 deletions src/CoroutineBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,44 @@ public class CoroutineBase
/// Determines whether the Update() method is called during process frames or physics frames.
/// </summary>
public CoProcessMode ProcessMode { get; private set; }


/// <summary>
/// Determines the pause behaviour of this coroutine.
/// </summary>
public CoRunMode RunMode { get; private set; }

public void StartCoroutine(CoroutineBase coroutine)
{
coroutine.Manager = Manager;
coroutine.Parent = this;

AddChild(coroutine);
coroutine.OnEnter();
coroutine.Init();
}

/// <summary>
/// Called when the coroutine starts.
/// Initializes the coroutine once it has been added to the active coroutine hierarchy.
/// </summary>
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();
}

/// <summary>
/// Called when the coroutine starts.
/// </summary>
public virtual void OnEnter() { }

/// <summary>
/// Called every frame if the coroutine is playing.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/CoroutineManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5123e67

Please sign in to comment.