From 099f52278c38fb464704ce583eb1230d2499e4d4 Mon Sep 17 00:00:00 2001 From: Maurice Date: Fri, 12 Dec 2025 11:34:01 +0100 Subject: [PATCH] save game settings and parse them --- .gitignore | 5 +- src/de/miaurizius/jgame2d/core/Boot.java | 4 +- src/de/miaurizius/jgame2d/core/Config.java | 65 +++++++++++++++++++ src/de/miaurizius/jgame2d/core/GamePanel.java | 13 ++-- src/de/miaurizius/jgame2d/core/UI.java | 6 +- .../jgame2d/core/handlers/KeyHandler.java | 16 ++--- 6 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 src/de/miaurizius/jgame2d/core/Config.java diff --git a/.gitignore b/.gitignore index b6f05ed..b3bfa42 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,7 @@ bin/ .DS_Store ### JPackage ### -JGame2D \ No newline at end of file +JGame2D + +### Local Game Settings ### +gamedata \ No newline at end of file diff --git a/src/de/miaurizius/jgame2d/core/Boot.java b/src/de/miaurizius/jgame2d/core/Boot.java index 0ad0011..9fbba6a 100644 --- a/src/de/miaurizius/jgame2d/core/Boot.java +++ b/src/de/miaurizius/jgame2d/core/Boot.java @@ -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); diff --git a/src/de/miaurizius/jgame2d/core/Config.java b/src/de/miaurizius/jgame2d/core/Config.java new file mode 100644 index 0000000..67c8c79 --- /dev/null +++ b/src/de/miaurizius/jgame2d/core/Config.java @@ -0,0 +1,65 @@ +package de.miaurizius.jgame2d.core; + +import java.io.*; +import java.util.HashMap; +import java.util.List; + +public class Config { + + private final GamePanel panel; + private final HashMap settings = new HashMap<>(); + private final String[] options = new String[]{ + "fullscreen", //0 + "music-vol", //1 + "sfx-vol" //2 + }; + + public Config(GamePanel panel) { + this.panel = panel; + for (String option : options) settings.put(option, null); + } + + // + 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 lines = reader.lines().toList(); + if(lines.isEmpty()) return; //No config found, use default values + for (int i = 0; i < options.length; i++) { + String value = lines.get(i).split(": ")[1]; + switch (options[i]) { + case "fullscreen" -> panel.fullscreen = Boolean.parseBoolean(value); + case "music-vol" -> panel.music.volumeScale = Integer.parseInt(value); + case "sfx-vol" -> panel.sfx.volumeScale = Integer.parseInt(value); + } + } + insertToHash(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + // HELP FUNCTIONS + private void insertToHash() { + settings.put(options[0], String.valueOf(panel.fullscreen)); + settings.put(options[1], String.valueOf(panel.music.volumeScale)); + settings.put(options[2], String.valueOf(panel.sfx.volumeScale)); + } +} diff --git a/src/de/miaurizius/jgame2d/core/GamePanel.java b/src/de/miaurizius/jgame2d/core/GamePanel.java index 7ce3ee3..28ed7b9 100644 --- a/src/de/miaurizius/jgame2d/core/GamePanel.java +++ b/src/de/miaurizius/jgame2d/core/GamePanel.java @@ -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(); diff --git a/src/de/miaurizius/jgame2d/core/UI.java b/src/de/miaurizius/jgame2d/core/UI.java index 08b66e8..0dd8191 100644 --- a/src/de/miaurizius/jgame2d/core/UI.java +++ b/src/de/miaurizius/jgame2d/core/UI.java @@ -370,7 +370,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,7 +408,9 @@ 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) { int textX; diff --git a/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java b/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java index bc1e582..87bbd38 100644 --- a/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java +++ b/src/de/miaurizius/jgame2d/core/handlers/KeyHandler.java @@ -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); } } @@ -125,25 +125,25 @@ public class KeyHandler implements KeyListener { 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;