generated from techno-dwarf-works/unity-package-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from techno-dwarf-works/feature/clean-up
Version 0.1.11
- Loading branch information
OpOpYaDev
authored and
OpOpYaDev
committed
May 21, 2024
1 parent
79cefb9
commit ef7f73d
Showing
4 changed files
with
73 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using Better.StateMachine.Runtime.States; | ||
using UnityEngine; | ||
|
||
namespace Better.StateMachine.Runtime.Modules | ||
{ | ||
public class StackOverflowModule<TState> : SingleModule<TState> | ||
where TState : BaseState | ||
{ | ||
private const int DefaultOverflowDepth = 50; | ||
|
||
public event Action Locked; | ||
public readonly int OverflowDepth; | ||
|
||
public int Depth { get; private set; } | ||
public int MaxDepth { get; private set; } | ||
public bool IsLocked { get; private set; } | ||
|
||
public StackOverflowModule(int overflowDepth = DefaultOverflowDepth) | ||
{ | ||
OverflowDepth = overflowDepth; | ||
} | ||
|
||
protected void Lock() | ||
{ | ||
IsLocked = true; | ||
Locked?.Invoke(); | ||
} | ||
|
||
public void Unlock() | ||
{ | ||
IsLocked = false; | ||
} | ||
|
||
protected internal override bool AllowChangeState(IStateMachine<TState> stateMachine, TState state) | ||
{ | ||
return base.AllowChangeState(stateMachine, state) && !IsLocked; | ||
} | ||
|
||
protected internal override void OnStatePreChanged(IStateMachine<TState> stateMachine, TState state) | ||
{ | ||
base.OnStatePreChanged(stateMachine, state); | ||
|
||
Depth++; | ||
MaxDepth = Mathf.Max(MaxDepth, Depth); | ||
|
||
if (Depth >= OverflowDepth) | ||
{ | ||
Lock(); | ||
} | ||
} | ||
|
||
protected internal override void OnStateChanged(IStateMachine<TState> stateMachine, TState state) | ||
{ | ||
base.OnStateChanged(stateMachine, state); | ||
|
||
Depth = 0; | ||
} | ||
|
||
protected internal override void OnMachineStopped(IStateMachine<TState> stateMachine) | ||
{ | ||
base.OnMachineStopped(stateMachine); | ||
|
||
Depth = 0; | ||
MaxDepth = 0; | ||
IsLocked = false; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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