add Orc monster implementation and enhance player-monster interaction

This commit is contained in:
2025-12-13 20:21:28 +01:00
parent d092a39115
commit 4f9d1dfcb6
7 changed files with 195 additions and 62 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.Objects;
import java.util.Random;
import java.util.logging.Level;
@@ -25,6 +26,7 @@ public class Entity {
public BufferedImage image, image2, image3;
public Rectangle solidArea = new Rectangle(0, 0, 48, 48);
public Rectangle attackArea = new Rectangle(0, 0, 0, 0);
public Entity attacker;
public int solidAreaDefaultX, solidAreaDefaultY;
public boolean collision;
protected String[] dialogue = new String[20];
@@ -36,13 +38,14 @@ public class Entity {
int dialogueIndex;
public boolean collisionOn;
public boolean invincible;
boolean attacking;
public boolean attacking;
public boolean alive = true;
public boolean dying;
public boolean hpBarOn;
public boolean consumable;
public boolean onPath;
public boolean knockback;
public Direction knockbackDirection;
// COUNTER
public int spriteCount;
@@ -105,7 +108,7 @@ public class Entity {
return;
}
switch(panel.player.direction) {
switch(knockbackDirection) {
case UP -> worldY -= speed;
case DOWN -> worldY += speed;
case LEFT ->worldX -= speed;
@@ -118,7 +121,7 @@ public class Entity {
knockback = false;
knockbackCount = 0;
speed = defaultSpeed;
} else {
} else if(attacking) attacking(); else {
setAction();
checkCollision();
@@ -130,14 +133,14 @@ public class Entity {
case RIGHT -> worldX += speed;
}
}
}
spriteCount++;
if(spriteCount > 24) {
if(spriteNum == 1) spriteNum = 2;
else if(spriteNum == 2) spriteNum = 1;
else spriteNum = 0;
spriteCount = 0;
spriteCount++;
if(spriteCount > 24) {
if(spriteNum == 1) spriteNum = 2;
else if(spriteNum == 2) spriteNum = 1;
else spriteNum = 0;
spriteCount = 0;
}
}
// INVINCIBLE COUNTER
@@ -179,8 +182,9 @@ public class Entity {
hpBarCount = 0;
changeOpacity(graphics2d, 0.4f);
}
//TODO: fix attacking sprite drawing position for monsters
if(dying) dyingAnimation(graphics2d);
if(type == EntityType.PLAYER) { // only modify sprite render position for player because I dont know yet how monster attack sprite are gonna look
if(type == EntityType.PLAYER || name.equals("orc")) { // only modify sprite render position for player because I dont know yet how monster attack sprite are gonna look
if(attacking) graphics2d.drawImage(parseSpriteATK(),
(direction == Direction.LEFT) ? screenX - panel.tileSize : screenX,
(direction == Direction.UP) ? screenY - panel.tileSize : screenY, null);
@@ -199,6 +203,48 @@ public class Entity {
// INTERACTION
public void setAction() {}
public void damageReaction() {}
public void attacking() {
if(panel.player.attackCancel && type == EntityType.PLAYER) return;
spriteCount++;
if(spriteCount <= 5) spriteNum = 1;
if(spriteCount > 5 && spriteCount <= 25) {
spriteNum = 2;
int currentWorldX = worldX;
int currentWorldY = worldY;
int solidAreaWidth = solidArea.width;
int solidAreaHeight = solidArea.height;
switch(direction) {
case UP -> worldY -= attackArea.height;
case DOWN -> worldY += attackArea.height;
case LEFT -> worldX -= attackArea.width;
case RIGHT -> worldX += attackArea.width;
}
solidArea.width = attackArea.width;
solidArea.height = attackArea.height;
if(type == EntityType.MONSTER) if(panel.collisionH.checkPlayer(this)) damagePlayer(attackValue);
if(type == EntityType.PLAYER) {
int monsterIndex = panel.collisionH.checkEntity(this, panel.monster[panel.currentMap.getIndex()]);
panel.player.damageMonster(monsterIndex, this, attack, currentWeapon.knockbackVal);
int iTileIndex = panel.collisionH.checkEntity(this, panel.iTile[panel.currentMap.getIndex()]);
panel.player.interactTile(iTileIndex);
}
worldX = currentWorldX;
worldY = currentWorldY;
solidArea.width = solidAreaWidth;
solidArea.height = solidAreaHeight;
}
if(spriteCount > 25) {
spriteNum = 1;
spriteCount = 0;
attacking = false;
}
}
public void damagePlayer(int attack) {
if(panel.player.invincible) return;
panel.playSE(6);
@@ -292,6 +338,12 @@ public class Entity {
}
return index;
}
public void setKnockback(Entity target, Entity attacker, int knockbackVal) {
this.attacker = attacker;
target.knockbackDirection = attacker.direction;
target.speed += knockbackVal;
target.knockback = true;
}
// PARTICLE SETUP
public Color getParticleColor() {
@@ -345,9 +397,10 @@ public class Entity {
return Math.abs(worldX - target.worldX);
}
public int dY(Entity target) {
return Math.abs(worldY - target.worldX);
return Math.abs(worldY - target.worldY);
}
public int dTile(Entity target) {
if(Objects.equals(name, "orc")) System.out.println("dX: " + dX(target) + " dY: " + dY(target));
return (dX(target) + dY(target)) / panel.tileSize;
}
public int getGoalCol(Entity target) {
@@ -479,10 +532,11 @@ public class Entity {
}
}
public void checkStopChasing(Entity target, int distance, int rate) {
if(dTile(target) > distance) if(new Random().nextInt(rate) == 0) onPath = false;
if(Objects.equals(name, "orc")) System.out.println("dTile: " + dTile(target) + " distance: " + distance);
if(dTile(target) > distance) onPath = false;
}
public void checkStartChasing(Entity target, int distance, int rate) {
if(dTile(target) < distance) if(new Random().nextInt(rate) == 0) onPath = true;
if(dTile(target) < distance) onPath = true;
}
public void checkShooting(int rate, int shotInterval) {
if(new Random().nextInt(rate) == 0 && projectile.alive == false && shotAvailableCount == shotInterval) {
@@ -498,6 +552,34 @@ public class Entity {
shotAvailableCount = 0;
}
}
public void checkAttack(int rate, int straight, int horizontal) {
boolean targetInRange = false;
int xDist = dX(panel.player);
int yDist = dY(panel.player);
switch(direction) {
case UP -> {
if(panel.player.worldY < worldY && yDist < straight && xDist < horizontal) targetInRange = true;
}
case DOWN -> {
if(panel.player.worldY > worldY && yDist < straight && xDist < horizontal) targetInRange = true;
}
case LEFT -> {
if(panel.player.worldX < worldX && xDist < straight && yDist < horizontal) targetInRange = true;
}
case RIGHT -> {
if(panel.player.worldX > worldX && xDist < straight && yDist < horizontal) targetInRange = true;
}
}
if(targetInRange)
if (new Random().nextInt(rate) == 0) {
attacking = true;
spriteNum = 1;
spriteCount = 0;
shotAvailableCount = 0;
}
}
public void setRandomDirection() {
actionLock++;
if(actionLock == 120) { //lock action for x frames