Compare commits

...

3 Commits

Author SHA1 Message Date
5dd43b6f9d added game over screen 2025-12-12 12:26:32 +01:00
785a76942e optimized settings 2025-12-12 11:52:31 +01:00
099f52278c save game settings and parse them 2025-12-12 11:34:01 +01:00
10 changed files with 232 additions and 74 deletions

5
.gitignore vendored
View File

@@ -32,4 +32,7 @@ bin/
.DS_Store
### JPackage ###
JGame2D
JGame2D
### Local Game Settings ###
gamedata

View File

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

View File

@@ -13,11 +13,13 @@ public class Boot {
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setResizable(false);
window.setTitle("JGame2D");
//window.setUndecorated(true);
GamePanel gamePanel = new GamePanel();
window.add(gamePanel);
gamePanel.config.load();
if(gamePanel.fullscreen) window.setUndecorated(true);
window.pack();
window.setLocationRelativeTo(null);

View File

@@ -0,0 +1,83 @@
package de.miaurizius.jgame2d.core;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Config {
private final GamePanel panel;
private final HashMap<String, String> settings = new HashMap<>();
public Config(GamePanel panel) {
this.panel = panel;
for (SETTING option : SETTING.values()) settings.put(option.name, null);
}
// GENERAL
public final void save() {
try {
insertToHash();
BufferedWriter writer = new BufferedWriter(new FileWriter("gamedata/config"));
settings.forEach((invoke, value) -> {
try {
writer.write(invoke + ": " + value);
writer.newLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public final void load() {
try (BufferedReader reader = new BufferedReader(new FileReader("gamedata/config"))) {
List<String> lines = reader.lines().toList();
if (lines.isEmpty()) return;
Map<String, String> configMap = new HashMap<>();
for (String line : lines) {
String[] parts = line.split(":\\s*", 2);
if (parts.length == 2) {
configMap.put(parts[0].trim(), parts[1].trim());
}
}
for (SETTING setting : SETTING.values()) {
String value = configMap.get(setting.name);
if (value != null) {
switch (setting) {
case FULLSCREEN -> panel.fullscreen = Boolean.parseBoolean(value);
case MUSICVOLUME -> panel.music.volumeScale = Integer.parseInt(value);
case SFXVOLUME -> panel.sfx.volumeScale = Integer.parseInt(value);
}
}
}
insertToHash();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// HELP FUNCTIONS
private void insertToHash() {
settings.put(SETTING.FULLSCREEN.name, String.valueOf(panel.fullscreen));
settings.put(SETTING.MUSICVOLUME.name, String.valueOf(panel.music.volumeScale));
settings.put(SETTING.SFXVOLUME.name, String.valueOf(panel.sfx.volumeScale));
}
private enum SETTING {
FULLSCREEN("fullscreen"),
MUSICVOLUME("music-vol"),
SFXVOLUME("sfx-vol");
private final String name;
SETTING(String name) {
this.name = name;
}
}
}

View File

@@ -28,11 +28,11 @@ public class GamePanel extends JPanel implements Runnable {
public final int screenHeight = tileSize * maxScreenRow; // 576 pixels
// FULLSCREEN SETTINGS
public boolean fullscreen;
int fScreenWidth = screenWidth;
int fScreenHeight = screenHeight;
BufferedImage tempScreen;
Graphics2D fg2;
public boolean fullscreen;
// WORLD SETTINGS
public final int maxWorldCol = 50;
@@ -50,8 +50,9 @@ public class GamePanel extends JPanel implements Runnable {
public AssetSetter assetSetter = new AssetSetter(this);
public UI ui = new UI(this);
public EventHandler eventH = new EventHandler(this);
public Sound se = new Sound();
public Sound sfx = new Sound();
public Sound music = new Sound();
public Config config = new Config(this);
Thread gameThread;
// ENTITY AND OBJECT
@@ -211,9 +212,9 @@ public class GamePanel extends JPanel implements Runnable {
music.stop();
}
public void playSE(int i) {
Clip c = se.clips[i];
se.fc = (FloatControl) c.getControl(FloatControl.Type.MASTER_GAIN);
se.checkVolume();
Clip c = sfx.clips[i];
sfx.fc = (FloatControl) c.getControl(FloatControl.Type.MASTER_GAIN);
sfx.checkVolume();
if(c.isRunning()) c.stop();
c.setFramePosition(0);
c.start();
@@ -230,7 +231,7 @@ public class GamePanel extends JPanel implements Runnable {
tempScreen = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB);
fg2 = (Graphics2D) tempScreen.getGraphics();
//setFullscreen();
if(fullscreen) setFullscreen();
}
public void setFullscreen() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
@@ -240,5 +241,18 @@ public class GamePanel extends JPanel implements Runnable {
fScreenWidth = Boot.window.getWidth();
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;
switch (panel.gameState) {
case GameState.PLAY:
drawPlayScreen();
break;
case GameState.PAUSE:
drawPauseScreen();
break;
case GameState.DIALOGUE:
drawDialogueScreen();
break;
case TITLE:
drawTitleScreen();
break;
case CHARACTER:
drawCharacterScreen();
break;
case GameState.PLAY -> drawPlayScreen();
case GameState.PAUSE -> drawPauseScreen();
case GameState.DIALOGUE -> drawDialogueScreen();
case TITLE -> drawTitleScreen();
case CHARACTER -> drawCharacterScreen();
case GAMEOVER -> drawGameOverScreen();
}
}
// HUD
public void drawPlayerLife() {
private void drawPlayerLife() {
int x = panel.tileSize / 2;
int y = panel.tileSize / 2;
int i = 0;
@@ -91,7 +82,7 @@ public class UI {
x += panel.tileSize;
}
}
public void drawMessages() {
private void drawMessages() {
int messageX = panel.tileSize;
int messageY = panel.tileSize*4;
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.BOLD, 32F));
@@ -112,7 +103,7 @@ public class UI {
}
}
}
public void drawCharStats() {
private void drawCharStats() {
// DRAW FRAME
final int frameX = panel.tileSize*2;
final int frameY = panel.tileSize;
@@ -159,7 +150,7 @@ public class UI {
textY += panel.tileSize;
graphics2d.drawImage(panel.player.currentShield.down1, tailX - panel.tileSize, textY-14, null);
}
public void drawInventory() {
private void drawInventory() {
// DRAW FRAME
int frameX = panel.tileSize*12;
int frameY = panel.tileSize;
@@ -221,7 +212,7 @@ public class UI {
}
// GAME STATES
public void drawPauseScreen() {
private void drawPauseScreen() {
graphics2d.setColor(Color.white);
graphics2d.setFont(graphics2d.getFont().deriveFont(32F));
@@ -240,7 +231,7 @@ public class UI {
}
panel.keyH.spacePressed = false;
}
public void drawDialogueScreen() {
private void drawDialogueScreen() {
drawPlayerLife();
// WINDOW
int x = panel.tileSize*2;
@@ -258,7 +249,7 @@ public class UI {
y += 40;
}
}
public void drawTitleScreen() {
private void drawTitleScreen() {
graphics2d.setColor(new Color(0, 0, 0));
graphics2d.fillRect(0, 0, panel.screenWidth, panel.screenHeight);
@@ -296,27 +287,59 @@ public class UI {
graphics2d.drawString(text, x, y);
if(commandNum == 2) graphics2d.drawString(">", x-panel.tileSize, y);
}
public void drawCharacterScreen() {
private void drawCharacterScreen() {
drawCharStats();
drawInventory();
}
public void drawPlayScreen() {
private void drawPlayScreen() {
drawPlayerLife();
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
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.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) {
private int getCenteredX(String text) {
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();
}
public int getItemIndex() {
@@ -328,7 +351,7 @@ public class UI {
}
// OPTIONS UI
public void optionsTop(int frameX, int frameY) {
private void optionsTop(int frameX, int frameY) {
int textX, textY;
String title = "Options";
textX = getCenteredX(title);
@@ -370,7 +393,7 @@ public class UI {
// END GAME
textY += panel.tileSize;
graphics2d.drawString("Quit Game", textX, textY);
graphics2d.drawString("Title Screen", textX, textY);
if(commandNum == 4) {
graphics2d.drawString(">", textX-25, textY);
if(panel.keyH.spacePressed) {
@@ -408,9 +431,11 @@ public class UI {
// SFX VOLUME
textY += panel.tileSize;
graphics2d.drawRect(textX, textY, 120, panel.tileSize/2); // 120/5 = 24
graphics2d.fillRect(textX, textY, 24 * panel.se.volumeScale, panel.tileSize/2);
graphics2d.fillRect(textX, textY, 24 * panel.sfx.volumeScale, panel.tileSize/2);
panel.config.save();
}
public void optionsControls(int frameX, int frameY) {
private void optionsControls(int frameX, int frameY) {
int textX;
int textY;
@@ -443,7 +468,7 @@ public class UI {
graphics2d.drawString(">", textX-25, textY);
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 textY = frameY + panel.tileSize*3;
@@ -462,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 textY = frameY + panel.tileSize*3;

View File

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

View File

@@ -18,7 +18,7 @@ public class KeyHandler implements KeyListener {
}
// STATE SPECIFIC KEYBIND CONFIGURATION
public void handleTitle(int code) {
private void handleTitle(int code) {
switch (code) {
case KeyEvent.VK_UP -> {
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) {
// CONTROLS
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;
}
}
public void handlePause(int code) {
private void handlePause(int code) {
if(code == KeyEvent.VK_SPACE) spacePressed = true;
int maxCommandNum = 0;
@@ -92,8 +92,8 @@ public class KeyHandler implements KeyListener {
panel.music.volumeScale--;
panel.music.checkVolume();
panel.playSE(9);
} else if(panel.ui.commandNum == 2 && panel.se.volumeScale > 0) {
panel.se.volumeScale--;
} else if(panel.ui.commandNum == 2 && panel.sfx.volumeScale > 0) {
panel.sfx.volumeScale--;
panel.playSE(9);
}
break;
@@ -103,8 +103,8 @@ public class KeyHandler implements KeyListener {
panel.music.volumeScale++;
panel.music.checkVolume();
panel.playSE(9);
} else if(panel.ui.commandNum == 2 && panel.se.volumeScale < 5) {
panel.se.volumeScale++;
} else if(panel.ui.commandNum == 2 && panel.sfx.volumeScale < 5) {
panel.sfx.volumeScale++;
panel.playSE(9);
}
}
@@ -115,35 +115,35 @@ public class KeyHandler implements KeyListener {
if(code == KeyEvent.VK_ESCAPE)
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
if (code == KeyEvent.VK_SPACE) {
panel.gameState = GameState.PLAY;
}
}
public void handleCharacter(int code) throws InterruptedException {
private void handleCharacter(int code) {
switch (code) {
case KeyEvent.VK_UP:
if(panel.ui.slotRow == 0) break;
if(panel.se.clips[9].isRunning()) break;
if(panel.sfx.clips[9].isRunning()) break;
panel.ui.slotRow--;
panel.playSE(9);
break;
case KeyEvent.VK_DOWN:
if(panel.ui.slotRow == 3) break;
if(panel.se.clips[9].isRunning()) break;
if(panel.sfx.clips[9].isRunning()) break;
panel.ui.slotRow++;
panel.playSE(9);
break;
case KeyEvent.VK_LEFT:
if(panel.ui.slotCol == 0) break;
if(panel.se.clips[9].isRunning()) break;
if(panel.sfx.clips[9].isRunning()) break;
panel.ui.slotCol--;
panel.playSE(9);
break;
case KeyEvent.VK_RIGHT:
if(panel.ui.slotCol == 4) break;
if(panel.se.clips[9].isRunning()) break;
if(panel.sfx.clips[9].isRunning()) break;
panel.ui.slotCol++;
panel.playSE(9);
break;
@@ -157,6 +157,30 @@ public class KeyHandler implements KeyListener {
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
@Override
@@ -165,25 +189,12 @@ public class KeyHandler implements KeyListener {
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
switch(panel.gameState) {
case PLAY:
handlePlay(code);
break;
case DIALOGUE:
handleDialogue(code);
break;
case TITLE:
handleTitle(code);
break;
case PAUSE:
handlePause(code);
break;
case CHARACTER:
try {
handleCharacter(code);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
break;
case PLAY -> handlePlay(code);
case DIALOGUE -> handleDialogue(code);
case TITLE -> handleTitle(code);
case PAUSE -> handlePause(code);
case CHARACTER -> handleCharacter(code);
case GAMEOVER -> handleGameOver(code);
}
}
@Override

View File

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

View File

@@ -125,6 +125,11 @@ public class Player extends Entity {
invincible = false;
invincibleCount = 0;
}
if(life <= 0) {
panel.gameState = GameState.GAMEOVER;
panel.playSE(12);
}
}
// INTERACTION
@@ -241,7 +246,7 @@ public class Player extends Entity {
super.speak();
}
// BACKGROUND CHECKS
// BACKGROUND JOBS
public void checkLevelUp() {
if(exp < nextLevelExp) return;
level++;
@@ -274,6 +279,15 @@ public class Player extends Entity {
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
public void setDefaultValues() {
@@ -298,6 +312,7 @@ public class Player extends Entity {
defense = getDefense();
// INVENTORY
inventory.clear();
inventory.add(currentWeapon);
inventory.add(currentShield);
}