Created
July 21, 2016 03:17
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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