Created
August 5, 2014 13:49
-
-
Save xdegtyarev/8b812bbfdea343a95c36 to your computer and use it in GitHub Desktop.
FSM example: robot states
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 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