O'Reilly logo

Procedural Content Generation for C++ Game Development by Dale Green

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

Implementing A* in the game

Now that we have the function that can calculate the shortest path, we need to incorporate this behavior into the game.

Enabling the enemy to follow a path

We now need to make the enemies follow the vector of target locations that the pathfinding algorithm generates. We need the enemy to constantly follow this path, so we'll override its base classes' Update function, as it's called during every game's tick. The code that will do this is fairly simple; if there is a location in the vector, move towards it at a fixed pace. When the position is reached, we simply remove it from the vector. When the vector is empty, we know that the enemy has reached its goal.

We'll start by adding the function declaration to Enemy.h:

public: ...

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