fixed a pathfinding bug

This commit is contained in:
2025-12-12 21:37:18 +01:00
parent 4ab53ceab9
commit 9484797ced

View File

@@ -289,55 +289,45 @@ public class Entity {
graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity)); graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
} }
public void searchPath(int goalCol, int goalRow) { public void searchPath(int goalCol, int goalRow) {
int startCol = (worldX + solidArea.x)/panel.tileSize; int startCol = (worldX + solidArea.x) / panel.tileSize;
int startRow = (worldY + solidArea.y)/panel.tileSize; int startRow = (worldY + solidArea.y) / panel.tileSize;
panel.pFinder.setNodes(startCol, startRow, goalCol, goalRow); 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; if (panel.pFinder.search()) {
int enRightX = worldX + solidArea.x + solidArea.width; int nextCol = panel.pFinder.pathList.getFirst().col;
int enTopY = worldY + solidArea.y; int nextRow = panel.pFinder.pathList.getFirst().row;
int enBottomY = worldY + solidArea.y + solidArea.height;
if(enTopY > nextY && enLeftX >= nextX && enRightX < nextX + panel.tileSize) direction = Direction.UP; int enLeftX = worldX + solidArea.x;
if(enTopY < nextY && enLeftX >= nextX && enRightX < nextX + panel.tileSize) direction = Direction.DOWN; int enTopY = worldY + solidArea.y;
if(enTopY >= nextY && enBottomY < nextY + panel.tileSize) {
if(enLeftX > nextX) { int targetX = nextCol * panel.tileSize;
direction = Direction.LEFT; int targetY = nextRow * panel.tileSize;
} else if(enLeftX < nextX) {
direction = Direction.RIGHT; // UP
if (nextRow < startRow) {
if (enLeftX < targetX) direction = Direction.RIGHT;
else if (enLeftX > targetX) direction = Direction.LEFT;
else direction = Direction.UP;
} }
} // DOWN
if (enTopY > nextY && enLeftX > nextX) { else if (nextRow > startRow) {
direction = Direction.UP; if (enLeftX < targetX) direction = Direction.RIGHT;
checkCollision(); else if (enLeftX > targetX) direction = Direction.LEFT;
if(collision) direction = Direction.LEFT; else direction = Direction.DOWN;
}
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;
}
int nextCol = panel.pFinder.pathList.getFirst().col; }
int nextRow = panel.pFinder.pathList.getFirst().row; else if (nextCol < startCol) { // DOWN LEFT
if(nextCol == goalCol && nextRow == goalRow) onPath = false; 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;
} }
} }