fixed a pathfinding bug
This commit is contained in:
@@ -289,55 +289,45 @@ public class Entity {
|
||||
graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
|
||||
}
|
||||
public void searchPath(int goalCol, int goalRow) {
|
||||
int startCol = (worldX + solidArea.x)/panel.tileSize;
|
||||
int startRow = (worldY + solidArea.y)/panel.tileSize;
|
||||
int startCol = (worldX + solidArea.x) / panel.tileSize;
|
||||
int startRow = (worldY + solidArea.y) / panel.tileSize;
|
||||
|
||||
panel.pFinder.setNodes(startCol, startRow, goalCol, goalRow);
|
||||
if(!panel.pFinder.search()) {
|
||||
onPath = false;
|
||||
return;
|
||||
}
|
||||
int nextX = panel.pFinder.pathList.getFirst().col * panel.tileSize;
|
||||
int nextY = panel.pFinder.pathList.getFirst().row * panel.tileSize;
|
||||
|
||||
int enLeftX = worldX + solidArea.x;
|
||||
int enRightX = worldX + solidArea.x + solidArea.width;
|
||||
int enTopY = worldY + solidArea.y;
|
||||
int enBottomY = worldY + solidArea.y + solidArea.height;
|
||||
if (panel.pFinder.search()) {
|
||||
int nextCol = panel.pFinder.pathList.getFirst().col;
|
||||
int nextRow = panel.pFinder.pathList.getFirst().row;
|
||||
|
||||
if(enTopY > nextY && enLeftX >= nextX && enRightX < nextX + panel.tileSize) direction = Direction.UP;
|
||||
if(enTopY < nextY && enLeftX >= nextX && enRightX < nextX + panel.tileSize) direction = Direction.DOWN;
|
||||
if(enTopY >= nextY && enBottomY < nextY + panel.tileSize) {
|
||||
if(enLeftX > nextX) {
|
||||
direction = Direction.LEFT;
|
||||
} else if(enLeftX < nextX) {
|
||||
direction = Direction.RIGHT;
|
||||
int enLeftX = worldX + solidArea.x;
|
||||
int enTopY = worldY + solidArea.y;
|
||||
|
||||
int targetX = nextCol * panel.tileSize;
|
||||
int targetY = nextRow * panel.tileSize;
|
||||
|
||||
// UP
|
||||
if (nextRow < startRow) {
|
||||
if (enLeftX < targetX) direction = Direction.RIGHT;
|
||||
else if (enLeftX > targetX) direction = Direction.LEFT;
|
||||
else direction = Direction.UP;
|
||||
}
|
||||
}
|
||||
if (enTopY > nextY && enLeftX > nextX) {
|
||||
direction = Direction.UP;
|
||||
checkCollision();
|
||||
if(collision) direction = Direction.LEFT;
|
||||
}
|
||||
if(enTopY > nextY && enLeftX < nextX) {
|
||||
direction = Direction.UP;
|
||||
checkCollision();
|
||||
if(collision) direction = Direction.RIGHT;
|
||||
}
|
||||
if(enTopY < nextY && enLeftX > nextX) {
|
||||
direction = Direction.DOWN;
|
||||
checkCollision();
|
||||
if(collision) direction = Direction.LEFT;
|
||||
}
|
||||
if(enTopY < nextY && enLeftX < nextX) {
|
||||
direction = Direction.DOWN;
|
||||
checkCollision();
|
||||
if(collision) direction = Direction.RIGHT;
|
||||
}
|
||||
// DOWN
|
||||
else if (nextRow > startRow) {
|
||||
if (enLeftX < targetX) direction = Direction.RIGHT;
|
||||
else if (enLeftX > targetX) direction = Direction.LEFT;
|
||||
else direction = Direction.DOWN;
|
||||
|
||||
int nextCol = panel.pFinder.pathList.getFirst().col;
|
||||
int nextRow = panel.pFinder.pathList.getFirst().row;
|
||||
if(nextCol == goalCol && nextRow == goalRow) onPath = false;
|
||||
}
|
||||
else if (nextCol < startCol) { // DOWN LEFT
|
||||
if (enTopY < targetY) direction = Direction.DOWN;
|
||||
else if (enTopY > targetY) direction = Direction.UP;
|
||||
else direction = Direction.LEFT;
|
||||
}
|
||||
else if (nextCol > startCol) { // RIGHT
|
||||
if (enTopY < targetY) direction = Direction.DOWN;
|
||||
else if (enTopY > targetY) direction = Direction.UP;
|
||||
else direction = Direction.RIGHT;
|
||||
}
|
||||
} else onPath = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user