Game Development Reference
In-Depth Information
Figure 9.11
State design pattern.
The
AIState
base class could then be defined as follows:
class
AIState
AIController
parent
function
Update(
float
deltaTime
)
function
Enter()
function
Exit()
end
The parent reference allows a particular instance of an
AIState
to keep track of
which
AIController
owns it. This is required because if we want to switch
into a new state, there has to be some way to notify the
AIController
of this.
Each
AIState
also has its own
Update
,
Enter
, and
Exit
functions, which
can implement state-specific actions.
The
AIController
class could then have a reference to the current
AIState
and the required
Update
and
SetState
functions:
class
AIController
AIState
state
function
Update(
float
deltaTime
)
function
SetState(
AIState
newState
)
end