outfly/src/var.rs

283 lines
8.8 KiB
Rust
Raw Normal View History

2024-03-16 21:20:23 +00:00
use bevy::prelude::*;
2024-04-14 13:37:36 +00:00
use std::collections::HashMap;
use std::env;
2024-03-16 21:20:23 +00:00
2024-04-14 14:20:51 +00:00
pub const SCOPE_SEPARATOR: &str = "$";
2024-04-12 23:34:18 +00:00
pub const DEFAULT_CHAT_SPEED: f32 = 10.0;
2024-03-17 17:26:44 +00:00
#[derive(Resource)]
2024-03-16 21:20:23 +00:00
pub struct Settings {
2024-03-30 20:14:04 +00:00
pub dev_mode: bool,
2024-04-07 23:44:36 +00:00
pub god_mode: bool,
2024-03-16 21:20:23 +00:00
pub mute_sfx: bool,
pub mute_music: bool,
pub volume_sfx: u8,
pub volume_music: u8,
2024-03-30 15:27:56 +00:00
pub mouse_sensitivity: f32,
2024-04-08 01:15:45 +00:00
pub fov: f32,
pub fov_highspeed: f32,
pub zoom_fov: f32,
2024-04-05 21:38:20 +00:00
pub zoom_sensitivity_factor: f32,
2024-03-17 19:31:16 +00:00
pub font_size_hud: f32,
pub font_size_conversations: f32,
2024-04-12 23:34:18 +00:00
pub chat_speed: f32,
pub hud_active: bool,
2024-04-05 21:38:20 +00:00
pub is_zooming: bool,
pub third_person: bool,
2024-04-11 19:06:21 +00:00
pub rotation_stabilizer_active: bool,
pub key_selectobject: MouseButton,
2024-04-05 21:38:20 +00:00
pub key_zoom: MouseButton,
2024-03-18 03:39:26 +00:00
pub key_togglehud: KeyCode,
pub key_exit: KeyCode,
pub key_restart: KeyCode,
pub key_fullscreen: KeyCode,
pub key_forward: KeyCode,
pub key_back: KeyCode,
pub key_left: KeyCode,
pub key_right: KeyCode,
pub key_up: KeyCode,
pub key_down: KeyCode,
pub key_run: KeyCode,
pub key_stop: KeyCode,
pub key_interact: KeyCode,
pub key_vehicle: KeyCode,
pub key_camera: KeyCode,
2024-03-30 18:14:59 +00:00
pub key_rotate: KeyCode,
2024-04-11 19:06:21 +00:00
pub key_rotation_stabilizer: KeyCode,
pub key_mouseup: KeyCode,
pub key_mousedown: KeyCode,
pub key_mouseleft: KeyCode,
pub key_mouseright: KeyCode,
pub key_rotateleft: KeyCode,
pub key_rotateright: KeyCode,
2024-03-20 01:03:42 +00:00
pub key_reply1: KeyCode,
pub key_reply2: KeyCode,
pub key_reply3: KeyCode,
pub key_reply4: KeyCode,
pub key_reply5: KeyCode,
pub key_reply6: KeyCode,
pub key_reply7: KeyCode,
pub key_reply8: KeyCode,
pub key_reply9: KeyCode,
pub key_reply10: KeyCode,
2024-04-07 23:44:36 +00:00
pub key_cheat_god_mode: KeyCode,
2024-04-01 03:25:35 +00:00
pub key_cheat_stop: KeyCode,
pub key_cheat_speed: KeyCode,
pub key_cheat_speed_backward: KeyCode,
2024-04-01 03:25:35 +00:00
pub key_cheat_pizza: KeyCode,
pub key_cheat_farview1: KeyCode,
pub key_cheat_farview2: KeyCode,
pub key_cheat_adrenaline_zero: KeyCode,
pub key_cheat_adrenaline_mid: KeyCode,
pub key_cheat_adrenaline_max: KeyCode,
2024-04-05 00:58:02 +00:00
pub key_cheat_die: KeyCode,
2024-03-16 21:20:23 +00:00
}
impl Default for Settings {
fn default() -> Self {
2024-03-30 20:14:04 +00:00
let dev_mode;
2024-03-28 19:54:34 +00:00
let default_mute_sfx = false;
let default_mute_music;
if let Ok(_) = env::var("CARGO") {
// Mute audio by default when run through `cargo`
default_mute_music = cfg!(debug_assertions);
2024-03-30 20:14:04 +00:00
// Enable dev mode when running `cargo run` without `--release`
dev_mode = cfg!(debug_assertions);
}
else {
default_mute_music = false;
2024-03-30 20:14:04 +00:00
dev_mode = false;
}
2024-03-22 11:08:00 +00:00
2024-03-16 21:20:23 +00:00
Settings {
2024-04-14 02:56:34 +00:00
dev_mode,
2024-04-07 23:44:36 +00:00
god_mode: false,
2024-03-22 11:08:00 +00:00
mute_sfx: default_mute_sfx,
mute_music: default_mute_music,
2024-03-16 21:20:23 +00:00
volume_sfx: 100,
volume_music: 100,
2024-04-08 01:15:45 +00:00
mouse_sensitivity: 0.7,
fov: 50.0,
fov_highspeed: 25.0,
2024-04-08 02:16:01 +00:00
zoom_fov: 15.0,
2024-04-08 00:17:36 +00:00
zoom_sensitivity_factor: 0.25,
2024-03-17 19:31:16 +00:00
font_size_hud: 32.0,
font_size_conversations: 32.0,
2024-04-12 23:34:18 +00:00
chat_speed: DEFAULT_CHAT_SPEED,
2024-03-18 01:15:44 +00:00
hud_active: false,
2024-04-05 21:38:20 +00:00
is_zooming: false,
third_person: false,
2024-04-11 19:06:21 +00:00
rotation_stabilizer_active: true,
key_selectobject: MouseButton::Left,
2024-04-05 21:38:20 +00:00
key_zoom: MouseButton::Right,
2024-03-18 03:39:26 +00:00
key_togglehud: KeyCode::Tab,
key_exit: KeyCode::Escape,
2024-04-11 18:47:11 +00:00
key_restart: KeyCode::F7,
2024-03-18 03:39:26 +00:00
key_fullscreen: KeyCode::F11,
key_forward: KeyCode::KeyW,
key_back: KeyCode::KeyS,
key_left: KeyCode::KeyA,
key_right: KeyCode::KeyD,
key_up: KeyCode::ShiftLeft,
key_down: KeyCode::ControlLeft,
key_run: KeyCode::KeyR,
key_stop: KeyCode::Space,
key_interact: KeyCode::KeyE,
key_vehicle: KeyCode::KeyQ,
key_camera: KeyCode::KeyF,
2024-03-30 18:14:59 +00:00
key_rotate: KeyCode::KeyR,
2024-04-11 19:06:21 +00:00
key_rotation_stabilizer: KeyCode::KeyY,
key_mouseup: KeyCode::KeyI,
key_mousedown: KeyCode::KeyK,
key_mouseleft: KeyCode::KeyJ,
key_mouseright: KeyCode::KeyL,
key_rotateleft: KeyCode::KeyU,
key_rotateright: KeyCode::KeyO,
2024-03-20 01:03:42 +00:00
key_reply1: KeyCode::Digit1,
key_reply2: KeyCode::Digit2,
key_reply3: KeyCode::Digit3,
key_reply4: KeyCode::Digit4,
key_reply5: KeyCode::Digit5,
key_reply6: KeyCode::Digit6,
key_reply7: KeyCode::Digit7,
key_reply8: KeyCode::Digit8,
key_reply9: KeyCode::Digit9,
key_reply10: KeyCode::Digit0,
2024-04-07 23:44:36 +00:00
key_cheat_god_mode: KeyCode::KeyG,
2024-04-01 03:25:35 +00:00
key_cheat_stop: KeyCode::KeyC,
key_cheat_speed: KeyCode::KeyV,
key_cheat_speed_backward: KeyCode::KeyB,
2024-04-11 18:47:11 +00:00
key_cheat_pizza: KeyCode::F9,
key_cheat_farview1: KeyCode::F10,
key_cheat_farview2: KeyCode::F12,
2024-04-01 03:25:35 +00:00
key_cheat_adrenaline_zero: KeyCode::F5,
key_cheat_adrenaline_mid: KeyCode::F6,
2024-04-11 18:47:11 +00:00
key_cheat_adrenaline_max: KeyCode::F8,
2024-04-05 00:58:02 +00:00
key_cheat_die: KeyCode::KeyZ,
2024-03-16 21:20:23 +00:00
}
}
}
impl Settings {
2024-04-11 19:20:54 +00:00
#[allow(dead_code)]
2024-03-16 21:20:23 +00:00
pub fn reset(&mut self) {
println!("Resetting settings!");
*self = Self::default();
}
2024-03-20 01:03:42 +00:00
2024-04-11 19:20:54 +00:00
pub fn reset_player_settings(&mut self) {
println!("Resetting player settings!");
let default = Self::default();
self.rotation_stabilizer_active = default.rotation_stabilizer_active;
self.third_person = default.third_person;
self.is_zooming = default.is_zooming;
}
2024-03-20 01:03:42 +00:00
pub fn get_reply_keys(&self) -> [KeyCode; 10] {
return [
self.key_reply1,
self.key_reply2,
self.key_reply3,
self.key_reply4,
self.key_reply5,
self.key_reply6,
self.key_reply7,
self.key_reply8,
self.key_reply9,
self.key_reply10,
];
}
2024-03-16 21:20:23 +00:00
}
2024-04-14 13:37:36 +00:00
#[derive(Resource)]
2024-04-14 14:20:51 +00:00
pub struct GameVars {
2024-04-14 13:37:36 +00:00
pub db: HashMap<String, String>,
}
2024-04-14 14:20:51 +00:00
impl Default for GameVars {
fn default() -> Self {
Self {
db: HashMap::new(),
}
}
}
2024-04-14 13:37:36 +00:00
impl GameVars {
#[allow(dead_code)]
2024-04-14 14:20:51 +00:00
pub fn get(&self, key: &str) -> Option<String> {
2024-04-14 13:37:36 +00:00
if let Some(value) = self.db.get(key) {
return Some(value.clone());
}
return None;
}
#[allow(dead_code)]
2024-04-14 14:20:51 +00:00
pub fn getf(&self, key: &str) -> Option<f64> {
2024-04-14 13:37:36 +00:00
if let Some(value) = self.db.get(key) {
if let Ok(float) = value.parse::<f64>() {
return Some(float);
}
}
return None;
}
#[allow(dead_code)]
2024-04-14 14:20:51 +00:00
pub fn getb(&self, key: &str) -> bool {
2024-04-14 13:37:36 +00:00
if let Some(value) = self.db.get(key) {
return value != "0";
}
return false;
}
#[allow(dead_code)]
2024-04-14 14:20:51 +00:00
pub fn set(&mut self, key: &str, value: String) {
self.db.insert(key.to_lowercase(), value);
}
// This method ensures that the variable name contains a scope separator,
// and if a scope is missing, it prefixes the fallback scope.
//
// Some examples, assuming fallback_scope="Clippy", SCOPE_SEPARATOR="$":
//
// "" -> "clippy$"
// "foo" -> "clippy$foo"
// "FOO" -> "clippy$foo"
// "$foo" -> "clippy$foo"
// "$$foo" -> "$$foo"
// "PizzaClippy$foo" -> "pizzaclippy$foo" (unchanged)
// "$foo$foo$foo$foo" -> "$foo$foo$foo$foo" (unchanged)
pub fn normalize_varname(fallback_scope: &str, key: &str) -> String {
let parts: Vec<&str> = key.split(SCOPE_SEPARATOR).collect();
let key: String = if parts.len() == 1 {
// we got a key like "foo", turn it into "<scope>$foo"
fallback_scope.to_string() + SCOPE_SEPARATOR + key
} else if parts.len() > 1 {
// we got a key with at least one "$"
// extract anything before the last "$":
let scope_part: String = parts[0..parts.len() - 2].join(SCOPE_SEPARATOR);
if scope_part.is_empty() {
// we got a key like "$foo", just prefix the fallback scope
fallback_scope.to_string() + key
}
else {
// we got a key like "Ke$ha$foo" or "$$foo" (which is the convention for
// global variables), leave the scope intact
key.to_string()
}
2024-04-14 14:20:51 +00:00
} else {
// we got an empty string. this is bad, but handle gracefully
fallback_scope.to_string() + SCOPE_SEPARATOR
2024-04-14 14:20:51 +00:00
};
return key.to_lowercase();
}
pub fn set_in_scope(&mut self, fallback_scope: &str, key: &str, value: String) {
let key = Self::normalize_varname(fallback_scope, key);
2024-04-14 14:20:51 +00:00
self.db.insert(key, value);
2024-04-14 13:37:36 +00:00
}
}