added player life display

This commit is contained in:
2025-11-28 21:22:50 +01:00
parent 5fbf815595
commit 8add9c7d7a
5 changed files with 77 additions and 3 deletions

View File

@@ -1,14 +1,18 @@
package de.miaurizius.jgame2d.core;
import de.miaurizius.jgame2d.core.enums.GameState;
import de.miaurizius.jgame2d.object.HeartObj;
import de.miaurizius.jgame2d.object.SuperObject;
import java.awt.*;
import java.awt.image.BufferedImage;
public class UI {
GamePanel panel;
Graphics2D graphics2d;
Font arial_40, arial_80B;
BufferedImage heart_full, heart_half, heart_blank;
public String currentDialogue;
public int commandNum = 0;
@@ -16,6 +20,12 @@ public class UI {
this.panel = panel;
arial_40 = new Font("Arial", Font.PLAIN, 40);
arial_80B = new Font("Arial", Font.BOLD, 80);
// CREATE HUD OBJECT
SuperObject heart = new HeartObj(panel);
heart_full = heart.image;
heart_half = heart.image2;
heart_blank = heart.image3;
}
public void draw(Graphics2D graphics2d) {
@@ -26,12 +36,14 @@ public class UI {
if(panel.gameState == null) return;
switch (panel.gameState) {
case GameState.PLAY:
drawPlayerLife();
break;
case GameState.PAUSE:
drawPlayerLife();
drawPauseScreen();
break;
case GameState.DIALOGUE:
drawPlayerLife();
drawDialogueScreen();
break;
case TITLE:
@@ -40,6 +52,33 @@ public class UI {
}
}
public void drawPlayerLife() {
int x = panel.tileSize / 2;
int y = panel.tileSize / 2;
int i = 0;
// DRAW MAX HEART
while(i<panel.player.maxLife/2) {
graphics2d.drawImage(heart_blank, x, y, null);
i++;
x += panel.tileSize;
}
// RESET
x = panel.tileSize / 2;
y = panel.tileSize / 2;
i = 0;
// DRAW CURRENT LIFE
while(i<panel.player.life) {
graphics2d.drawImage(heart_half, x, y, null);
i++;
if(i < panel.player.life) graphics2d.drawImage(heart_full, x, y, null);
i++;
x += panel.tileSize;
}
}
public void drawPauseScreen() {
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 80));
String text = "PAUSED";
@@ -112,7 +151,6 @@ public class UI {
graphics2d.drawRoundRect(x+5, y+5, width-10, height-10, 25, 25);
}
public int getCenteredX(String text) {
return panel.screenWidth / 2 - (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth() / 2;
}