From 4e8794338ae807ef7ad8e60c4e05c64c36daa0bc Mon Sep 17 00:00:00 2001 From: yuni Date: Thu, 13 Jun 2024 03:41:15 +0200 Subject: [PATCH] fix wrong radio playing on startup --- src/audio.rs | 42 +++++++++++------------------------------- src/var.rs | 12 ++++++++++-- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/audio.rs b/src/audio.rs index e0b2e4c..2a6c11e 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -48,12 +48,12 @@ pub struct ZoomTimer(Timer); const PATHS: &[(SfxType, Sfx, &str)] = &[ ( - SfxType::BGM, + SfxType::Radio, Sfx::BGM, "music/Aleksey Chistilin - Cinematic Cello.ogg", ), ( - SfxType::BGMNoAR, + SfxType::Radio, Sfx::BGMActualJupiterRecording, "music/JupiterRecording.ogg", ), @@ -132,8 +132,7 @@ pub fn str2sfx(sfx_label: &str) -> Sfx { } pub enum SfxType { - BGM, - BGMNoAR, + Radio, LoopSfx, OneOff, } @@ -167,7 +166,7 @@ pub fn respawn_sinks( mut commands: Commands, asset_server: Res, q_audiosinks: Query, With)>, - mut ew_togglemusic: EventWriter, + settings: Res, ) { for sink in &q_audiosinks { commands.entity(sink).despawn(); @@ -175,27 +174,14 @@ pub fn respawn_sinks( for (sfxtype, sfx, path) in PATHS { let source = asset_server.load(*path); match sfxtype { - SfxType::BGM => { + SfxType::Radio => { commands.spawn(( *sfx, AudioBundle { source, settings: PlaybackSettings { mode: PlaybackMode::Loop, - paused: true, - ..default() - }, - }, - )); - } - SfxType::BGMNoAR => { - commands.spawn(( - *sfx, - AudioBundle { - source, - settings: PlaybackSettings { - mode: PlaybackMode::Loop, - paused: true, + paused: !settings.is_radio_playing(*sfx).unwrap_or(true), ..default() }, }, @@ -218,7 +204,6 @@ pub fn respawn_sinks( SfxType::OneOff => (), } } - ew_togglemusic.send(ToggleMusicEvent()); } pub fn play_sfx( @@ -242,17 +227,12 @@ pub fn play_sfx( pub fn toggle_music(q_audiosinks: Query<(&AudioSink, &Sfx)>, settings: Res) { for (bgm_sink, sfx) in &q_audiosinks { - let play = match *sfx { - Sfx::BGM => settings.radio_mode == 1, - Sfx::BGMActualJupiterRecording => settings.radio_mode == 2, - _ => { - continue; + if let Some(play) = settings.is_radio_playing(*sfx) { + if play { + bgm_sink.play(); + } else { + bgm_sink.pause(); } - }; - if play { - bgm_sink.play(); - } else { - bgm_sink.pause(); } } } diff --git a/src/var.rs b/src/var.rs index dcfd0a2..924dec2 100644 --- a/src/var.rs +++ b/src/var.rs @@ -41,7 +41,7 @@ pub struct Settings { pub alive: bool, pub mute_sfx: bool, pub radio_mode: usize, - pub radio_modes: Vec, + pub radio_modes: Vec, // see also: settings.is_radio_playing() pub volume_sfx: u8, pub volume_music: u8, pub mouse_sensitivity: f32, @@ -170,7 +170,7 @@ impl Default for Settings { alive: true, mute_sfx: default_mute_sfx, radio_mode: 1, - radio_modes: vec![ + radio_modes: vec![ // see also: settings.is_radio_playing() "Off".to_string(), "Cinematic".to_string(), "Vibration Sensor Readings".to_string(), @@ -323,6 +323,14 @@ impl Settings { pub fn in_control(&self) -> bool { return self.alive && !self.menu_active; } + + pub fn is_radio_playing(&self, sfx: audio::Sfx) -> Option { + match sfx { + audio::Sfx::BGM => Some(self.radio_mode == 1), + audio::Sfx::BGMActualJupiterRecording => Some(self.radio_mode == 2), + _ => None, + } + } } #[derive(Resource, Default, Debug)]