I was able to implement the A* pathfinding algorithm to calculate a path from the enemy to player.
After trying to do movement well, I realized I ran into all sorts of problems. I was able to add clearance to the pathfinding, so I'm now going to have sprites of variable size, which leads to even more issues.
I am currently doing movement like - path[0] is first Tile in path and it is removed when you get to it:
if (path.Count > 0) {
if (path[0].isTouching(this))
path.RemoveAt(0);
}
}
if (path [0].getCenterX() > this.getCenterX()) {
speedX = speed;
speedY = 0;
System.Console.WriteLine("moving right");
}
else if (path [0].getCenterX() < this.getCenterX()) {
speedX = -speed;
speedY = 0;
System.Console.WriteLine("moving left");
}
if (path [0].getCenterY() > this.getCenterY()) {
speedX = 0;
speedY = speed;
System.Console.WriteLine("moving down");
}
else if (path [0].getCenterY() < this.getCenterY()) {
speedX = 0;
speedY = -speed;
System.Console.WriteLine("moving up");
}
this.move(speedX, speedY);
This doesn't work very well for a variety of reasons.
I am certain the pathfinding I implemented calculates the proper path of Tiles. Now I think I have to move from a pixel to pixel movement -> tile to tile in order to have this working better. This would probably help, right? How is movement usually done to work with A*? If the tile-based movement would aid, any tips on implementing this?