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

Triggers
A trigger is an object that defines a condition, which, when satisfied by an
agent, generates an action (it is triggered). Many of the triggers utilized in
commercial games have the property that they are triggered when a game
entity enters a trigger region: a predefined region of space that is attached
to the trigger. These regions can be any arbitrary shape but are usually cir-
cular or rectangular for 2D environments and spherical, cubic, or
cylindrical for 3D environments.
Triggers are a wonderfully useful tool for both game designers and AI
programmers. You can use them to create all sorts of events and behaviors.
For instance, triggers make doing stuff like this easy:
n
A game character wanders down a gloomy corridor. It steps onto a
pressure-sensitive plate and triggers a mechanism that rams forty
pointy sticks through its respiratory cavity. (This is one of the most
obvious uses for a trigger.)
n
You shoot a guard. When it dies a trigger is added to the game that
alerts other guards to the body if they wander within a specified dis
-
tance of it.
n
A game character shoots its gun. A trigger is added to the game that
alerts any other character within a specified radius to the noise.
n
A lever on a wall is implemented as a trigger. If an agent pulls it, it
opens a door.
n
You’ve implemented a puzzle in one corner of a room but you think
a few players will have difficulty solving it. As an aid, you can
Raven: An Overview | 303
Overview of the Game Architecture
Figure 7.2. UML class diagram showing the Raven projectile hierarchy
attach a trigger to the puzzle that activates if the player stands near it
more than three times. When activated, the trigger flashes up some
kind of hint system to help the player solve the puzzle.
n
A troll whacks an ogre on its head with a spiky stick. The ogre runs
off but is bleeding. As each drop of blood falls to the ground it
leaves a trigger. The troll can then chase the ogre by following the
trail of blood.
Raven makes use of several types of triggers. The class hierarchy is given
in Figure 7.3.
It’s worthwhile spending some time examining each of these objects in
detail. First let’s take a quick look at the
TriggerRegion class.
TriggerRegion
The TriggerRegion class defines a method isTouching that all trigger
regions must implement.
isTouching returns true if an entity of the given
size and position is overlapping the trigger region. Each trigger type owns
304 | Chapter 7
Overview of the Game Architecture
Figure 7.3. The Trigger class hierarchy
an instance of a TriggerRegion and utilizes the isTouching method to deter
-
mine when it should be triggered.
Here is its declaration:
class TriggerRegion
{
public:
virtual ~TriggerRegion(){}
virtual bool isTouching(Vector2D EntityPos, double EntityRadius)const = 0;
};
And here is an example of a concrete trigger region that defines a circular
region of space:
class TriggerRegion_Circle : public TriggerRegion
{
private:
//the center of the region
Vector2D m_vPos;
//the radius of the region
double m_dRadius;
public:
TriggerRegion_Circle(Vector2D pos,
double radius):m_dRadius(radius),
m_vPos(pos)
{}
bool isTouching(Vector2D pos, double EntityRadius)const
{
//distances calculated in squared-distance space
return Vec2DDistanceSq(m_vPos, pos) <
(EntityRadius + m_dRadius)*(EntityRadius + m_dRadius);
}
};
As you can see, the method isTouching will return true as soon as the entity
overlaps with the circle defined by the region.
Trigger
The Trigger class is a base class from which all other trigger types are
derived. It has two methods that must be implemented by all child classes:
Try and Update. These methods are called each iteration of the game’s
update loop.
Update updates a triggers internal state (if any). Try tests if the
entity passed to it as a parameter is overlapping the trigger region and takes
action appropriately.
Raven: An Overview | 305
Overview of the Game Architecture

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