refactor entity behavior and enhance monster chasing logic
This commit is contained in:
@@ -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,32,12);i++;
|
||||||
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,33,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,25,27);i++;
|
||||||
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,26,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,27);i++;
|
||||||
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,27,28);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,27,29);i++;
|
||||||
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,28,29);i++;
|
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,27,30);i++;
|
||||||
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,29,29);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,18,40);i++;
|
||||||
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,17,40);i++;
|
panel.iTile[Map.OVERWORLD.getIndex()][i] = new DryTreeTI(panel,17,40);i++;
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import java.awt.image.BufferedImage;
|
|||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class Entity {
|
public class Entity {
|
||||||
@@ -47,6 +48,7 @@ public class Entity {
|
|||||||
public int spriteCount;
|
public int spriteCount;
|
||||||
public int actionLock;
|
public int actionLock;
|
||||||
public int invincibleCount;
|
public int invincibleCount;
|
||||||
|
public int shotAvailableCount;
|
||||||
int dyingCount;
|
int dyingCount;
|
||||||
int hpBarCount;
|
int hpBarCount;
|
||||||
int knockbackCount;
|
int knockbackCount;
|
||||||
@@ -339,6 +341,21 @@ public class Entity {
|
|||||||
public int getRow() {
|
public int getRow() {
|
||||||
return (worldY + solidArea.y) / panel.tileSize;
|
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
|
// SETTING THINGS UP
|
||||||
BufferedImage parseSprite() {
|
BufferedImage parseSprite() {
|
||||||
@@ -417,7 +434,10 @@ public class Entity {
|
|||||||
}
|
}
|
||||||
} else onPath = false;
|
} 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 startCol = (worldX + solidArea.x) / panel.tileSize;
|
||||||
int startRow = (worldY + solidArea.y) / 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package de.miaurizius.jgame2d.entity.monster;
|
package de.miaurizius.jgame2d.entity.monster;
|
||||||
|
|
||||||
import de.miaurizius.jgame2d.core.GamePanel;
|
import de.miaurizius.jgame2d.core.GamePanel;
|
||||||
import de.miaurizius.jgame2d.core.enums.Direction;
|
|
||||||
import de.miaurizius.jgame2d.core.enums.EntityType;
|
import de.miaurizius.jgame2d.core.enums.EntityType;
|
||||||
import de.miaurizius.jgame2d.entity.Entity;
|
import de.miaurizius.jgame2d.entity.Entity;
|
||||||
import de.miaurizius.jgame2d.entity.item.CoinObj;
|
import de.miaurizius.jgame2d.entity.item.CoinObj;
|
||||||
@@ -36,41 +35,16 @@ public class GreenSlimeMON extends Entity {
|
|||||||
getImage();
|
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
|
// INTERACTION
|
||||||
public void setAction() {
|
public void setAction() {
|
||||||
|
if(!onPath) checkStartChasing(panel.player, 5 ,100);
|
||||||
|
checkStopChasing(panel.player, 10, 100);
|
||||||
if(onPath) {
|
if(onPath) {
|
||||||
searchPath((panel.player.worldX+panel.player.solidArea.x)/panel.tileSize, (panel.player.worldY+panel.player.solidArea.y)/panel.tileSize);
|
followPlayer();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
setRandomDirection();
|
||||||
actionLock++;
|
checkShooting(200, 30);
|
||||||
|
|
||||||
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);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
public void damageReaction() {
|
public void damageReaction() {
|
||||||
actionLock = 0;
|
actionLock = 0;
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ public class MiniMap extends TileManager {
|
|||||||
col = 0;
|
col = 0;
|
||||||
row++;
|
row++;
|
||||||
}
|
}
|
||||||
|
g2.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user