84 lines
2.7 KiB
Java
84 lines
2.7 KiB
Java
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;
|
|
}
|
|
}
|
|
|
|
}
|