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 okfine, but the enemy gets caught on obstacles/solid and solid tiles. theThe Collision.tileCollision
methods check to see if there is/will 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.
//Should/Can go right
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());
}
//Should/Can go left
else if (Collision.tileCollisionLeft(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
== 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());
}
//Should/Can go down
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());
}
//Should/Can go up
else if (Collision.tileCollisionUp(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
== 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
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. So howHow would I go about doing such a thing? By the way, myMy program is tiles based for obstacles such as walls, but entities like the player and enemies have free movement. And thisThis is where I'm confused,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. And I don't want to check every pixel on the screen for a path, because that would take forever, and be extremely inefficient.