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; package de.miaurizius.jgame2d.core;
import de.miaurizius.jgame2d.core.enums.GameState; 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.*;
import java.awt.image.BufferedImage;
public class UI { public class UI {
GamePanel panel; GamePanel panel;
Graphics2D graphics2d; Graphics2D graphics2d;
Font arial_40, arial_80B; Font arial_40, arial_80B;
BufferedImage heart_full, heart_half, heart_blank;
public String currentDialogue; public String currentDialogue;
public int commandNum = 0; public int commandNum = 0;
@@ -16,6 +20,12 @@ public class UI {
this.panel = panel; this.panel = panel;
arial_40 = new Font("Arial", Font.PLAIN, 40); arial_40 = new Font("Arial", Font.PLAIN, 40);
arial_80B = new Font("Arial", Font.BOLD, 80); 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) { public void draw(Graphics2D graphics2d) {
@@ -26,12 +36,14 @@ public class UI {
if(panel.gameState == null) return; if(panel.gameState == null) return;
switch (panel.gameState) { switch (panel.gameState) {
case GameState.PLAY: case GameState.PLAY:
drawPlayerLife();
break; break;
case GameState.PAUSE: case GameState.PAUSE:
drawPlayerLife();
drawPauseScreen(); drawPauseScreen();
break; break;
case GameState.DIALOGUE: case GameState.DIALOGUE:
drawPlayerLife();
drawDialogueScreen(); drawDialogueScreen();
break; break;
case TITLE: 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() { public void drawPauseScreen() {
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 80)); graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 80));
String text = "PAUSED"; String text = "PAUSED";
@@ -112,7 +151,6 @@ public class UI {
graphics2d.drawRoundRect(x+5, y+5, width-10, height-10, 25, 25); graphics2d.drawRoundRect(x+5, y+5, width-10, height-10, 25, 25);
} }
public int getCenteredX(String text) { public int getCenteredX(String text) {
return panel.screenWidth / 2 - (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth() / 2; return panel.screenWidth / 2 - (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth() / 2;
} }

View File

@@ -28,6 +28,10 @@ public class Entity {
String[] dialogue = new String[20]; String[] dialogue = new String[20];
int dialogueIndex = 0; int dialogueIndex = 0;
// CHARACTER STATUS
public int maxLife;
public int life;
public Entity(GamePanel panel) { public Entity(GamePanel panel) {
this.panel = panel; this.panel = panel;
} }

View File

@@ -38,6 +38,10 @@ public class Player extends Entity {
worldY = panel.tileSize * 21; worldY = panel.tileSize * 21;
speed = 4; speed = 4;
direction = Direction.DOWN; direction = Direction.DOWN;
// PLAYER STATUS (1 heart = 2 lives)
maxLife = 6;
life = maxLife;
} }
public void getPlayerImage() { public void getPlayerImage() {

View File

@@ -0,0 +1,28 @@
package de.miaurizius.jgame2d.object;
import de.miaurizius.jgame2d.core.Boot;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.Utility;
import javax.imageio.ImageIO;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
public class HeartObj extends SuperObject {
GamePanel panel;
public HeartObj(GamePanel panel) {
this.panel = panel;
name = "heart";
try {
image = Utility.scaleImage(ImageIO.read(new FileInputStream("assets/objects/heart_full.png")), panel.tileSize, panel.tileSize);
image2 = Utility.scaleImage(ImageIO.read(new FileInputStream("assets/objects/heart_half.png")), panel.tileSize, panel.tileSize);
image3 = Utility.scaleImage(ImageIO.read(new FileInputStream("assets/objects/heart_blank.png")), panel.tileSize, panel.tileSize);
} catch(IOException e) {
Boot.logger.log(Level.SEVERE, "Could not load image", e);
}
}
}

View File

@@ -7,7 +7,7 @@ import java.awt.image.BufferedImage;
public class SuperObject { public class SuperObject {
public BufferedImage image; public BufferedImage image, image2, image3;
public String name; public String name;
public boolean collision = false; public boolean collision = false;
public int worldX, worldY; public int worldX, worldY;