implement noise cancellation modes

This commit is contained in:
yuni 2024-06-13 04:06:15 +02:00
parent 4e8794338a
commit c273217f65
4 changed files with 60 additions and 24 deletions

View file

@ -20,9 +20,13 @@ fullscreen_on = true
# render_mode may be "vulkan" or "gl" # render_mode may be "vulkan" or "gl"
render_mode = "vulkan" 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) # The following options are booleans (may be true or false)
augmented_reality = true augmented_reality = true
music = true
sound = true
third_person = true third_person = true
shadows_sun = true shadows_sun = true

View file

@ -81,7 +81,7 @@ pub enum AchievementEvent {
pub enum GameEvent { pub enum GameEvent {
SetAR(Turn), SetAR(Turn),
SetMusic(Cycle), SetMusic(Cycle),
SetSound(Turn), SetSound(Cycle),
SetMap(Turn), SetMap(Turn),
SetFullscreen(Turn), SetFullscreen(Turn),
SetMenu(Turn), SetMenu(Turn),
@ -144,7 +144,7 @@ impl Cycle {
pub fn setup(mut settings: ResMut<Settings>, prefs: Res<var::Preferences>) { pub fn setup(mut settings: ResMut<Settings>, prefs: Res<var::Preferences>) {
settings.hud_active = prefs.augmented_reality; settings.hud_active = prefs.augmented_reality;
settings.radio_mode = prefs.radio_station; 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.third_person = prefs.third_person;
settings.shadows_sun = prefs.shadows_sun; settings.shadows_sun = prefs.shadows_sun;
} }
@ -172,10 +172,10 @@ pub fn handle_game_event(
prefs.augmented_reality = settings.hud_active; prefs.augmented_reality = settings.hud_active;
prefs.save(); prefs.save();
} }
GameEvent::SetMusic(turn) => { GameEvent::SetMusic(cycle) => {
match turn.to_index(settings.radio_mode, &settings.radio_modes) { match cycle.to_index(settings.radio_mode, &settings.radio_modes) {
Some(radio_mode) => { Some(mode) => {
settings.radio_mode = radio_mode; settings.radio_mode = mode;
} }
None => {} None => {}
} }
@ -183,11 +183,15 @@ pub fn handle_game_event(
prefs.radio_station = settings.radio_mode; prefs.radio_station = settings.radio_mode;
prefs.save(); prefs.save();
} }
GameEvent::SetSound(turn) => { GameEvent::SetSound(cycle) => {
// TODO invert "mute_sfx" to "sfx_active" match cycle.to_index(settings.noise_cancellation_mode, &settings.noise_cancellation_modes) {
settings.mute_sfx = turn.to_bool(settings.mute_sfx); Some(mode) => {
settings.set_noise_cancellation_mode(mode);
}
None => {}
}
ew_togglemusic.send(audio::ToggleMusicEvent()); ew_togglemusic.send(audio::ToggleMusicEvent());
prefs.sound = !settings.mute_sfx; prefs.noise_cancellation_mode = settings.noise_cancellation_mode;
prefs.save(); prefs.save();
} }
GameEvent::SetMap(turn) => { GameEvent::SetMap(turn) => {

View file

@ -458,8 +458,15 @@ pub fn update_menu(
match MENUDEF[i].1 { match MENUDEF[i].1 {
MenuAction::ToggleSound => { MenuAction::ToggleSound => {
let onoff = bool2string(!settings.mute_sfx); let noisecancel =
text.sections[i].value = format!("Sound: {onoff}\n"); 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 => { MenuAction::ToggleMusic => {
let station = let station =
@ -575,7 +582,7 @@ pub fn handle_input(
ew_updatemenu.send(UpdateMenuEvent); ew_updatemenu.send(UpdateMenuEvent);
} }
MenuAction::ToggleSound => { MenuAction::ToggleSound => {
ew_game.send(GameEvent::SetSound(Toggle)); ew_game.send(GameEvent::SetSound(Next));
ew_updatemenu.send(UpdateMenuEvent); ew_updatemenu.send(UpdateMenuEvent);
} }
MenuAction::ToggleCamera => { MenuAction::ToggleCamera => {

View file

@ -40,6 +40,8 @@ pub struct Settings {
pub version: String, pub version: String,
pub alive: bool, pub alive: bool,
pub mute_sfx: bool, pub mute_sfx: bool,
pub noise_cancellation_mode: usize,
pub noise_cancellation_modes: Vec<String>,
pub radio_mode: usize, pub radio_mode: usize,
pub radio_modes: Vec<String>, // see also: settings.is_radio_playing() pub radio_modes: Vec<String>, // see also: settings.is_radio_playing()
pub volume_sfx: u8, pub volume_sfx: u8,
@ -156,7 +158,6 @@ pub struct Settings {
impl Default for Settings { impl Default for Settings {
fn default() -> Self { fn default() -> Self {
let dev_mode = cfg!(feature = "dev_mode") && env::var("CARGO").is_ok(); 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") { let version = if let Some(version) = option_env!("CARGO_PKG_VERSION") {
version.to_string() version.to_string()
} else { } else {
@ -168,12 +169,19 @@ impl Default for Settings {
god_mode: false, god_mode: false,
version, version,
alive: true, alive: true,
mute_sfx: default_mute_sfx, mute_sfx: false,
radio_mode: 1, noise_cancellation_mode: 0,
radio_modes: vec![ // see also: settings.is_radio_playing() noise_cancellation_modes: vec![
"Off".to_string(), "Off".to_string(),
"Cinematic".to_string(), "Ambience".to_string(),
"Vibration Sensor Readings".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_sfx: 100,
volume_music: 100, volume_music: 100,
@ -325,12 +333,25 @@ impl Settings {
} }
pub fn is_radio_playing(&self, sfx: audio::Sfx) -> Option<bool> { pub fn is_radio_playing(&self, sfx: audio::Sfx) -> Option<bool> {
let radio = self.radio_mode;
let ambience = self.noise_cancellation_mode != 1 && self.noise_cancellation_mode != 3;
match sfx { match sfx {
audio::Sfx::BGM => Some(self.radio_mode == 1), audio::Sfx::BGM => Some(radio == 1),
audio::Sfx::BGMActualJupiterRecording => Some(self.radio_mode == 2), audio::Sfx::BGMActualJupiterRecording => Some(radio == 0 && ambience),
_ => None, _ => 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)] #[derive(Resource, Default, Debug)]
@ -432,7 +453,7 @@ pub struct Preferences {
pub render_mode: String, pub render_mode: String,
pub augmented_reality: bool, pub augmented_reality: bool,
pub radio_station: usize, pub radio_station: usize,
pub sound: bool, pub noise_cancellation_mode: usize,
pub third_person: bool, pub third_person: bool,
pub shadows_sun: bool, pub shadows_sun: bool,