Game Development Reference
In-Depth Information
Using a finite state machine, each of these behaviors would be implemented in a separate state.
The initial state would process the player's current command and transition to the corresponding
state implementing the command. Once each of these commands was completed, the finite state
machine would transition back to the state that processes the player's commands. The process
would be repeated, and the state machine would transition to the state implementing the player's
current command. (See Figure 8-2 .)
Retreat Finished = false
Retreat
State
Command = Retreat
Retreat Finished = true
Patrol Finished = false
Process
Command
State
Command = Patrol
Patrol
State
Default State
Patrol Finished = true
Attack Enemy Finished = true
Command = Attack Enemy
Attack
Enemy
State
Attack Enemy Finished = false
Figure 8-2. The finite state machine
In terms of implementing a finite state machine in code, we have a Finite State Machine class for
the enemy type we want to control. Each of the behaviors for this type of enemy is represented by
a separate class and is loaded into the finite state machine for processing. The UpdateMachine()
function in the Finite State Machine class is called to update the state machine. See Figure 8-3 for
a diagram illustrating a general class level view of how our state machine in Figure 8-2 would be
implemented in code.
 
 
Search WWH ::




Custom Search