Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Haijie HJ2 Pan committed Apr 28, 2023
1 parent ee6153e commit c86a7c5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions FSM/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public State(TState name, StateMachine<TState, TParam> stateMachine)
m_Machine = stateMachine;
}

internal int GetStateInt() { return Name.GetHashCode(); }

internal TParam GetParameter()
{
return m_Machine.GetParameter();
Expand Down
8 changes: 5 additions & 3 deletions FSM/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ public State<TState, TParam> NewState(TState stateName)
m_States.Add(state);
return state;
}

public StateMachine<TState, TParam> Any(Func<TParam, bool> valid, Action<TParam> transfer,TState to)
{
foreach (State<TState, TParam> state in m_States)
{
if (to.GetHashCode() != state.Name.GetHashCode())
if (to.GetHashCode() != state.GetStateInt())
{
Transition<TState, TParam> transition = new Transition<TState, TParam>(state, valid).Transfer(transfer);
transition.To(to);
Expand All @@ -80,11 +81,12 @@ public StateMachine<TState, TParam> Any(Func<TParam, bool> valid, Action<TParam>
}
return this;
}

public StateMachine<TState, TParam> Where(TState from, Func<TParam, bool> valid, Action<TParam> transfer, TState to)
{
foreach(State<TState,TParam> state in m_States)
{
if ((from.GetHashCode() & state.GetHashCode()) == state.GetHashCode())
if ((from.GetHashCode() & state.GetStateInt()) == state.GetStateInt())
{
Transition<TState, TParam> transition = new Transition<TState, TParam>(state, valid).Transfer(transfer);
transition.To(to);
Expand All @@ -103,7 +105,7 @@ public StateMachine<TState, TParam> Initialize()
private State<TState, TParam> GetState(TState stateName)
{
foreach (State<TState, TParam> state in m_States)
if (state.Name.GetHashCode() == stateName.GetHashCode())
if (state.GetStateInt() == stateName.GetHashCode())
return state;
throw new Exception($"{stateName} is not exist! Please call {nameof(NewState)} to create this state");
}
Expand Down

0 comments on commit c86a7c5

Please sign in to comment.