feat(settings): persisted Music level; SettingsMenu applies via GameIO

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-01 23:08:27 +02:00
parent d0d8648bd0
commit 2bad875df1
5 changed files with 60 additions and 29 deletions

View File

@@ -67,8 +67,7 @@ public final class App {
SaveService saves = new SaveService();
SettingsStore settingsStore = new SettingsStore();
Settings settings = settingsStore.load();
ConsoleIO consoleIo = io instanceof ConsoleIO c ? c : null;
SettingsMenu.apply(settings, consoleIo);
SettingsMenu.apply(settings, io);
boolean running = true;
while (running) {
@@ -84,7 +83,7 @@ public final class App {
}
}
}
case SETTINGS -> settings = SettingsMenu.show(io, settings, settingsStore, consoleIo);
case SETTINGS -> settings = SettingsMenu.show(io, settings, settingsStore);
case QUIT -> running = false;
}
}

View File

@@ -2,50 +2,47 @@ package thb.jeanluc.adventure.menu;
import thb.jeanluc.adventure.io.ConsoleIO;
import thb.jeanluc.adventure.io.GameIO;
import thb.jeanluc.adventure.io.MusicLevel;
import thb.jeanluc.adventure.save.Settings;
import thb.jeanluc.adventure.save.SettingsStore;
import java.util.List;
/** Minimal settings screen: toggle colour and glyph mode; persists + applies. */
/** Settings screen: cycle colour, glyph, and music level; persists + applies. */
public final class SettingsMenu {
private SettingsMenu() {
}
/**
* Loops the settings screen until the player picks "Back", persisting and
* applying each change. Returns the final settings.
*
* @param io active IO
* @param current starting settings
* @param store where to persist
* @param consoleIo the console IO to apply changes to live, or null if not console
*/
public static Settings show(GameIO io, Settings current, SettingsStore store, ConsoleIO consoleIo) {
/** Loops the settings screen until "Back", persisting + applying each change. */
public static Settings show(GameIO io, Settings current, SettingsStore store) {
Settings s = current;
while (true) {
int i = io.choose("SETTINGS", List.of(
"Colour: " + s.colorMode(),
"Glyphs: " + s.glyphMode(),
"Music: " + s.musicLevel(),
"Back"));
if (i == 0) {
s = new Settings(nextColor(s.colorMode()), s.glyphMode());
s = new Settings(nextColor(s.colorMode()), s.glyphMode(), s.musicLevel());
} else if (i == 1) {
s = new Settings(s.colorMode(), nextGlyph(s.glyphMode()));
s = new Settings(s.colorMode(), nextGlyph(s.glyphMode()), s.musicLevel());
} else if (i == 2) {
s = new Settings(s.colorMode(), s.glyphMode(), s.musicLevel().next());
} else {
return s;
}
store.save(s);
apply(s, consoleIo);
apply(s, io);
}
}
/** Applies colour/glyph settings to the console IO (no-op if null). */
public static void apply(Settings s, ConsoleIO consoleIo) {
if (consoleIo != null) {
consoleIo.setColorMode(s.colorMode());
consoleIo.setGlyphMode(s.glyphMode());
/** Applies settings to the IO: music level always; colour/glyph only on the console. */
public static void apply(Settings s, GameIO io) {
io.setMusicLevel(s.musicLevel());
if (io instanceof ConsoleIO c) {
c.setColorMode(s.colorMode());
c.setGlyphMode(s.glyphMode());
}
}

View File

@@ -1,11 +1,23 @@
package thb.jeanluc.adventure.save;
import thb.jeanluc.adventure.io.ConsoleIO;
import thb.jeanluc.adventure.io.MusicLevel;
/** Persisted user preferences (minimal: colour + glyph fidelity). */
public record Settings(ConsoleIO.ColorMode colorMode, ConsoleIO.GlyphMode glyphMode) {
/** Persisted user preferences (colour + glyph fidelity + music level). */
public record Settings(ConsoleIO.ColorMode colorMode, ConsoleIO.GlyphMode glyphMode, MusicLevel musicLevel) {
public Settings {
if (musicLevel == null) {
musicLevel = MusicLevel.MEDIUM; // backward-compat: old saves lack this field
}
}
/** Backward-compatible constructor for callers that don't set music. */
public Settings(ConsoleIO.ColorMode colorMode, ConsoleIO.GlyphMode glyphMode) {
this(colorMode, glyphMode, MusicLevel.MEDIUM);
}
public static Settings defaults() {
return new Settings(ConsoleIO.ColorMode.AUTO, ConsoleIO.GlyphMode.UNICODE);
return new Settings(ConsoleIO.ColorMode.AUTO, ConsoleIO.GlyphMode.UNICODE, MusicLevel.MEDIUM);
}
}