Coding the A* pathfinding algorithm
With an understanding of the fundamentals of A*, let's start implementing it in our game. This will allow the enemies to follow our player around the level regardless of its topology.
With a complex algorithm such as this, having a visual representation of what's happening is really helpful. Wherever it's appropriate, we will take a look at a visual representation of what's happening using the following example:
The Tile datatype
Let's start by taking a quick look at the Tile
struct that was defined in Level.h
. As we've seen, a node contains quite a few values. In the implementation, it's the level tiles that will act as nodes. As such, all the information that's required by a node is defined in its type:
// The level tile/node type. struct Tile { TILE type; // The type of tile this is. int columnIndex; // The column index of the tile. int rowIndex; // The row index of the tile. sf::Sprite sprite; // The tile sprite. ...