using System.Collections.Generic; namespace SemiColinGames { public interface IState { public void Enter(); // 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 { Dictionary> states; IState state; public FSM(Dictionary> states, string initial) { this.states = states; StateName = initial; Transition(StateName); } public string StateName { get; private set; } public void Update(T obj, float modelTime, World world) { string newState = state.Update(obj, modelTime, world); if (newState != null) { Transition(newState); } } void Transition(string state) { StateName = state; IState newState = states[state]; this.state = newState; this.state.Enter(); } } }