From c273217f6516d38f4895bbdb09cc40c408c9c50f Mon Sep 17 00:00:00 2001 From: yuni Date: Thu, 13 Jun 2024 04:06:15 +0200 Subject: [PATCH] implement noise cancellation modes --- src/data/outfly.toml | 8 ++++++-- src/game.rs | 24 ++++++++++++++---------- src/menu.rs | 13 ++++++++++--- src/var.rs | 39 ++++++++++++++++++++++++++++++--------- 4 files changed, 60 insertions(+), 24 deletions(-) diff --git a/src/data/outfly.toml b/src/data/outfly.toml index 6f0c2dd..16e8bb0 100644 --- a/src/data/outfly.toml +++ b/src/data/outfly.toml @@ -20,9 +20,13 @@ fullscreen_on = true # render_mode may be "vulkan" or "gl" render_mode = "vulkan" +# radio_station can be an integer number +radio_station = 1 + +# noise_cancellation_mode can be an integer number +noise_cancellation_mode = 0 + # The following options are booleans (may be true or false) augmented_reality = true -music = true -sound = true third_person = true shadows_sun = true diff --git a/src/game.rs b/src/game.rs index 7fe545b..0590498 100644 --- a/src/game.rs +++ b/src/game.rs @@ -81,7 +81,7 @@ pub enum AchievementEvent { pub enum GameEvent { SetAR(Turn), SetMusic(Cycle), - SetSound(Turn), + SetSound(Cycle), SetMap(Turn), SetFullscreen(Turn), SetMenu(Turn), @@ -144,7 +144,7 @@ impl Cycle { pub fn setup(mut settings: ResMut, prefs: Res) { settings.hud_active = prefs.augmented_reality; settings.radio_mode = prefs.radio_station; - settings.mute_sfx = !prefs.sound; + settings.set_noise_cancellation_mode(prefs.noise_cancellation_mode); settings.third_person = prefs.third_person; settings.shadows_sun = prefs.shadows_sun; } @@ -172,10 +172,10 @@ pub fn handle_game_event( prefs.augmented_reality = settings.hud_active; prefs.save(); } - GameEvent::SetMusic(turn) => { - match turn.to_index(settings.radio_mode, &settings.radio_modes) { - Some(radio_mode) => { - settings.radio_mode = radio_mode; + GameEvent::SetMusic(cycle) => { + match cycle.to_index(settings.radio_mode, &settings.radio_modes) { + Some(mode) => { + settings.radio_mode = mode; } None => {} } @@ -183,11 +183,15 @@ pub fn handle_game_event( prefs.radio_station = settings.radio_mode; prefs.save(); } - GameEvent::SetSound(turn) => { - // TODO invert "mute_sfx" to "sfx_active" - settings.mute_sfx = turn.to_bool(settings.mute_sfx); + GameEvent::SetSound(cycle) => { + match cycle.to_index(settings.noise_cancellation_mode, &settings.noise_cancellation_modes) { + Some(mode) => { + settings.set_noise_cancellation_mode(mode); + } + None => {} + } ew_togglemusic.send(audio::ToggleMusicEvent()); - prefs.sound = !settings.mute_sfx; + prefs.noise_cancellation_mode = settings.noise_cancellation_mode; prefs.save(); } GameEvent::SetMap(turn) => { diff --git a/src/menu.rs b/src/menu.rs index 01fe741..8e1ca9e 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -458,8 +458,15 @@ pub fn update_menu( match MENUDEF[i].1 { MenuAction::ToggleSound => { - let onoff = bool2string(!settings.mute_sfx); - text.sections[i].value = format!("Sound: {onoff}\n"); + let noisecancel = + if let Some(noisecancel) = + settings.noise_cancellation_modes.get(settings.noise_cancellation_mode) + { + noisecancel + } else { + &settings.noise_cancellation_modes[0] + }; + text.sections[i].value = format!("Noise Cancellation: {noisecancel}\n"); } MenuAction::ToggleMusic => { let station = @@ -575,7 +582,7 @@ pub fn handle_input( ew_updatemenu.send(UpdateMenuEvent); } MenuAction::ToggleSound => { - ew_game.send(GameEvent::SetSound(Toggle)); + ew_game.send(GameEvent::SetSound(Next)); ew_updatemenu.send(UpdateMenuEvent); } MenuAction::ToggleCamera => { diff --git a/src/var.rs b/src/var.rs index 924dec2..0551e5b 100644 --- a/src/var.rs +++ b/src/var.rs @@ -40,6 +40,8 @@ pub struct Settings { pub version: String, pub alive: bool, pub mute_sfx: bool, + pub noise_cancellation_mode: usize, + pub noise_cancellation_modes: Vec, pub radio_mode: usize, pub radio_modes: Vec, // see also: settings.is_radio_playing() pub volume_sfx: u8, @@ -156,7 +158,6 @@ pub struct Settings { impl Default for Settings { fn default() -> Self { let dev_mode = cfg!(feature = "dev_mode") && env::var("CARGO").is_ok(); - let default_mute_sfx = false; let version = if let Some(version) = option_env!("CARGO_PKG_VERSION") { version.to_string() } else { @@ -168,12 +169,19 @@ impl Default for Settings { god_mode: false, version, alive: true, - mute_sfx: default_mute_sfx, - radio_mode: 1, - radio_modes: vec![ // see also: settings.is_radio_playing() + mute_sfx: false, + noise_cancellation_mode: 0, + noise_cancellation_modes: vec![ "Off".to_string(), - "Cinematic".to_string(), - "Vibration Sensor Readings".to_string(), + "Ambience".to_string(), + "Mechanical".to_string(), + "Max".to_string(), + ], + radio_mode: 1, + radio_modes: vec![ + // see also: settings.is_radio_playing() + "Off".to_string(), + "Cinematic Frequency".to_string(), ], volume_sfx: 100, volume_music: 100, @@ -325,12 +333,25 @@ impl Settings { } pub fn is_radio_playing(&self, sfx: audio::Sfx) -> Option { + let radio = self.radio_mode; + let ambience = self.noise_cancellation_mode != 1 && self.noise_cancellation_mode != 3; match sfx { - audio::Sfx::BGM => Some(self.radio_mode == 1), - audio::Sfx::BGMActualJupiterRecording => Some(self.radio_mode == 2), + audio::Sfx::BGM => Some(radio == 1), + audio::Sfx::BGMActualJupiterRecording => Some(radio == 0 && ambience), _ => None, } } + + pub fn set_noise_cancellation_mode(&mut self, value: usize) { + let value = if value >= self.noise_cancellation_modes.len() { + warn!("Attempting to set too large noise cancellation mode: {value}"); + 0 + } else { + value + }; + self.noise_cancellation_mode = value; + self.mute_sfx = value >= 2; + } } #[derive(Resource, Default, Debug)] @@ -432,7 +453,7 @@ pub struct Preferences { pub render_mode: String, pub augmented_reality: bool, pub radio_station: usize, - pub sound: bool, + pub noise_cancellation_mode: usize, pub third_person: bool, pub shadows_sun: bool,