Skip to content

Instantly share code, notes, and snippets.

@Urethramancer
Created July 21, 2016 03:17
Show Gist options
  • Save Urethramancer/84bd84d549d317bf68eab3716c74e796 to your computer and use it in GitHub Desktop.
Save Urethramancer/84bd84d549d317bf68eab3716c74e796 to your computer and use it in GitHub Desktop.
The most basic state machine class and state interface in C#, free of enumeration.
public interface State
{
void Enter();
void Run();
void Exit();
}
public class StateMachine
{
State currentState;
public void ChangeState(State newState)
{
if(currentState == newState || newState == null) return;
if(currentState != null) currentState.Exit();
currentState = newState;
currentState.Enter();
}
public void UpdateState()
{
if(currentState != null) currentState.Run();
}
public void ResetState()
{
if(currentState == null) return;
currentState.Exit();
currentState.Enter();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment