Files
JGame2D/src/de/miaurizius/jgame2d/core/UI.java

158 lines
4.8 KiB
Java

package de.miaurizius.jgame2d.core;
import de.miaurizius.jgame2d.core.enums.GameState;
import de.miaurizius.jgame2d.entity.Entity;
import de.miaurizius.jgame2d.entity.item.HeartObj;
import java.awt.*;
import java.awt.image.BufferedImage;
public class UI {
GamePanel panel;
Graphics2D graphics2d;
Font arial_40, arial_80B; //TODO: Custom font loader: https://www.youtube.com/watch?v=g-wrebFVP3E
BufferedImage heart_full, heart_half, heart_blank;
public String currentDialogue;
public int commandNum;
public UI(GamePanel panel) {
this.panel = panel;
arial_40 = new Font("Arial", Font.PLAIN, 40);
arial_80B = new Font("Arial", Font.BOLD, 80);
// CREATE HUD OBJECT
Entity heart = new HeartObj(panel);
heart_full = heart.image;
heart_half = heart.image2;
heart_blank = heart.image3;
}
public void draw(Graphics2D graphics2d) {
this.graphics2d = graphics2d;
graphics2d.setFont(arial_40);
graphics2d.setColor(Color.white);
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:
drawTitleScreen();
break;
}
}
// HUD
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;
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;
}
}
// GAME STATES
public void drawPauseScreen() {
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 80));
String text = "PAUSED";
int y = panel.screenHeight / 2;
graphics2d.drawString(text, getCenteredX(text), y);
}
public void drawDialogueScreen() {
// WINDOW
int x = panel.tileSize*2;
int y = panel.tileSize/2;
int width = panel.screenWidth - (panel.tileSize*4);
int height = panel.tileSize*4;
drawSubWindow(x, y, width, height);
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 28F));
x += panel.tileSize;
y += panel.tileSize;
for(String line : currentDialogue.split("\n")) {
graphics2d.drawString(line, x, y);
y += 40;
}
}
public void drawTitleScreen() {
graphics2d.setColor(new Color(0, 0, 0));
graphics2d.fillRect(0, 0, panel.screenWidth, panel.screenHeight);
// TITLE NAME
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.BOLD, 96F));
String text = "JGame2D";
int x = getCenteredX(text);
int y = panel.tileSize*3;
// SHADOW
graphics2d.setColor(Color.gray);
graphics2d.drawString(text, x+5, y+5);
// MAIN COLOR
graphics2d.setColor(Color.white);
graphics2d.drawString(text, x, y);
// MENU
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.BOLD, 48F));
text = "NEW GAME";
x = getCenteredX(text);
y += panel.tileSize*4;
graphics2d.drawString(text, x, y);
if(commandNum == 0) graphics2d.drawString(">", x-panel.tileSize, y);
text = "LOAD GAME";
x = getCenteredX(text);
y += panel.tileSize;
graphics2d.drawString(text, x, y);
if(commandNum == 1) graphics2d.drawString(">", x-panel.tileSize, y);
text = "QUIT";
x = getCenteredX(text);
y += panel.tileSize;
graphics2d.drawString(text, x, y);
if(commandNum == 2) graphics2d.drawString(">", x-panel.tileSize, y);
}
// UTILITY
public void drawSubWindow(int x, int y, int width, int height) {
graphics2d.setColor(new Color(0,0,0,210));
graphics2d.fillRoundRect(x, y, width, height, 35, 35);
graphics2d.setColor(new Color(255,255,255));
graphics2d.setStroke(new BasicStroke(5));
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;
}
}