Title screen, pause menu, settings dialog and savegames
- title screen (continue/new/load/settings/help) and Esc pause menu - settings: language & sound, camera, graphics, game, rebindable keys; stored in localStorage and in every savegame (always applied on load) - savegames as JSON (.dcsave): download, load via file picker or drag & drop - autosave to localStorage every 2 minutes and when the tab is hidden/closed - configurable wall detail, light count, interface size, game speed, hints - audio behind a cargo feature so native tests run (cargo test --no-default-features) - savegame round-trip test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fbf6f223fc
commit
fb7dab3683
17 files changed
+1742
-345
No files matched your search
+209
@@ -0,0 +1,209 @@
|
||||
// Player settings: stored in the browser (localStorage "dc.settings") and in every savegame.
|
||||
|
||||
use macroquad::prelude::KeyCode;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
// Rebindable keyboard actions. Esc (menu) and the arrow keys (camera) are fixed.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub enum Action {
|
||||
Tool(usize), // 0..12, same order as ui::TOOLS
|
||||
CamForward,
|
||||
CamBack,
|
||||
CamLeft,
|
||||
CamRight,
|
||||
RotateLeft,
|
||||
RotateRight,
|
||||
TiltUp,
|
||||
TiltDown,
|
||||
Pause,
|
||||
Mute,
|
||||
VolumeUp,
|
||||
VolumeDown,
|
||||
Outlines,
|
||||
Help,
|
||||
}
|
||||
|
||||
pub fn all_actions() -> Vec<Action> {
|
||||
let mut v: Vec<Action> = (0..12).map(Action::Tool).collect();
|
||||
v.extend_from_slice(&[
|
||||
Action::CamForward,
|
||||
Action::CamBack,
|
||||
Action::CamLeft,
|
||||
Action::CamRight,
|
||||
Action::RotateLeft,
|
||||
Action::RotateRight,
|
||||
Action::TiltUp,
|
||||
Action::TiltDown,
|
||||
Action::Pause,
|
||||
Action::Mute,
|
||||
Action::VolumeUp,
|
||||
Action::VolumeDown,
|
||||
Action::Outlines,
|
||||
Action::Help,
|
||||
]);
|
||||
v
|
||||
}
|
||||
|
||||
impl Action {
|
||||
// stable id used in settings files and as language key suffix (key.<id>)
|
||||
pub fn id(self) -> String {
|
||||
match self {
|
||||
Action::Tool(i) => format!("tool{}", i),
|
||||
Action::CamForward => "cam_forward".into(),
|
||||
Action::CamBack => "cam_back".into(),
|
||||
Action::CamLeft => "cam_left".into(),
|
||||
Action::CamRight => "cam_right".into(),
|
||||
Action::RotateLeft => "rotate_left".into(),
|
||||
Action::RotateRight => "rotate_right".into(),
|
||||
Action::TiltUp => "tilt_up".into(),
|
||||
Action::TiltDown => "tilt_down".into(),
|
||||
Action::Pause => "pause".into(),
|
||||
Action::Mute => "mute".into(),
|
||||
Action::VolumeUp => "volume_up".into(),
|
||||
Action::VolumeDown => "volume_down".into(),
|
||||
Action::Outlines => "outlines".into(),
|
||||
Action::Help => "help".into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn default_key(self) -> KeyCode {
|
||||
use KeyCode::*;
|
||||
match self {
|
||||
Action::Tool(i) => [Key1, Key2, Key3, Key4, Key5, Key6, Key7, Key8, Key9, Key0, X, V][i.min(11)],
|
||||
Action::CamForward => W,
|
||||
Action::CamBack => S,
|
||||
Action::CamLeft => A,
|
||||
Action::CamRight => D,
|
||||
Action::RotateLeft => Q,
|
||||
Action::RotateRight => E,
|
||||
Action::TiltUp => R,
|
||||
Action::TiltDown => F,
|
||||
Action::Pause => P,
|
||||
Action::Mute => M,
|
||||
Action::VolumeUp => Equal,
|
||||
Action::VolumeDown => Minus,
|
||||
Action::Outlines => G,
|
||||
Action::Help => H,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Keys that can be bound, with their display names
|
||||
const KEY_NAMES: &[(KeyCode, &str)] = &[
|
||||
(KeyCode::A, "A"), (KeyCode::B, "B"), (KeyCode::C, "C"), (KeyCode::D, "D"), (KeyCode::E, "E"), (KeyCode::F, "F"),
|
||||
(KeyCode::G, "G"), (KeyCode::H, "H"), (KeyCode::I, "I"), (KeyCode::J, "J"), (KeyCode::K, "K"), (KeyCode::L, "L"),
|
||||
(KeyCode::M, "M"), (KeyCode::N, "N"), (KeyCode::O, "O"), (KeyCode::P, "P"), (KeyCode::Q, "Q"), (KeyCode::R, "R"),
|
||||
(KeyCode::S, "S"), (KeyCode::T, "T"), (KeyCode::U, "U"), (KeyCode::V, "V"), (KeyCode::W, "W"), (KeyCode::X, "X"),
|
||||
(KeyCode::Y, "Y"), (KeyCode::Z, "Z"),
|
||||
(KeyCode::Key0, "0"), (KeyCode::Key1, "1"), (KeyCode::Key2, "2"), (KeyCode::Key3, "3"), (KeyCode::Key4, "4"),
|
||||
(KeyCode::Key5, "5"), (KeyCode::Key6, "6"), (KeyCode::Key7, "7"), (KeyCode::Key8, "8"), (KeyCode::Key9, "9"),
|
||||
(KeyCode::Space, "Space"), (KeyCode::Minus, "-"), (KeyCode::Equal, "="), (KeyCode::Comma, ","), (KeyCode::Period, "."),
|
||||
(KeyCode::Slash, "/"), (KeyCode::Semicolon, ";"), (KeyCode::Apostrophe, "'"), (KeyCode::LeftBracket, "["),
|
||||
(KeyCode::RightBracket, "]"), (KeyCode::Backslash, "\\"), (KeyCode::Tab, "Tab"), (KeyCode::Backspace, "Backspace"),
|
||||
(KeyCode::Enter, "Enter"), (KeyCode::Insert, "Insert"), (KeyCode::Delete, "Delete"), (KeyCode::Home, "Home"),
|
||||
(KeyCode::End, "End"), (KeyCode::PageUp, "PageUp"), (KeyCode::PageDown, "PageDown"),
|
||||
(KeyCode::F1, "F1"), (KeyCode::F2, "F2"), (KeyCode::F3, "F3"), (KeyCode::F4, "F4"), (KeyCode::F6, "F6"),
|
||||
(KeyCode::F7, "F7"), (KeyCode::F8, "F8"), (KeyCode::F9, "F9"), (KeyCode::F10, "F10"),
|
||||
(KeyCode::Kp0, "Num0"), (KeyCode::Kp1, "Num1"), (KeyCode::Kp2, "Num2"), (KeyCode::Kp3, "Num3"), (KeyCode::Kp4, "Num4"),
|
||||
(KeyCode::Kp5, "Num5"), (KeyCode::Kp6, "Num6"), (KeyCode::Kp7, "Num7"), (KeyCode::Kp8, "Num8"), (KeyCode::Kp9, "Num9"),
|
||||
(KeyCode::KpAdd, "Num+"), (KeyCode::KpSubtract, "Num-"), (KeyCode::KpMultiply, "Num*"), (KeyCode::KpDivide, "Num/"),
|
||||
(KeyCode::LeftShift, "Shift"), (KeyCode::LeftControl, "Ctrl"), (KeyCode::LeftAlt, "Alt"),
|
||||
];
|
||||
|
||||
pub fn key_name(k: KeyCode) -> String {
|
||||
KEY_NAMES.iter().find(|(c, _)| *c == k).map(|(_, n)| n.to_string()).unwrap_or_else(|| "?".into())
|
||||
}
|
||||
|
||||
fn key_from_name(n: &str) -> Option<KeyCode> {
|
||||
KEY_NAMES.iter().find(|(_, name)| *name == n).map(|(c, _)| *c)
|
||||
}
|
||||
|
||||
// All bindable keys (for the key capture dialog)
|
||||
pub fn bindable_keys() -> impl Iterator<Item = KeyCode> {
|
||||
KEY_NAMES.iter().map(|(k, _)| *k)
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(default)]
|
||||
pub struct Settings {
|
||||
// language & sound
|
||||
pub language: String,
|
||||
pub volume: f32,
|
||||
pub music_volume: f32,
|
||||
pub muted: bool,
|
||||
// camera
|
||||
pub rotate_speed: f32,
|
||||
pub zoom_speed: f32,
|
||||
pub edge_scroll: bool,
|
||||
pub invert_mouse: bool,
|
||||
// graphics
|
||||
pub outlines: bool,
|
||||
pub wall_detail: u8, // 1 low, 2 medium, 3 high
|
||||
pub max_lights: u8, // 8, 16 or 24
|
||||
pub ui_scale: u8, // font scale 2 (normal) or 3 (large)
|
||||
// game
|
||||
pub game_speed: f32, // 1 or 2
|
||||
pub hints: bool,
|
||||
// key bindings: action id -> key name
|
||||
pub keys: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Settings {
|
||||
Settings {
|
||||
language: "en".into(),
|
||||
volume: 0.7,
|
||||
music_volume: 0.6,
|
||||
muted: false,
|
||||
rotate_speed: 1.0,
|
||||
zoom_speed: 1.0,
|
||||
edge_scroll: true,
|
||||
invert_mouse: false,
|
||||
outlines: true,
|
||||
wall_detail: 2,
|
||||
max_lights: 24,
|
||||
ui_scale: 2,
|
||||
game_speed: 1.0,
|
||||
hints: true,
|
||||
keys: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn key(&self, a: Action) -> KeyCode {
|
||||
self.keys.get(&a.id()).and_then(|n| key_from_name(n)).unwrap_or_else(|| a.default_key())
|
||||
}
|
||||
|
||||
// Bind a key; if another action already uses it, the two actions swap keys
|
||||
pub fn bind(&mut self, a: Action, k: KeyCode) {
|
||||
let old = self.key(a);
|
||||
for other in all_actions() {
|
||||
if other != a && self.key(other) == k {
|
||||
self.keys.insert(other.id(), key_name(old));
|
||||
}
|
||||
}
|
||||
self.keys.insert(a.id(), key_name(k));
|
||||
}
|
||||
|
||||
pub fn reset_keys(&mut self) {
|
||||
self.keys.clear();
|
||||
}
|
||||
|
||||
pub fn wall_subdivisions(&self) -> usize {
|
||||
match self.wall_detail {
|
||||
1 => 2,
|
||||
3 => 4,
|
||||
_ => 3,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_json(&self) -> String {
|
||||
serde_json::to_string(self).unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn from_json(s: &str) -> Option<Settings> {
|
||||
serde_json::from_str(s).ok()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user