From 2ed21add1c148705571f2b6bef69989c37d860e9 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sun, 30 Nov 2025 00:44:12 +0100 Subject: [PATCH] make hp bar hideable --- src/de/miaurizius/jgame2d/entity/Entity.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/de/miaurizius/jgame2d/entity/Entity.java b/src/de/miaurizius/jgame2d/entity/Entity.java index 588983f..56942b4 100644 --- a/src/de/miaurizius/jgame2d/entity/Entity.java +++ b/src/de/miaurizius/jgame2d/entity/Entity.java @@ -35,12 +35,14 @@ public class Entity { boolean attacking; public boolean alive = true; public boolean dying; + public boolean hpBarOn; // COUNTER public int spriteCounter; public int actionLock; public int invincibleCount; int dyingCount; + int hpBarCounter; // ATTRIBUTES public EntityType type; @@ -106,16 +108,26 @@ public class Entity { ) { // MONSTER HP-BAR - if(this.type == EntityType.MONSTER) { + if(this.type == EntityType.MONSTER && hpBarOn) { graphics2d.setColor(new Color(35, 35, 35)); graphics2d.fillRect(screenX-1, screenY-6, panel.tileSize+2, 12); graphics2d.setColor(new Color(255, 0, 30)); graphics2d.fillRect(screenX, screenY-5, (int) ((double) panel.tileSize/maxLife)*life, 10); + + hpBarCounter++; + if(hpBarCounter > 600) { //bar disappears after 10 seconds + hpBarCounter = 0; + hpBarOn = false; + } } // DRAW ENTITY - if(invincible) graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f)); + if(invincible) { + hpBarOn = true; + hpBarCounter = 0; + changeOpacity(graphics2d, 0.4f); + } 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(attacking) graphics2d.drawImage(parseSpriteATK(), @@ -124,7 +136,7 @@ public class Entity { else graphics2d.drawImage(parseSprite(), screenX, screenY, null); } else graphics2d.drawImage(parseSprite(), screenX, screenY, null); - graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f)); + changeOpacity(graphics2d, 1f); } }