Skip to content

Commit

Permalink
rename , add where method
Browse files Browse the repository at this point in the history
  • Loading branch information
Haijie HJ2 Pan committed Apr 28, 2023
1 parent 6c9c330 commit ee6153e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
14 changes: 7 additions & 7 deletions FSM/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class State<TState, TParam> where TState : Enum
{
internal readonly TState Name;
readonly StateMachine<TState, TParam> m_Machine;
internal readonly List<Translation<TState, TParam>> Translations = new List<Translation<TState, TParam>>();
internal readonly List<Transition<TState, TParam>> Transitions = new List<Transition<TState, TParam>>();
private Action<TParam> m_OnInitialize;
private Action<TParam> m_OnEnter;
private Action<TParam> m_OnUpdate;
Expand All @@ -22,8 +22,8 @@ internal static State<TState, TParam> Clone(State<TState, TParam> original, Stat
clone.m_OnEnter = original.m_OnEnter;
clone.m_OnUpdate = original.m_OnUpdate;
clone.m_OnExit = original.m_OnExit;
foreach (Translation<TState, TParam> translation in original.Translations)
clone.Translations.Add(Translation<TState, TParam>.Clone(translation, clone));
foreach (Transition<TState, TParam> transition in original.Transitions)
clone.Transitions.Add(Transition<TState, TParam>.Clone(transition, clone));
return clone;
}

Expand All @@ -41,11 +41,11 @@ public StateMachine<TState, TParam> End()
{
return m_Machine;
}
public Translation<TState, TParam> Translate(Func<TParam, bool> valid)
public Transition<TState, TParam> Translate(Func<TParam, bool> valid)
{
Translation<TState, TParam> translation = new Translation<TState, TParam>(this, valid);
Translations.Add(translation);
return translation;
Transition<TState, TParam> transition = new Transition<TState, TParam>(this, valid);
Transitions.Add(transition);
return transition;
}
public State<TState, TParam> Initialize(Action<TParam> init) { m_OnInitialize = init; return this; }
public State<TState, TParam> Enter(Action<TParam> enter) { m_OnEnter = enter; return this; }
Expand Down
34 changes: 23 additions & 11 deletions FSM/StateMachine.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Task.Switch.Structure.FSM
{
Expand Down Expand Up @@ -68,15 +67,28 @@ public State<TState, TParam> NewState(TState stateName)
m_States.Add(state);
return state;
}
public StateMachine<TState, TParam> Any(TState to, Func<TParam, bool> valid, Action<TParam> transfer = null)
public StateMachine<TState, TParam> Any(Func<TParam, bool> valid, Action<TParam> transfer,TState to)
{
foreach (State<TState, TParam> state in m_States)
{
if (!Enum.Equals(to, state.Name))
if (to.GetHashCode() != state.Name.GetHashCode())
{
Translation<TState, TParam> translation = new Translation<TState, TParam>(state, valid).Transfer(transfer);
translation.To(to);
state.Translations.Add(translation);
Transition<TState, TParam> transition = new Transition<TState, TParam>(state, valid).Transfer(transfer);
transition.To(to);
state.Transitions.Add(transition);
}
}
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())
{
Transition<TState, TParam> transition = new Transition<TState, TParam>(state, valid).Transfer(transfer);
transition.To(to);
state.Transitions.Add(transition);
}
}
return this;
Expand All @@ -91,21 +103,21 @@ public StateMachine<TState, TParam> Initialize()
private State<TState, TParam> GetState(TState stateName)
{
foreach (State<TState, TParam> state in m_States)
if (Enum.Equals(stateName, state.Name))
if (state.Name.GetHashCode() == stateName.GetHashCode())
return state;
throw new Exception($"{stateName} is not exist! Please call {nameof(NewState)} to create this state");
}
public void Update()
{
if (m_Status == StateMachineStatus.Running && m_CurrentActiveState != null)
{
foreach (Translation<TState, TParam> translation in m_CurrentActiveState.Translations)
foreach (Transition<TState, TParam> transition in m_CurrentActiveState.Transitions)
{
if (translation.OnValid())
if (transition.OnValid())
{
m_CurrentActiveState.OnExit();
m_CurrentActiveState = GetState(translation.ToStateName);
translation.OnTransfer();
m_CurrentActiveState = GetState(transition.ToStateName);
transition.OnTransfer();
m_CurrentActiveState.OnEnter();
return;
}
Expand Down
20 changes: 10 additions & 10 deletions FSM/Translation.cs → FSM/Transition.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
using System;
namespace Task.Switch.Structure.FSM
{
public class Translation<TState, TParam> where TState : Enum
where TParam : class
public class Transition<TState, TParam> where TState : Enum
where TParam : class
{
private readonly Func<TParam, bool> m_Valid;
private Action<TParam> m_Transfer;
internal TState ToStateName { private set; get; }
private readonly State<TState, TParam> m_State;
public Translation(State<TState, TParam> state, Func<TParam, bool> valid)
public Transition(State<TState, TParam> state, Func<TParam, bool> valid)
{
m_State = state;
m_Valid = valid;
}

public static Translation<TState, TParam> Clone(Translation<TState, TParam> original, State<TState, TParam> state)
public static Transition<TState, TParam> Clone(Transition<TState, TParam> original, State<TState, TParam> state)
{
Translation<TState, TParam> clone = new Translation<TState, TParam>(state, original.m_Valid);
Transition<TState, TParam> clone = new Transition<TState, TParam>(state, original.m_Valid);
clone.m_Transfer = original.m_Transfer;
clone.ToStateName = original.ToStateName;
return clone;
Expand All @@ -28,14 +28,14 @@ public State<TState, TParam> To(TState stateName)
}
internal bool OnValid()
{
bool valid = false;
bool validity = false;
if (m_Valid != null)
valid = m_Valid(m_State.GetParameter());
validity = m_Valid(m_State.GetParameter());
if (StateMachine<TState, TParam>.Log != null)
StateMachine<TState, TParam>.Log($"State:{m_State.Name} {nameof(OnValid)}:{valid} ToState:{ToStateName}");
return valid;
StateMachine<TState, TParam>.Log($"State:{m_State.Name} {nameof(OnValid)}:{validity} ToState:{ToStateName}");
return validity;
}
public Translation<TState, TParam> Transfer(Action<TParam> transfer)
public Transition<TState, TParam> Transfer(Action<TParam> transfer)
{
m_Transfer = transfer;
return this;
Expand Down

0 comments on commit ee6153e

Please sign in to comment.