I am making a Topdown 2d game in SDL2 with C++. It has a tiled map of each tile being 32x32 px in dimensions.
I have used the A* search algorithm so the enemy can find the player in the map. Currently the path is being correctly traced and after performing A*, it returns a stack of SDL_Point
which are just x and y
values on map. But I can't figure out how to make the enemy follow this path slowly overtime rather than just hopping on x and y
points in the stack.
Below is the move function, the move function is constantly called in the main game loop:
void Gunner::move(std::array<Tile*, MAP_LENGTH>& map, double deltaTime) {
// calculate A* path from current pos to player pos
Astar astar(target, this);
stack<SDL_Point> path = astar.astar(map);
if (path.size() != 0) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
int xP = path.top().x;
int yP = path.top().y;
SDL_Rect r = { xP, yP, 32, 32 };
/*
Make the enemy follow path
*/
// debugging purpose
SDL_RenderFillRect(renderer, &r);
path.pop();
}
}
This is the path generated where e is enemy and p is player.