From 48476e317f3f6dc290ffa7060f4010cfc61f4edb Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 13 May 2024 04:41:17 +0200 Subject: [PATCH] reset audio on death/respawn --- src/audio.rs | 24 +++++++++++++++++++++--- src/menu.rs | 2 ++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/audio.rs b/src/audio.rs index ccbe27f..c217ca5 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -20,6 +20,7 @@ impl Plugin for AudioPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, setup); app.add_systems(Update, toggle_bgm); + app.add_systems(Update, respawn_sinks.run_if(on_event::())); app.add_systems(Update, play_zoom_sfx); app.add_systems(Update, pause_all.run_if(on_event::())); app.add_systems(PostUpdate, play_sfx); @@ -27,6 +28,7 @@ impl Plugin for AudioPlugin { app.add_event::(); app.add_event::(); app.add_event::(); + app.add_event::(); app.insert_resource(ZoomTimer( Timer::from_seconds(0.09, TimerMode::Repeating))); } @@ -94,18 +96,35 @@ pub enum SfxType { #[derive(Event)] pub struct PlaySfxEvent(pub Sfx); #[derive(Event)] pub struct PauseAllSfxEvent; +#[derive(Event)] pub struct RespawnSinksEvent; #[derive(Event)] pub struct ToggleMusicEvent(); #[derive(Resource)] pub struct Sounds(HashMap>); pub fn setup( mut commands: Commands, - settings: Res, asset_server: Res, + mut ew_respawnsinks: EventWriter, ) { let mut map = HashMap::new(); - for (sfxtype, sfx, path) in PATHS { + for (_, sfx, path) in PATHS { let source = asset_server.load(*path); map.insert(*sfx, source.clone()); + } + commands.insert_resource(Sounds(map)); + ew_respawnsinks.send(RespawnSinksEvent); +} + +pub fn respawn_sinks( + mut commands: Commands, + asset_server: Res, + settings: Res, + q_audiosinks: Query, With)>, +) { + for sink in &q_audiosinks { + commands.entity(sink).despawn(); + } + for (sfxtype, sfx, path) in PATHS { + let source = asset_server.load(*path); match sfxtype { SfxType::BGM => { commands.spawn(( @@ -137,7 +156,6 @@ pub fn setup( SfxType::OneOff => () } } - commands.insert_resource(Sounds(map)); } pub fn toggle_bgm( diff --git a/src/menu.rs b/src/menu.rs index 462e401..ba32eeb 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -108,6 +108,7 @@ pub fn show_deathscreen( mut ew_sfx: EventWriter, mut ew_effect: EventWriter, mut ew_respawn: EventWriter, + mut ew_respawnaudiosinks: EventWriter, mut settings: ResMut, ) { for event in er_deathscreen.read() { @@ -129,6 +130,7 @@ pub fn show_deathscreen( text.sections[3].value = settings.death_cause.clone(); } } else { + ew_respawnaudiosinks.send(audio::RespawnSinksEvent); ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::WakeUp)); ew_effect.send(visual::SpawnEffectEvent { class: visual::Effects::FadeIn(Color::BLACK), duration: 0.3 }); ew_respawn.send(world::RespawnEvent);