added path finding algorithm

This commit is contained in:
2025-12-12 21:33:52 +01:00
parent a89838eb38
commit 4ab53ceab9
5 changed files with 249 additions and 10 deletions

View File

@@ -40,6 +40,7 @@ public class Entity {
public boolean dying;
public boolean hpBarOn;
public boolean consumable;
public boolean onPath;
// COUNTER
public int spriteCounter;
@@ -86,15 +87,7 @@ public class Entity {
// DEFAULT
public void update() {
setAction();
collisionOn = false;
panel.collisionH.checkTile(this);
panel.collisionH.checkObject(this, false);
panel.collisionH.checkEntity(this, panel.npc[panel.currentMap.getIndex()]);
panel.collisionH.checkEntity(this, panel.monster[panel.currentMap.getIndex()]);
panel.collisionH.checkEntity(this, panel.iTile[panel.currentMap.getIndex()]);
boolean contactPlayer = panel.collisionH.checkPlayer(this);
if(this.type == EntityType.MONSTER && contactPlayer) damagePlayer(attack);
checkCollision();
if(!collisionOn) {
switch (direction) {
@@ -207,6 +200,17 @@ public class Entity {
} //If entity is consumable
public void checkDrop() {
}
public void checkCollision() {
collisionOn = false;
panel.collisionH.checkTile(this);
panel.collisionH.checkObject(this, false);
panel.collisionH.checkEntity(this, panel.npc[panel.currentMap.getIndex()]);
panel.collisionH.checkEntity(this, panel.monster[panel.currentMap.getIndex()]);
panel.collisionH.checkEntity(this, panel.iTile[panel.currentMap.getIndex()]);
boolean contactPlayer = panel.collisionH.checkPlayer(this);
if(this.type == EntityType.MONSTER && contactPlayer) damagePlayer(attack);
}
public void dropItem(Entity droppedItem) {
for(int i = 0; i < panel.obj[panel.currentMap.getIndex()].length; i++) {
@@ -284,5 +288,56 @@ public class Entity {
public void changeOpacity(Graphics2D graphics2d, float opacity) {
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;
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(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;
}
}
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;
}
int nextCol = panel.pFinder.pathList.getFirst().col;
int nextRow = panel.pFinder.pathList.getFirst().row;
if(nextCol == goalCol && nextRow == goalRow) onPath = false;
}
}

View File

@@ -18,9 +18,22 @@ public class OldManNPC extends Entity {
speed = 1;
getImage();
setDialogue();
solidArea.x = 8;
solidArea.y = 16;
solidAreaDefaultX = solidArea.x;
solidAreaDefaultY = solidArea.y;
solidArea.width = 30;
solidArea.height = 30;
}
public void setAction() {
if(onPath) {
searchPath(12, 9);
return;
}
actionLock++;
if(actionLock != 120) return; //lock action for x frames
Random rand = new Random();
@@ -30,6 +43,12 @@ public class OldManNPC extends Entity {
if(i > 50 && i <= 75) direction = Direction.LEFT;
if(i > 75) direction = Direction.RIGHT;
actionLock = 0;
}
@Override
public void speak() {
super.speak();
super.onPath = true;
}
// SETTING THINGS UP