|
|
@ -4,11 +4,12 @@ using System.Collections.Generic; |
|
|
|
namespace SemiColinGames { |
|
|
|
public interface IState<T> { |
|
|
|
public void Enter(); |
|
|
|
public string? Update(T obj, float modelTime, World world); |
|
|
|
|
|
|
|
// Returns the name of the new state, or null if we should stay in the same state.
|
|
|
|
public string Update(T obj, float modelTime, World world); |
|
|
|
} |
|
|
|
|
|
|
|
public class FSM<T> { |
|
|
|
float timeInState = 0f; |
|
|
|
Dictionary<string, IState<T>> states; |
|
|
|
IState<T> state; |
|
|
|
|
|
|
@ -21,16 +22,13 @@ namespace SemiColinGames { |
|
|
|
public string StateName { get; private set; } |
|
|
|
|
|
|
|
public void Update(T obj, float modelTime, World world) { |
|
|
|
timeInState += modelTime; |
|
|
|
string? newState = state.Update(obj, modelTime, world); |
|
|
|
string newState = state.Update(obj, modelTime, world); |
|
|
|
if (newState != null) { |
|
|
|
Transition(newState); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void Transition(string state) { |
|
|
|
Debug.WriteLine("{0} -> {1} @ {2}", StateName, state, timeInState); |
|
|
|
timeInState = 0f; |
|
|
|
StateName = state; |
|
|
|
IState<T> newState = states[state]; |
|
|
|
this.state = newState; |
|
|
|