Game Development Reference
In-Depth Information
Planning for multiple cases
The alternate simple approach to state machines is to use the if blocks to test what a state
is: the only downside is that this can become very cumbersome to manage very quickly.
Consider a slightly more complex scenario (related to the game) where a group of thugs
are battling with you, but they are only confident when they are in a group and will run if
their health is good. Such a system wouldn't be possible using the previous switch style
(or at least will be difficult to do so), so by using several if blocks as shown in the fol-
lowing code, we can achieve something like this:
if (EnemyState == State.Idle)
{
//Check for player
// If player found EnemyState == State.Attacking
//Check for fellow enemies
}
if (EnemyState == State.Attacking && PlayerState ==
State.Idle)
{
//Enemy Sneak attack
}
if (EnemyState == State.Attacking)
{
//Play Attacking Music
}
if (EnemyState == State.Attacking && Health < 5)
{
//Run away
}
if (EnemyState == State.Attacking && PlayerState ==
State.RunningAway)
{
//Give Chase
}
Search WWH ::




Custom Search