Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 0.1.11 #18

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ public virtual void Run()
{
if (!module.AllowRunMachine(this))
{
var message = $"{module} not allow machine run";
Debug.LogWarning(message);

return;
}
}
Expand All @@ -85,9 +82,6 @@ public virtual void Stop()
{
if (!module.AllowStopMachine(this))
{
var message = $"{module} not allow machine stop";
Debug.LogWarning(message);

return;
}
}
Expand Down Expand Up @@ -129,9 +123,6 @@ public async Task ChangeStateAsync(TState state, CancellationToken cancellationT
{
if (!module.AllowChangeState(this, state))
{
var message = $"{module} not allow change state to {state}";
Debug.LogWarning(message);

return;
}
}
Expand Down
69 changes: 69 additions & 0 deletions Assets/BetterStateMachine/Runtime/Modules/StackOverflowModule.cs
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.

2 changes: 1 addition & 1 deletion Assets/BetterStateMachine/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.tdw.better.statemachine",
"displayName": "Better State Machine",
"version": "0.1.10",
"version": "0.1.11",
"unity": "2021.3",
"description": " ",
"dependencies": {
Expand Down
Loading