diff --git a/src/de/miaurizius/jgame2d/entity/Entity.java b/src/de/miaurizius/jgame2d/entity/Entity.java index 774e11a..b68bef9 100644 --- a/src/de/miaurizius/jgame2d/entity/Entity.java +++ b/src/de/miaurizius/jgame2d/entity/Entity.java @@ -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; + } + } + } } diff --git a/src/de/miaurizius/jgame2d/entity/monster/GreenSlimeMON.java b/src/de/miaurizius/jgame2d/entity/monster/GreenSlimeMON.java index 653ec1e..9b8bdb5 100644 --- a/src/de/miaurizius/jgame2d/entity/monster/GreenSlimeMON.java +++ b/src/de/miaurizius/jgame2d/entity/monster/GreenSlimeMON.java @@ -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; diff --git a/src/de/miaurizius/jgame2d/entity/npc/OldManNPC.java b/src/de/miaurizius/jgame2d/entity/npc/OldManNPC.java index 68b0513..57bbf13 100644 --- a/src/de/miaurizius/jgame2d/entity/npc/OldManNPC.java +++ b/src/de/miaurizius/jgame2d/entity/npc/OldManNPC.java @@ -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 {