save game settings and parse them

This commit is contained in:
2025-12-12 11:34:01 +01:00
parent d294578a74
commit 099f52278c
6 changed files with 91 additions and 18 deletions

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,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<String, String> 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<String> 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));
}
}

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();

View File

@@ -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;

View File

@@ -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;