Skip to content

Commit

Permalink
Merge pull request #18 from techno-dwarf-works/feature/clean-up
Browse files Browse the repository at this point in the history
Version 0.1.11
  • Loading branch information
OpOpYaDev committed May 21, 2024
1 parent 79cefb9 commit ef7f73d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
9 changes: 0 additions & 9 deletions Runtime/Implementations/StateMachine.cs
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 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;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Modules/StackOverflowModule.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 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

0 comments on commit ef7f73d

Please sign in to comment.