Skip to content

Instantly share code, notes, and snippets.

@xdegtyarev
Created August 5, 2014 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdegtyarev/8b812bbfdea343a95c36 to your computer and use it in GitHub Desktop.
Save xdegtyarev/8b812bbfdea343a95c36 to your computer and use it in GitHub Desktop.
FSM example: robot states
public class Charging : State {
public override void Execute( Robot entity ) {
if ( entity.charge == 100f ) {
entity.fsm.ChangeState( SearchHumans.Instance );
} else {
entity.DoCharge();
}
}
// singleton
private static instance;
public static Instance {
if ( instance != null ) {
return instance;
} else {
instance = new Charging();
return instance;
}
}
}
public class SearchHumans : State {
public override void Execute( Robot entity ) {
if ( entity.isHumansDetected() ) {
entity.fsm.ChangeState( KillAllHumans.Instance );
} else {
entity.SearchHumans();
}
}
// singleton code
}
public class KillAllHumans : State {
public override void Execute( Robot entity ) {
if ( entity.allHumansIsDead() ) {
entity.fsm.ChangeState( Charging.Instance );
} else {
entity.KillHumans();
}
}
// singleton code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment