added player life display
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -28,6 +28,10 @@ public class Entity {
|
||||
String[] dialogue = new String[20];
|
||||
int dialogueIndex = 0;
|
||||
|
||||
// CHARACTER STATUS
|
||||
public int maxLife;
|
||||
public int life;
|
||||
|
||||
public Entity(GamePanel panel) {
|
||||
this.panel = panel;
|
||||
}
|
||||
|
||||
@@ -38,6 +38,10 @@ public class Player extends Entity {
|
||||
worldY = panel.tileSize * 21;
|
||||
speed = 4;
|
||||
direction = Direction.DOWN;
|
||||
|
||||
// PLAYER STATUS (1 heart = 2 lives)
|
||||
maxLife = 6;
|
||||
life = maxLife;
|
||||
}
|
||||
|
||||
public void getPlayerImage() {
|
||||
|
||||
28
src/de/miaurizius/jgame2d/object/HeartObj.java
Normal file
28
src/de/miaurizius/jgame2d/object/HeartObj.java
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import java.awt.image.BufferedImage;
|
||||
|
||||
public class SuperObject {
|
||||
|
||||
public BufferedImage image;
|
||||
public BufferedImage image, image2, image3;
|
||||
public String name;
|
||||
public boolean collision = false;
|
||||
public int worldX, worldY;
|
||||
|
||||
Reference in New Issue
Block a user