Game Development Reference
In-Depth Information
How it works...
The first thing we did was define an abstract class called AIState . It's convenient to use
the control pattern since it means we have access to the spatial and familiar ways to attach/
detach states and turn them on and off.
The stateEnter and stateExit methods are called when the state is enabled and dis-
abled, and happens on transition from and to other states. The class also expects there to be
some kind of AI control class.
The first state extending AIState was the PatrolState . Its update method has three
outcomes. If the AI has spotted something it can attack, it will change to the Attack-
State . Otherwise, if it's close to the place it has selected to move to, it will select a new
target. Or, if it still has some way to go, it will just continue moving towards it.
The AttackState has a bit more functionality, as it also handles firing and ammunition
management. Remember, if it has come here, the AI has already decided it should attack
something. Hence, if it has no ammunition, it will switch to the RetreatState (al-
though we generously give it some ammo every time it enters the state). Otherwise, it will
attack or try attacking.
The RetreatState only has one goal: to try to get as far away from the threat as pos-
sible. Once it has lost sight of the target, or has fled for the specified amount of time, it will
switch to PatrolState .
As we can see, the logic is all contained within the associated state, which can be very con-
venient. The flow of the states will also always make sure the AI ends up in the
PatrolState in the end.
Search WWH ::




Custom Search