fix wrong radio playing on startup

This commit is contained in:
yuni 2024-06-13 03:41:15 +02:00
parent 8c7a856717
commit 4e8794338a
2 changed files with 21 additions and 33 deletions

View file

@ -48,12 +48,12 @@ pub struct ZoomTimer(Timer);
const PATHS: &[(SfxType, Sfx, &str)] = &[ const PATHS: &[(SfxType, Sfx, &str)] = &[
( (
SfxType::BGM, SfxType::Radio,
Sfx::BGM, Sfx::BGM,
"music/Aleksey Chistilin - Cinematic Cello.ogg", "music/Aleksey Chistilin - Cinematic Cello.ogg",
), ),
( (
SfxType::BGMNoAR, SfxType::Radio,
Sfx::BGMActualJupiterRecording, Sfx::BGMActualJupiterRecording,
"music/JupiterRecording.ogg", "music/JupiterRecording.ogg",
), ),
@ -132,8 +132,7 @@ pub fn str2sfx(sfx_label: &str) -> Sfx {
} }
pub enum SfxType { pub enum SfxType {
BGM, Radio,
BGMNoAR,
LoopSfx, LoopSfx,
OneOff, OneOff,
} }
@ -167,7 +166,7 @@ pub fn respawn_sinks(
mut commands: Commands, mut commands: Commands,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
q_audiosinks: Query<Entity, (With<AudioSink>, With<Sfx>)>, q_audiosinks: Query<Entity, (With<AudioSink>, With<Sfx>)>,
mut ew_togglemusic: EventWriter<ToggleMusicEvent>, settings: Res<Settings>,
) { ) {
for sink in &q_audiosinks { for sink in &q_audiosinks {
commands.entity(sink).despawn(); commands.entity(sink).despawn();
@ -175,27 +174,14 @@ pub fn respawn_sinks(
for (sfxtype, sfx, path) in PATHS { for (sfxtype, sfx, path) in PATHS {
let source = asset_server.load(*path); let source = asset_server.load(*path);
match sfxtype { match sfxtype {
SfxType::BGM => { SfxType::Radio => {
commands.spawn(( commands.spawn((
*sfx, *sfx,
AudioBundle { AudioBundle {
source, source,
settings: PlaybackSettings { settings: PlaybackSettings {
mode: PlaybackMode::Loop, mode: PlaybackMode::Loop,
paused: true, paused: !settings.is_radio_playing(*sfx).unwrap_or(true),
..default()
},
},
));
}
SfxType::BGMNoAR => {
commands.spawn((
*sfx,
AudioBundle {
source,
settings: PlaybackSettings {
mode: PlaybackMode::Loop,
paused: true,
..default() ..default()
}, },
}, },
@ -218,7 +204,6 @@ pub fn respawn_sinks(
SfxType::OneOff => (), SfxType::OneOff => (),
} }
} }
ew_togglemusic.send(ToggleMusicEvent());
} }
pub fn play_sfx( pub fn play_sfx(
@ -242,13 +227,7 @@ pub fn play_sfx(
pub fn toggle_music(q_audiosinks: Query<(&AudioSink, &Sfx)>, settings: Res<var::Settings>) { pub fn toggle_music(q_audiosinks: Query<(&AudioSink, &Sfx)>, settings: Res<var::Settings>) {
for (bgm_sink, sfx) in &q_audiosinks { for (bgm_sink, sfx) in &q_audiosinks {
let play = match *sfx { if let Some(play) = settings.is_radio_playing(*sfx) {
Sfx::BGM => settings.radio_mode == 1,
Sfx::BGMActualJupiterRecording => settings.radio_mode == 2,
_ => {
continue;
}
};
if play { if play {
bgm_sink.play(); bgm_sink.play();
} else { } else {
@ -256,6 +235,7 @@ pub fn toggle_music(q_audiosinks: Query<(&AudioSink, &Sfx)>, settings: Res<var::
} }
} }
} }
}
pub fn play_zoom_sfx( pub fn play_zoom_sfx(
time: Res<Time>, time: Res<Time>,

View file

@ -41,7 +41,7 @@ pub struct Settings {
pub alive: bool, pub alive: bool,
pub mute_sfx: bool, pub mute_sfx: bool,
pub radio_mode: usize, pub radio_mode: usize,
pub radio_modes: Vec<String>, pub radio_modes: Vec<String>, // see also: settings.is_radio_playing()
pub volume_sfx: u8, pub volume_sfx: u8,
pub volume_music: u8, pub volume_music: u8,
pub mouse_sensitivity: f32, pub mouse_sensitivity: f32,
@ -170,7 +170,7 @@ impl Default for Settings {
alive: true, alive: true,
mute_sfx: default_mute_sfx, mute_sfx: default_mute_sfx,
radio_mode: 1, radio_mode: 1,
radio_modes: vec![ radio_modes: vec![ // see also: settings.is_radio_playing()
"Off".to_string(), "Off".to_string(),
"Cinematic".to_string(), "Cinematic".to_string(),
"Vibration Sensor Readings".to_string(), "Vibration Sensor Readings".to_string(),
@ -323,6 +323,14 @@ impl Settings {
pub fn in_control(&self) -> bool { pub fn in_control(&self) -> bool {
return self.alive && !self.menu_active; return self.alive && !self.menu_active;
} }
pub fn is_radio_playing(&self, sfx: audio::Sfx) -> Option<bool> {
match sfx {
audio::Sfx::BGM => Some(self.radio_mode == 1),
audio::Sfx::BGMActualJupiterRecording => Some(self.radio_mode == 2),
_ => None,
}
}
} }
#[derive(Resource, Default, Debug)] #[derive(Resource, Default, Debug)]