refactor entity behavior and enhance monster chasing logic

This commit is contained in:
2025-12-13 19:09:05 +01:00
parent 9263626ca2
commit d092a39115
4 changed files with 65 additions and 35 deletions

View File

@@ -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;
}
}
}