From c48e5cdb3aaee4a820a1dc77cce8d9218914d729 Mon Sep 17 00:00:00 2001 From: yuni Date: Sun, 22 Sep 2024 03:40:12 +0200 Subject: [PATCH] add setting for flashlight strength --- doc/gamedesign.md | 2 +- src/actor.rs | 4 +++- src/cmd.rs | 3 ++- src/game.rs | 9 ++++++++- src/menu.rs | 14 ++++++++++++++ src/var.rs | 13 ++++++++----- 6 files changed, 36 insertions(+), 9 deletions(-) diff --git a/doc/gamedesign.md b/doc/gamedesign.md index 1209a3f..935d479 100644 --- a/doc/gamedesign.md +++ b/doc/gamedesign.md @@ -113,7 +113,7 @@ A variety of relatively simple game systems should interact with each other to c - Spacesuit energy distribution into: - [X] Residual Light Amplification - [X] Augmented Reality - - [ ] Flashlight intensity + - [X] Flashlight intensity - [ ] AI assistance systems - [ ] Thruster boost - [ ] G-force dampeners diff --git a/src/actor.rs b/src/actor.rs index 3e285a5..ead1691 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -21,6 +21,8 @@ use bevy_xpbd_3d::prelude::*; pub const ENGINE_SPEED_FACTOR: f32 = 30.0; const MAX_TRANSMISSION_DISTANCE: f32 = 100.0; const MAX_INTERACT_DISTANCE: f32 = 50.0; +const POWER_DRAIN_FLASHLIGHT: [f32; 3] = [200e3, 1500e3, 2500e3]; +pub const FLASHLIGHT_INTENSITY: [f32; 3] = [10e6, 400e6, 2e9]; // in lumens pub struct ActorPlugin; impl Plugin for ActorPlugin { @@ -272,7 +274,7 @@ pub fn update_power( let d = time.delta_seconds(); for mut battery in &mut q_battery { if settings.flashlight_active { - battery.power -= 2400e3 * d; // 2.4MW + battery.power -= POWER_DRAIN_FLASHLIGHT[prefs.flashlight_power] * d; // 2.4MW if battery.power <= 0.0 { settings.flashlight_active = false; ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Switch)); diff --git a/src/cmd.rs b/src/cmd.rs index d76b9e9..e6911d8 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -816,6 +816,7 @@ fn spawn_entities( mut achievement_tracker: ResMut, mut ew_updateavatar: EventWriter, settings: Res, + prefs: Res, ) { for state_wrapper in er_spawn.read() { let state = &state_wrapper.0; @@ -1073,7 +1074,7 @@ fn spawn_entities( ..default() }, spot_light: SpotLight { - intensity: 400_000_000.0, // lumens + intensity: actor::FLASHLIGHT_INTENSITY[prefs.flashlight_power], color: Color::WHITE, shadows_enabled: true, inner_angle: PI32 / 8.0 * 0.85, diff --git a/src/game.rs b/src/game.rs index 793bda6..bbf0287 100644 --- a/src/game.rs +++ b/src/game.rs @@ -88,6 +88,7 @@ pub enum GameEvent { SetThirdPerson(Turn), SetRotationStabilizer(Turn), SetShadows(Turn), + UpdateFlashlight, Achievement(String), } @@ -141,7 +142,7 @@ impl Cycle { } } -pub fn setup(mut settings: ResMut, prefs: Res) { +pub fn setup(mut settings: ResMut, prefs: ResMut) { settings.hud_active = prefs.augmented_reality; settings.radio_mode = prefs.radio_station; settings.set_noise_cancellation_mode(prefs.noise_cancellation_mode); @@ -159,6 +160,7 @@ pub fn handle_game_event( mut ew_togglemusic: EventWriter, mut q_window: Query<&mut Window, With>, mut q_light: Query<&mut DirectionalLight>, + mut q_flashlight: Query<&mut SpotLight, With>, mut mapcam: ResMut, mut log: ResMut, opt: Res, @@ -255,6 +257,11 @@ pub fn handle_game_event( hud::LogLevel::Achievement, ); } + GameEvent::UpdateFlashlight => { + for mut spotlight in &mut q_flashlight { + spotlight.intensity = actor::FLASHLIGHT_INTENSITY[prefs.flashlight_power]; + } + } } } } diff --git a/src/menu.rs b/src/menu.rs index 1fdf352..c0f1085 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -67,6 +67,7 @@ pub const MENUDEF: &[(&str, MenuAction)] = &[ ("", MenuAction::ToggleAR), ("", MenuAction::ChangeARAvatar), ("", MenuAction::ModLightAmp), + ("", MenuAction::ModFlashlightPower), ("", MenuAction::ToggleSound), ("", MenuAction::ToggleMusic), ("", MenuAction::ToggleCamera), @@ -82,6 +83,7 @@ pub enum MenuAction { ToggleAR, ChangeARAvatar, ModLightAmp, + ModFlashlightPower, ToggleSound, ToggleMusic, ToggleCamera, @@ -488,6 +490,10 @@ pub fn update_menu( let n = prefs.light_amp; text.sections[i].value = format!("Light Amplification: {n}/3\n"); } + MenuAction::ModFlashlightPower => { + let n = prefs.flashlight_power + 1; + text.sections[i].value = format!("Flashlight Power: {n}/3\n"); + } MenuAction::ChangeARAvatar => { if let Some(ava) = hud::PLAYER_AR_AVATARS.get(settings.ar_avatar) { let avatar_title = ava.3; @@ -594,6 +600,14 @@ pub fn handle_input( ew_updateoverlays.send(hud::UpdateOverlayVisibility); ew_updatemenu.send(UpdateMenuEvent); } + MenuAction::ModFlashlightPower => { + prefs.flashlight_power += 1; + if prefs.flashlight_power > 2 { + prefs.flashlight_power = 0; + } + ew_game.send(GameEvent::UpdateFlashlight); + ew_updatemenu.send(UpdateMenuEvent); + } MenuAction::ToggleMusic => { ew_game.send(GameEvent::SetMusic(Next)); ew_updatemenu.send(UpdateMenuEvent); diff --git a/src/var.rs b/src/var.rs index 3c66698..7ec6935 100644 --- a/src/var.rs +++ b/src/var.rs @@ -453,6 +453,7 @@ pub struct Preferences { pub shadows_sun: bool, pub avatar: usize, pub light_amp: usize, // 0-3 + pub flashlight_power: usize, // 0-2 #[serde(skip)] pub source_file: Option, @@ -566,23 +567,25 @@ pub fn load_prefs() -> Preferences { }; match toml.parse::() { Ok(doc) => match toml_edit::de::from_document::(doc) { - Ok(mut pref) => { + Ok(mut prefs) => { if let Some(path) = &path { println!("Loaded preference file from {path}"); } else { println!("Loaded preferences from internal defaults"); } - pref.source_file = path; - return pref; + prefs.source_file = path; + prefs.flashlight_power = prefs.flashlight_power.clamp(0, 2); + prefs.light_amp = prefs.light_amp.clamp(0, 3); + prefs } Err(error) => { eprintln!("Error: Failed to read preference line: {error}"); - return Preferences::default(); + Preferences::default() } }, Err(error) => { eprintln!("Error: Failed to open preferences: {error}"); - return Preferences::default(); + Preferences::default() } } }