add guarding mechanic and implement guard sprite handling

This commit is contained in:
2025-12-13 23:55:47 +01:00
parent 133afa9c06
commit d018ed3e6e
4 changed files with 64 additions and 12 deletions

View File

@@ -23,6 +23,7 @@ public class Entity {
protected GamePanel panel;
public BufferedImage up1, up2, down1, down2, left1, left2, right1, right2;
public BufferedImage attackUp1, attackUp2, attackDown1, attackDown2, attackLeft1, attackLeft2, attackRight1, attackRight2;
public BufferedImage guardUp, guardDown, guardLeft, guardRight;
public BufferedImage image, image2, image3;
public Rectangle solidArea = new Rectangle(0, 0, 48, 48);
public Rectangle attackArea = new Rectangle(0, 0, 0, 0);
@@ -38,6 +39,7 @@ public class Entity {
int dialogueIndex;
public boolean collisionOn;
public boolean invincible;
public boolean transparent;
public boolean attacking;
public boolean alive = true;
public boolean dying;
@@ -45,6 +47,7 @@ public class Entity {
public boolean consumable;
public boolean onPath;
public boolean knockback;
public boolean guarding;
public Direction knockbackDirection;
// COUNTER
@@ -181,16 +184,16 @@ public class Entity {
if(invincible) {
hpBarOn = true;
hpBarCount = 0;
changeOpacity(graphics2d, 0.4f);
if(transparent) changeOpacity(graphics2d, 0.4f);
}
if(dying) dyingAnimation(graphics2d);
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);
else if(guarding) graphics2d.drawImage(parseSpriteGRD(), screenX, screenY, null);
else graphics2d.drawImage(parseSprite(), screenX, screenY, null);
} else graphics2d.drawImage(parseSprite(), screenX, screenY, null);
changeOpacity(graphics2d, 1f);
}
@@ -247,11 +250,18 @@ public class Entity {
}
public void damagePlayer(int attack) {
if(panel.player.invincible) return;
panel.playSE(6);
boolean block = panel.player.guarding && panel.player.direction == this.direction.getOpposite();
int damage = attack - panel.player.defense;
panel.player.life -= Math.max(damage, 0);
if(block) {
panel.playSE(15);
damage = 0;
} else panel.playSE(6);
panel.player.life -= Math.max(damage, (block ? 0 : 1));
if(damage != 0) panel.player.transparent = true;
panel.player.invincible = true;
}
public void speak() {
@@ -427,6 +437,14 @@ public class Entity {
case RIGHT -> (spriteNum == 1) ? attackRight1 : attackRight2;
};
}
BufferedImage parseSpriteGRD() {
return switch (direction) {
case UP -> guardUp;
case DOWN -> guardDown;
case LEFT -> guardLeft;
case RIGHT -> guardRight;
};
}
public BufferedImage initEntitySprites(String name) {
try {
return Utility.scaleImage(ImageIO.read(new FileInputStream("assets/" + name + ".png")), panel.tileSize, panel.tileSize);