Im having trouble making the enemy walking intelligence, what road to take to reach the target.
So even though i have try this for 3 days, i have no clue what to do now.
Say a enemy have a target. and the enemy should aproach it. but i want the enemy to take the right and shorter road, considering collisions. i have write and erased many code,right now whats left is this...
Vector2 destPos = findPosibleDestination(); //find the nearest posible destination with no collision
boolean hasToMoveRight = (int)destPos.x > (int)pos.x;
boolean hasToMoveLeft = (int)destPos.x < (int)pos.x;
boolean hasToMoveUp = (int)destPos.y > (int)pos.y;
boolean hasToMoveDown = (int)destPos.y < (int)pos.y;
//and here it goes the craziness i did xD
//where each move returns true if the current movement has no collision or false if has collision and doesnt move
if(hasToMoveRight){
if(!moveRight()){
if(!moveUp()){
if(!moveDown()){
moveLeft();
}
}
}
}
else if(hasToMoveLeft){
if(!moveLeft()){
if(!moveDown()){
if(!moveUp()){
moveRight();
}
}
}
}
else if(hasToMoveUp){
if(!moveUp()){
if(!moveRight()){
if(!moveLeft()){
moveDown();
}
}
}
}
else if(hasToMoveDown){
if(!moveDown()){
if(!moveLeft()){
if(!moveRight()){
moveDown();
}
}
}
}
Now im pretty sure im far from the real thing here, i tried to make a walking track with an Array of Vector2 first but failed.
What should i try next?
Thank you.