slimes can now get aggro and follow you (and stop following you if you ran away)

This commit is contained in:
2025-12-12 21:58:41 +01:00
parent c02ae0302c
commit 2e73a012d1
3 changed files with 59 additions and 1 deletions

View File

@@ -334,5 +334,46 @@ public class Entity {
}
} else onPath = false;
}
public void followPlayer(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()) {
int nextCol = panel.pFinder.pathList.getFirst().col;
int nextRow = panel.pFinder.pathList.getFirst().row;
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;
}
// DOWN
else if (nextRow > startRow) {
if (enLeftX < targetX) direction = Direction.RIGHT;
else if (enLeftX > targetX) direction = Direction.LEFT;
else direction = Direction.DOWN;
}
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;
}
}
}
}

View File

@@ -35,8 +35,24 @@ public class GreenSlimeMON extends Entity {
getImage();
}
@Override
public void update() {
super.update();
int dx = Math.abs(worldX - panel.player.worldX);
int dy = Math.abs(worldY - panel.player.worldY);
int dTile = (dx+dy)/panel.tileSize;
if(!onPath && dTile < 5) if(new Random().nextInt(100)+1 > 50) onPath = true;
if(onPath && dTile > 20) onPath = false;
}
// INTERACTION
public void setAction() {
if(onPath) {
searchPath((panel.player.worldX+panel.player.solidArea.x)/panel.tileSize, (panel.player.worldY+panel.player.solidArea.y)/panel.tileSize);
return;
}
actionLock++;
if(actionLock == 120) { //lock action for x frames
@@ -57,7 +73,7 @@ public class GreenSlimeMON extends Entity {
}
public void damageReaction() {
actionLock = 0;
direction = panel.player.direction;
onPath = true;
}
public void checkDrop() {
int i = new Random().nextInt(100)+1;

View File

@@ -5,6 +5,7 @@ import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.enums.EntityType;
import de.miaurizius.jgame2d.entity.Entity;
import java.awt.*;
import java.util.Random;
public class OldManNPC extends Entity {