optimized settings
This commit is contained in:
@@ -3,23 +3,19 @@ package de.miaurizius.jgame2d.core;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
private final GamePanel panel;
|
private final GamePanel panel;
|
||||||
private final HashMap<String, String> settings = new HashMap<>();
|
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) {
|
public Config(GamePanel panel) {
|
||||||
this.panel = panel;
|
this.panel = panel;
|
||||||
for (String option : options) settings.put(option, null);
|
for (SETTING option : SETTING.values()) settings.put(option.name, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
// GENERAL
|
||||||
public final void save() {
|
public final void save() {
|
||||||
try {
|
try {
|
||||||
insertToHash();
|
insertToHash();
|
||||||
@@ -38,16 +34,25 @@ public class Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
public final void load() {
|
public final void load() {
|
||||||
try {
|
try (BufferedReader reader = new BufferedReader(new FileReader("gamedata/config"))) {
|
||||||
BufferedReader reader = new BufferedReader(new FileReader("gamedata/config"));
|
|
||||||
List<String> lines = reader.lines().toList();
|
List<String> lines = reader.lines().toList();
|
||||||
if(lines.isEmpty()) return; //No config found, use default values
|
if (lines.isEmpty()) return;
|
||||||
for (int i = 0; i < options.length; i++) {
|
Map<String, String> configMap = new HashMap<>();
|
||||||
String value = lines.get(i).split(": ")[1];
|
for (String line : lines) {
|
||||||
switch (options[i]) {
|
String[] parts = line.split(":\\s*", 2);
|
||||||
case "fullscreen" -> panel.fullscreen = Boolean.parseBoolean(value);
|
if (parts.length == 2) {
|
||||||
case "music-vol" -> panel.music.volumeScale = Integer.parseInt(value);
|
configMap.put(parts[0].trim(), parts[1].trim());
|
||||||
case "sfx-vol" -> panel.sfx.volumeScale = Integer.parseInt(value);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
insertToHash();
|
||||||
@@ -58,8 +63,21 @@ public class Config {
|
|||||||
|
|
||||||
// HELP FUNCTIONS
|
// HELP FUNCTIONS
|
||||||
private void insertToHash() {
|
private void insertToHash() {
|
||||||
settings.put(options[0], String.valueOf(panel.fullscreen));
|
settings.put(SETTING.FULLSCREEN.name, String.valueOf(panel.fullscreen));
|
||||||
settings.put(options[1], String.valueOf(panel.music.volumeScale));
|
settings.put(SETTING.MUSICVOLUME.name, String.valueOf(panel.music.volumeScale));
|
||||||
settings.put(options[2], String.valueOf(panel.sfx.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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user