O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

out all the states in MinerOwnedStates.cpp and examine the Miner class to
familiarize yourself with its member variables. Above all else, make sure
you understand how the state design pattern works before you read any fur
-
ther. If you are a little unsure, please take the time to go over the previous
few pages until you feel comfortable with the concept.
You have seen how the use of the state design pattern provides a very
flexible mechanism for state-driven agents. It’s extremely easy to add addi
-
tional states as and when required. Indeed, should you so wish, you can
switch an agent’s entire state architecture for an alternative one. This can
be useful if you have a very complicated design that would be better orga
-
nized as a collection of several separate smaller state machines. For
example, the state machine for a first-person shooter (FPS) like Unreal 2
tends to be large and complex. When designing the AI for a game of this
sort you may find it preferable to think in terms of several smaller state
machines representing functionality like “defend the flag” or “explore
map,” which can be switched in and out when appropriate. The state design
pattern makes this easy to do.
Making the State Base Class Reusable
As the design stands, it’s necessary to create a separate State base class for
each character type to derive its states from. Instead, let’s make it reusable
by turning it into a class template.
template <class entity_type>
class State
{
public:
virtual void Enter(entity_type*)=0;
virtual void Execute(entity_type*)=0;
virtual void Exit(entity_type*)=0;
virtual ~State(){}
};
The declaration for a concrete state — using the EnterMineAndDigFor
-
Nugget miner state as an example — now looks like this:
class EnterMineAndDigForNugget : public State<Miner>
{
public:
/* OMITTED */
};
This, as you will see shortly, makes life easier in the long run.
62 | Chapter 2
Making the State Base Class Reusable

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required