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

bool AimAt(Vector2D target)const;
//this discharges a projectile from the weapon at the given target position
//(provided the weapon is ready to be discharged... every weapon has its
//own rate of fire)
virtual void ShootAt(Vector2D target) = 0;
//each weapon has its own shape and color
virtual void Render() = 0;
//this method returns a value representing the desirability of using the
//weapon. This is used by the AI to select the most suitable weapon for
//a bot's current situation. This value is calculated using fuzzy logic.
//(Fuzzy logic is covered in Chapter 10)
virtual double GetDesirability(double DistToTarget)=0;
//returns the maximum speed of the projectile this weapon fires
double GetProjectileSpeed()const;
int NumRoundsRemaining()const;
void DecrementNumRounds();
void IncrementRounds(int num);
//returns an enumerated value representing the gun type
unsigned int GetTypeOfGun()const;
};
Both AI and human players use this interface to aim and shoot weapons. If
you are interested in seeing how each weapon type is implemented, please
check out the relevant files in the Raven/Armory folder.
Projectiles
The projectiles (slugs, pellets, rockets, and bolts) fired by the weapons are
derived from the
Raven_Projectile class, which in turn inherits from the
familiar
MovingEntity class. The class hierarchy is shown in Figure 7.2.
Each projectile is modeled as a point mass and obeys real-world physics.
(This is overkill for this type of game, but given that a
MovingEntity class
had already been defined it was a piece of cake to implement.)
When a weapon is fired, an instance of the correct type of projectile is
created and added to
Raven_Game::m_Projectiles. Once the projectile has
impacted (and any animation sequence has terminated) it is removed from
the list. Whenever a projectile intersects a bot, it sends it a message inform
-
ing it of who fired the shot and the amount of damage done.
302 | Chapter 7
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