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: ...
Get Procedural Content Generation for C++ Game Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.