From d092a391156ecd3cb49d5f470b0055899cdddcd1 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sat, 13 Dec 2025 19:09:05 +0100 Subject: [PATCH] refactor entity behavior and enhance monster chasing logic --- .../jgame2d/core/handlers/AssetSetter.java | 9 ++-- src/de/miaurizius/jgame2d/entity/Entity.java | 54 ++++++++++++++++++- .../jgame2d/entity/monster/GreenSlimeMON.java | 36 ++----------- src/de/miaurizius/jgame2d/tile/MiniMap.java | 1 + 4 files changed, 65 insertions(+), 35 deletions(-) diff --git a/src/de/miaurizius/jgame2d/core/handlers/AssetSetter.java b/src/de/miaurizius/jgame2d/core/handlers/AssetSetter.java index f26562b..1872040 100644 --- a/src/de/miaurizius/jgame2d/core/handlers/AssetSetter.java +++ b/src/de/miaurizius/jgame2d/core/handlers/AssetSetter.java @@ -88,15 +88,18 @@ public class AssetSetter { panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,32,12);i++; panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,33,12);i++; - panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,29,21);i++; + panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,30,21);i++; panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,25,27);i++; panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,26,27);i++; panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,27,27);i++; panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,27,28);i++; panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,27,29);i++; - panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,28,29);i++; - panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,29,29);i++; + panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,27,30);i++; + panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,27,31);i++; + panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,28,31);i++; + panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,29,31);i++; + panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,30,31);i++; panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,18,40);i++; panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,17,40);i++; diff --git a/src/de/miaurizius/jgame2d/entity/Entity.java b/src/de/miaurizius/jgame2d/entity/Entity.java index e572d12..633f209 100644 --- a/src/de/miaurizius/jgame2d/entity/Entity.java +++ b/src/de/miaurizius/jgame2d/entity/Entity.java @@ -14,6 +14,7 @@ import java.awt.image.BufferedImage; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Random; import java.util.logging.Level; public class Entity { @@ -47,6 +48,7 @@ public class Entity { public int spriteCount; public int actionLock; public int invincibleCount; + public int shotAvailableCount; int dyingCount; int hpBarCount; int knockbackCount; @@ -339,6 +341,21 @@ public class Entity { public int getRow() { return (worldY + solidArea.y) / panel.tileSize; } + public int dX(Entity target) { + return Math.abs(worldX - target.worldX); + } + public int dY(Entity target) { + return Math.abs(worldY - target.worldX); + } + public int dTile(Entity target) { + return (dX(target) + dY(target)) / panel.tileSize; + } + public int getGoalCol(Entity target) { + return (target.worldX+target.solidArea.x)/panel.tileSize; + } + public int getGoalRow(Entity target) { + return (target.worldY+target.solidArea.y)/panel.tileSize; + } // SETTING THINGS UP BufferedImage parseSprite() { @@ -417,7 +434,10 @@ public class Entity { } } else onPath = false; } - public void followPlayer(int goalCol, int goalRow) { + public void followPlayer() { + int goalCol = panel.player.getCol(); + int goalRow = panel.player.getRow(); + int startCol = (worldX + solidArea.x) / panel.tileSize; int startRow = (worldY + solidArea.y) / panel.tileSize; @@ -458,5 +478,37 @@ public class Entity { } } } + public void checkStopChasing(Entity target, int distance, int rate) { + if(dTile(target) > distance) if(new Random().nextInt(rate) == 0) onPath = false; + } + public void checkStartChasing(Entity target, int distance, int rate) { + if(dTile(target) < distance) if(new Random().nextInt(rate) == 0) onPath = true; + } + public void checkShooting(int rate, int shotInterval) { + if(new Random().nextInt(rate) == 0 && projectile.alive == false && shotAvailableCount == shotInterval) { + projectile.set(worldX, worldY, direction, true, this); + + // CHECK VACANCY + for(int ii = 0; ii < panel.projectileList.size(); ii++) { + if(panel.projectileList.get(ii) == null) { + panel.projectileList.set(ii, projectile); + break; + } + } + shotAvailableCount = 0; + } + } + public void setRandomDirection() { + actionLock++; + if(actionLock == 120) { //lock action for x frames + Random rand = new Random(); + int i = rand.nextInt(100)+1; //Generate number between 1 and 100 + if(i <= 25) direction = Direction.UP; + if(i > 25 && i <= 50) direction = Direction.DOWN; + if(i > 50 && i <= 75) direction = Direction.LEFT; + if(i > 75) direction = Direction.RIGHT; + actionLock = 0; + } + } } diff --git a/src/de/miaurizius/jgame2d/entity/monster/GreenSlimeMON.java b/src/de/miaurizius/jgame2d/entity/monster/GreenSlimeMON.java index dcf212d..32255b8 100644 --- a/src/de/miaurizius/jgame2d/entity/monster/GreenSlimeMON.java +++ b/src/de/miaurizius/jgame2d/entity/monster/GreenSlimeMON.java @@ -1,7 +1,6 @@ package de.miaurizius.jgame2d.entity.monster; import de.miaurizius.jgame2d.core.GamePanel; -import de.miaurizius.jgame2d.core.enums.Direction; import de.miaurizius.jgame2d.core.enums.EntityType; import de.miaurizius.jgame2d.entity.Entity; import de.miaurizius.jgame2d.entity.item.CoinObj; @@ -36,41 +35,16 @@ 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) checkStartChasing(panel.player, 5 ,100); + checkStopChasing(panel.player, 10, 100); if(onPath) { - searchPath((panel.player.worldX+panel.player.solidArea.x)/panel.tileSize, (panel.player.worldY+panel.player.solidArea.y)/panel.tileSize); + followPlayer(); return; } - - actionLock++; - - if(actionLock == 120) { //lock action for x frames - Random rand = new Random(); - int i = rand.nextInt(100)+1; //Generate number between 1 and 100 - if(i <= 25) direction = Direction.UP; - if(i > 25 && i <= 50) direction = Direction.DOWN; - if(i > 50 && i <= 75) direction = Direction.LEFT; - if(i > 75) direction = Direction.RIGHT; - actionLock = 0; - } - -// int i = new Random().nextInt(100)+1; -// if(i > 99 && !projectile.alive) { -// projectile.set(worldX, worldY, direction, true, this); -// panel.projectileList.add(projectile); -// } + setRandomDirection(); + checkShooting(200, 30); } public void damageReaction() { actionLock = 0; diff --git a/src/de/miaurizius/jgame2d/tile/MiniMap.java b/src/de/miaurizius/jgame2d/tile/MiniMap.java index b86340a..f0a12e9 100644 --- a/src/de/miaurizius/jgame2d/tile/MiniMap.java +++ b/src/de/miaurizius/jgame2d/tile/MiniMap.java @@ -41,6 +41,7 @@ public class MiniMap extends TileManager { col = 0; row++; } + g2.dispose(); } }