The enemies in my game are supposed to follow the player in order to cause damage to it. Right now, I have it where the enemy just goes in the direction that will bring it closer to the player, and this works fine, but the enemy gets caught on obstacles and solid tiles. The Collision.tileCollision
methods check to see if there is or will be a collision with any solid tiles, by adding the speed of the enemy to its current coordinate, in order to look ahead for collisions. If there is no collision, and the direction its checking will bring the enemy closer to the player, then it goes in that direction.
if (Collision.tileCollisionRight(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel)
== false && (Math.abs((this.thisEnemy.getX() + this.thisEnemy.getSpeed())
- this.player.getX()) < Collision.getXDist(this.thisEnemy, this.player)))
{
//Should/Can go right
this.thisEnemy.setX(this.thisEnemy.getX() + this.thisEnemy.getSpeed());
}
else if (Collision.tileCollisionLeft(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel)
== false && (Math.abs((this.thisEnemy.getX() - this.thisEnemy.getSpeed())
- this.player.getX())) < Collision.getXDist(this.thisEnemy, this.player))
{
//Should/Can go left
this.thisEnemy.setX(this.thisEnemy.getX() - this.thisEnemy.getSpeed());
}
if (Collision.tileCollisionDown(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel)
== false && (Math.abs((this.thisEnemy.getY() + this.thisEnemy.getSpeed())
- this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
{
//Should/Can go down
this.thisEnemy.setY(this.thisEnemy.getY() + this.thisEnemy.getSpeed());
}
else if (Collision.tileCollisionUp(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel)
== false && (Math.abs((this.thisEnemy.getY() - this.thisEnemy.getSpeed())
- this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
{
//Should/Can go up
this.thisEnemy.setY(this.thisEnemy.getY() - this.thisEnemy.getSpeed());
}
I have heard that the A* search algorithm is good for path finding, but I have no idea how to implement the algorithm into my program in order for the enemies to not get stuck on walls when following the player. How would I go about doing such a thing? My program is tiles based for obstacles such as walls, but entities like the player and enemies have free movement. This is where I'm confused; I understand how it would work, if the entities also did tile based movements, but that is not how my game is set up. I don't want to check every pixel on the screen for a path, because that would take forever, and be extremely inefficient.
Node
Object, instead of storing the weights in Maps. \$\endgroup\$