Ahoy!
I’m currently trying to implement A* Pathfinding into my test project. I essentially just need the enemies to follow the player, all while avoiding blocked paths. Here is what I have already:
This is at the top of the enemy class.
//Pathfinding
Tile tile;
private Array<Tile> closedPath = new Array<Tile>();
private Array<Tile> openPath = new Array<Tile>();
This is in the update method.
public void update(float delta){
//Damage Flash
if(flashTick > 0){ flashTick--; c = Color.RED;}
else c = oc;
// ---------- MY CURRENT PATHFINDING CODE ----- //
//Grab Target Position
int dx = (int)(world.getPlayer().getPosition().x - position.x);
int dy = (int)(world.getPlayer().getPosition().y - position.y);
//Grab This Position
int tileX = (int)(position.x);
int tileY = (int)(position.y);
tile = world.getLevel().get(tileX, tileY);
//Handle Tile Closed/Open
//This basically checks all adjacent blocks from the enemies current location.
if(tile.getType() != Type.VOID || tile != null){
for(int x = tileX - 1; x <= tileX + 1; x++){
for(int y = tileY - 1; y <= tileY + 1; y++){
if(world.getLevel().get(x, y).getType() != Type.FLOOR)
closedPath.add(world.getLevel().get(x, y));
else
openPath.add(world.getLevel().get(x, y));
}
}
}
// ---------- NO LONGER PART OF MY CURRENT PATHFINDING CODE ----- //
//velocity.x = dx;
//velocity.y = dy;
//Velocity Clamp
if(velocity.x > SPEED) velocity.x = SPEED;
if(velocity.x < -SPEED) velocity.x = -SPEED;
if(velocity.y > SPEED) velocity.y = SPEED;
if(velocity.y < -SPEED) velocity.y = -SPEED;
//Velocity
velocity.scl(delta);
//Collisions
blockCollision(delta);
entityCollision(delta);
//Add velocity to position
position.add(velocity);
}
I am not quite sure what to do from here. I’ve been reading a lot of documentation and watching a lot of presentation type videos. Currently looking at this one, http://www.policyalmanac.org/games/aStarTutorial.htm - and I’ve looked at the coke and code one.
Where do I go from here? I’m a bit confused and would very much appreciate some input.
Thank you all!
-A