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

In the output from the program, each time you see Miner Bob change loca
-
tion he is changing state. All the other events are the actions that take place
within the states. We’ll examine each of Miner Bob’s potential states in just
a moment, but for now, let me explain a little about the code structure of
the demo.
The BaseGameEntity Class
All inhabitants of West World are derived from the base class
BaseGameEntity. This is a simple class with a private member for storing an
ID number. It also specifies a pure virtual member function,
Update, that
must be implemented by all subclasses.
Update is a function that gets called
every update step and will be used by subclasses to update their state
machine along with any other data that must be updated each time step.
The
BaseGameEntity class declaration looks like this:
class BaseGameEntity
{
private:
//every entity has a unique identifying number
int m_ID;
//this is the next valid ID. Each time a BaseGameEntity is instantiated
//this value is updated
static int m_iNextValidID;
//this is called within the constructor to make sure the ID is set
//correctly. It verifies that the value passed to the method is greater
//or equal to the next valid ID, before setting the ID and incrementing
//the next valid ID
void SetID(int val);
public:
BaseGameEntity(int id)
{
SetID(id);
}
virtual ~BaseGameEntity(){}
//all entities must implement an update function
virtual void Update()=0;
int ID()const{return m_ID;}
};
For reasons that will become obvious later in the chapter, it’s very impor
-
tant for each entity in your game to have a unique identifier. Therefore, on
instantiation, the ID passed to the constructor is tested in the
SetID method
to make sure it’s unique. If it is not, the program will exit with an assertion
52 | Chapter 2
The West World Project

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