added game over screen

This commit is contained in:
2025-12-12 12:26:32 +01:00
parent 785a76942e
commit 5dd43b6f9d
7 changed files with 123 additions and 56 deletions

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: de.miaurizius.jgame2d.core.Boot

View File

@@ -241,5 +241,18 @@ public class GamePanel extends JPanel implements Runnable {
fScreenWidth = Boot.window.getWidth(); fScreenWidth = Boot.window.getWidth();
fScreenHeight = Boot.window.getHeight(); fScreenHeight = Boot.window.getHeight();
} }
public void retry() {
player.setDefaultPositions();
player.restoreLife();
assetSetter.setNPC();
assetSetter.setMonster();
}
public void restart() {
player.setDefaultValues();
assetSetter.setObject();
assetSetter.setNPC();
assetSetter.setMonster();
assetSetter.setITiles();
}
} }

View File

@@ -47,26 +47,17 @@ 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 -> drawPlayScreen();
drawPlayScreen(); case GameState.PAUSE -> drawPauseScreen();
break; case GameState.DIALOGUE -> drawDialogueScreen();
case GameState.PAUSE: case TITLE -> drawTitleScreen();
drawPauseScreen(); case CHARACTER -> drawCharacterScreen();
break; case GAMEOVER -> drawGameOverScreen();
case GameState.DIALOGUE:
drawDialogueScreen();
break;
case TITLE:
drawTitleScreen();
break;
case CHARACTER:
drawCharacterScreen();
break;
} }
} }
// HUD // HUD
public void drawPlayerLife() { private void drawPlayerLife() {
int x = panel.tileSize / 2; int x = panel.tileSize / 2;
int y = panel.tileSize / 2; int y = panel.tileSize / 2;
int i = 0; int i = 0;
@@ -91,7 +82,7 @@ public class UI {
x += panel.tileSize; x += panel.tileSize;
} }
} }
public void drawMessages() { private void drawMessages() {
int messageX = panel.tileSize; int messageX = panel.tileSize;
int messageY = panel.tileSize*4; int messageY = panel.tileSize*4;
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.BOLD, 32F)); graphics2d.setFont(graphics2d.getFont().deriveFont(Font.BOLD, 32F));
@@ -112,7 +103,7 @@ public class UI {
} }
} }
} }
public void drawCharStats() { private void drawCharStats() {
// DRAW FRAME // DRAW FRAME
final int frameX = panel.tileSize*2; final int frameX = panel.tileSize*2;
final int frameY = panel.tileSize; final int frameY = panel.tileSize;
@@ -159,7 +150,7 @@ public class UI {
textY += panel.tileSize; textY += panel.tileSize;
graphics2d.drawImage(panel.player.currentShield.down1, tailX - panel.tileSize, textY-14, null); graphics2d.drawImage(panel.player.currentShield.down1, tailX - panel.tileSize, textY-14, null);
} }
public void drawInventory() { private void drawInventory() {
// DRAW FRAME // DRAW FRAME
int frameX = panel.tileSize*12; int frameX = panel.tileSize*12;
int frameY = panel.tileSize; int frameY = panel.tileSize;
@@ -221,7 +212,7 @@ public class UI {
} }
// GAME STATES // GAME STATES
public void drawPauseScreen() { private void drawPauseScreen() {
graphics2d.setColor(Color.white); graphics2d.setColor(Color.white);
graphics2d.setFont(graphics2d.getFont().deriveFont(32F)); graphics2d.setFont(graphics2d.getFont().deriveFont(32F));
@@ -240,7 +231,7 @@ public class UI {
} }
panel.keyH.spacePressed = false; panel.keyH.spacePressed = false;
} }
public void drawDialogueScreen() { private void drawDialogueScreen() {
drawPlayerLife(); drawPlayerLife();
// WINDOW // WINDOW
int x = panel.tileSize*2; int x = panel.tileSize*2;
@@ -258,7 +249,7 @@ public class UI {
y += 40; y += 40;
} }
} }
public void drawTitleScreen() { private void drawTitleScreen() {
graphics2d.setColor(new Color(0, 0, 0)); graphics2d.setColor(new Color(0, 0, 0));
graphics2d.fillRect(0, 0, panel.screenWidth, panel.screenHeight); graphics2d.fillRect(0, 0, panel.screenWidth, panel.screenHeight);
@@ -296,27 +287,59 @@ public class UI {
graphics2d.drawString(text, x, y); graphics2d.drawString(text, x, y);
if(commandNum == 2) graphics2d.drawString(">", x-panel.tileSize, y); if(commandNum == 2) graphics2d.drawString(">", x-panel.tileSize, y);
} }
public void drawCharacterScreen() { private void drawCharacterScreen() {
drawCharStats(); drawCharStats();
drawInventory(); drawInventory();
} }
public void drawPlayScreen() { private void drawPlayScreen() {
drawPlayerLife(); drawPlayerLife();
drawMessages(); drawMessages();
} }
private void drawGameOverScreen() {
graphics2d.setColor(new Color(0,0,0, 150));
graphics2d.fillRect(0, 0, panel.screenWidth, panel.screenHeight);
int x, y;
String text;
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.BOLD, 110F));
text = "GAME OVER";
// SHADOW
graphics2d.setColor(Color.black);
x = getCenteredX(text);
y = panel.tileSize*3;
graphics2d.drawString(text, x, y);
// MAIN TEXT
graphics2d.setColor(Color.white);
graphics2d.drawString(text, x-4, y-4);
// OPTIONS
graphics2d.setFont(graphics2d.getFont().deriveFont(50F));
text = "Retry";
x = getCenteredX(text);
y += panel.tileSize*4;
graphics2d.drawString(text, x, y);
if(commandNum == 0) graphics2d.drawString(">", x-panel.tileSize, y);
text = "Return to title";
x = getCenteredX(text);
y += 55;
graphics2d.drawString(text, x, y);
if(commandNum == 1) graphics2d.drawString(">", x-panel.tileSize, y);
}
// UTILITY // UTILITY
public void drawSubWindow(int x, int y, int width, int height) { private void drawSubWindow(int x, int y, int width, int height) {
graphics2d.setColor(new Color(0,0,0,210)); graphics2d.setColor(new Color(0,0,0,210));
graphics2d.fillRoundRect(x, y, width, height, 35, 35); graphics2d.fillRoundRect(x, y, width, height, 35, 35);
graphics2d.setColor(new Color(255,255,255)); graphics2d.setColor(new Color(255,255,255));
graphics2d.setStroke(new BasicStroke(5)); graphics2d.setStroke(new BasicStroke(5));
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) { private 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;
} }
public int getAlignedToRightX(String text, int tailX) { private int getAlignedToRightX(String text, int tailX) {
return tailX - (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth(); return tailX - (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth();
} }
public int getItemIndex() { public int getItemIndex() {
@@ -328,7 +351,7 @@ public class UI {
} }
// OPTIONS UI // OPTIONS UI
public void optionsTop(int frameX, int frameY) { private void optionsTop(int frameX, int frameY) {
int textX, textY; int textX, textY;
String title = "Options"; String title = "Options";
textX = getCenteredX(title); textX = getCenteredX(title);
@@ -412,7 +435,7 @@ public class UI {
panel.config.save(); panel.config.save();
} }
public void optionsControls(int frameX, int frameY) { private void optionsControls(int frameX, int frameY) {
int textX; int textX;
int textY; int textY;
@@ -445,7 +468,7 @@ public class UI {
graphics2d.drawString(">", textX-25, textY); graphics2d.drawString(">", textX-25, textY);
if(panel.keyH.spacePressed) optionState = OptionState.OVERVIEW; if(panel.keyH.spacePressed) optionState = OptionState.OVERVIEW;
} }
public void optionsFSNotify(int frameX, int frameY) { private void optionsFSNotify(int frameX, int frameY) {
int textX = frameX + panel.tileSize; int textX = frameX + panel.tileSize;
int textY = frameY + panel.tileSize*3; int textY = frameY + panel.tileSize*3;
@@ -464,7 +487,7 @@ public class UI {
} }
} }
public void optionsQuitNotify(int frameX, int frameY) { private void optionsQuitNotify(int frameX, int frameY) {
int textX = frameX + panel.tileSize; int textX = frameX + panel.tileSize;
int textY = frameY + panel.tileSize*3; int textY = frameY + panel.tileSize*3;

View File

@@ -7,5 +7,6 @@ public enum GameState {
DIALOGUE, DIALOGUE,
TITLE, TITLE,
CHARACTER, CHARACTER,
GAMEOVER,
} }

View File

@@ -18,7 +18,7 @@ public class KeyHandler implements KeyListener {
} }
// STATE SPECIFIC KEYBIND CONFIGURATION // STATE SPECIFIC KEYBIND CONFIGURATION
public void handleTitle(int code) { private void handleTitle(int code) {
switch (code) { switch (code) {
case KeyEvent.VK_UP -> { case KeyEvent.VK_UP -> {
if(panel.ui.commandNum != 0) panel.ui.commandNum--; if(panel.ui.commandNum != 0) panel.ui.commandNum--;
@@ -42,7 +42,7 @@ public class KeyHandler implements KeyListener {
} }
} }
} }
public void handlePlay(int code) { private void handlePlay(int code) {
switch (code) { switch (code) {
// CONTROLS // CONTROLS
case KeyEvent.VK_W, KeyEvent.VK_UP -> upPressed = true; case KeyEvent.VK_W, KeyEvent.VK_UP -> upPressed = true;
@@ -65,7 +65,7 @@ public class KeyHandler implements KeyListener {
case KeyEvent.VK_C -> panel.gameState = GameState.CHARACTER; case KeyEvent.VK_C -> panel.gameState = GameState.CHARACTER;
} }
} }
public void handlePause(int code) { private void handlePause(int code) {
if(code == KeyEvent.VK_SPACE) spacePressed = true; if(code == KeyEvent.VK_SPACE) spacePressed = true;
int maxCommandNum = 0; int maxCommandNum = 0;
@@ -115,13 +115,13 @@ public class KeyHandler implements KeyListener {
if(code == KeyEvent.VK_ESCAPE) if(code == KeyEvent.VK_ESCAPE)
if(panel.ui.optionState == UI.OptionState.OVERVIEW) panel.gameState = GameState.PLAY; else panel.ui.optionState = UI.OptionState.OVERVIEW; if(panel.ui.optionState == UI.OptionState.OVERVIEW) panel.gameState = GameState.PLAY; else panel.ui.optionState = UI.OptionState.OVERVIEW;
} }
public void handleDialogue(int code) { private void handleDialogue(int code) {
// EXIT STATE // EXIT STATE
if (code == KeyEvent.VK_SPACE) { if (code == KeyEvent.VK_SPACE) {
panel.gameState = GameState.PLAY; panel.gameState = GameState.PLAY;
} }
} }
public void handleCharacter(int code) throws InterruptedException { private void handleCharacter(int code) {
switch (code) { switch (code) {
case KeyEvent.VK_UP: case KeyEvent.VK_UP:
if(panel.ui.slotRow == 0) break; if(panel.ui.slotRow == 0) break;
@@ -157,6 +157,30 @@ public class KeyHandler implements KeyListener {
break; break;
} }
} }
private void handleGameOver(int code) {
switch(code) {
case KeyEvent.VK_UP:
if (panel.ui.commandNum <= 0) break;
panel.ui.commandNum--;
panel.playSE(9);
break;
case KeyEvent.VK_DOWN:
if (panel.ui.commandNum >= 1) break;
panel.ui.commandNum++;
panel.playSE(9);
break;
case KeyEvent.VK_SPACE:
if(panel.ui.commandNum == 0) {
panel.gameState = GameState.PLAY;
panel.retry();
}
if(panel.ui.commandNum == 1) {
panel.gameState = GameState.TITLE;
panel.restart();
panel.stopMusic();
}
}
}
// KEY-LISTENER // KEY-LISTENER
@Override @Override
@@ -165,25 +189,12 @@ public class KeyHandler implements KeyListener {
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
int code = e.getKeyCode(); int code = e.getKeyCode();
switch(panel.gameState) { switch(panel.gameState) {
case PLAY: case PLAY -> handlePlay(code);
handlePlay(code); case DIALOGUE -> handleDialogue(code);
break; case TITLE -> handleTitle(code);
case DIALOGUE: case PAUSE -> handlePause(code);
handleDialogue(code); case CHARACTER -> handleCharacter(code);
break; case GAMEOVER -> handleGameOver(code);
case TITLE:
handleTitle(code);
break;
case PAUSE:
handlePause(code);
break;
case CHARACTER:
try {
handleCharacter(code);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
break;
} }
} }
@Override @Override

View File

@@ -32,6 +32,7 @@ public class Sound {
load(9, "assets/sounds/cursor.wav"); load(9, "assets/sounds/cursor.wav");
load(10, "assets/sounds/burning.wav"); load(10, "assets/sounds/burning.wav");
load(11, "assets/sounds/cuttree.wav"); load(11, "assets/sounds/cuttree.wav");
load(12, "assets/sounds/gameover.wav");
} }
@Deprecated @Deprecated

View File

@@ -125,6 +125,11 @@ public class Player extends Entity {
invincible = false; invincible = false;
invincibleCount = 0; invincibleCount = 0;
} }
if(life <= 0) {
panel.gameState = GameState.GAMEOVER;
panel.playSE(12);
}
} }
// INTERACTION // INTERACTION
@@ -241,7 +246,7 @@ public class Player extends Entity {
super.speak(); super.speak();
} }
// BACKGROUND CHECKS // BACKGROUND JOBS
public void checkLevelUp() { public void checkLevelUp() {
if(exp < nextLevelExp) return; if(exp < nextLevelExp) return;
level++; level++;
@@ -274,6 +279,15 @@ public class Player extends Entity {
inventory.remove(itemIndex); inventory.remove(itemIndex);
} }
} }
public void setDefaultPositions() {
worldX = panel.tileSize * 23;
worldY = panel.tileSize * 21;
direction = Direction.DOWN;
}
public void restoreLife() {
life = maxLife;
invincible = false;
}
// SETTING THINGS UP // SETTING THINGS UP
public void setDefaultValues() { public void setDefaultValues() {
@@ -298,6 +312,7 @@ public class Player extends Entity {
defense = getDefense(); defense = getDefense();
// INVENTORY // INVENTORY
inventory.clear();
inventory.add(currentWeapon); inventory.add(currentWeapon);
inventory.add(currentShield); inventory.add(currentShield);
} }