From 9484797ced6bbc7a14a71712987bd77653f96b7d Mon Sep 17 00:00:00 2001 From: Maurice Date: Fri, 12 Dec 2025 21:37:18 +0100 Subject: [PATCH] fixed a pathfinding bug --- src/de/miaurizius/jgame2d/entity/Entity.java | 76 +++++++++----------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/src/de/miaurizius/jgame2d/entity/Entity.java b/src/de/miaurizius/jgame2d/entity/Entity.java index fa3d404..a843e13 100644 --- a/src/de/miaurizius/jgame2d/entity/Entity.java +++ b/src/de/miaurizius/jgame2d/entity/Entity.java @@ -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; } }