reset audio on death/respawn

This commit is contained in:
yuni 2024-05-13 04:41:17 +02:00
parent ea25c7fed3
commit 48476e317f
2 changed files with 23 additions and 3 deletions

View file

@ -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::<RespawnSinksEvent>()));
app.add_systems(Update, play_zoom_sfx);
app.add_systems(Update, pause_all.run_if(on_event::<PauseAllSfxEvent>()));
app.add_systems(PostUpdate, play_sfx);
@ -27,6 +28,7 @@ impl Plugin for AudioPlugin {
app.add_event::<PlaySfxEvent>();
app.add_event::<PauseAllSfxEvent>();
app.add_event::<ToggleMusicEvent>();
app.add_event::<RespawnSinksEvent>();
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<Sfx, Handle<AudioSource>>);
pub fn setup(
mut commands: Commands,
settings: Res<var::Settings>,
asset_server: Res<AssetServer>,
mut ew_respawnsinks: EventWriter<RespawnSinksEvent>,
) {
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<AssetServer>,
settings: Res<var::Settings>,
q_audiosinks: Query<Entity, (With<AudioSink>, With<Sfx>)>,
) {
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(

View file

@ -108,6 +108,7 @@ pub fn show_deathscreen(
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
mut ew_effect: EventWriter<visual::SpawnEffectEvent>,
mut ew_respawn: EventWriter<world::RespawnEvent>,
mut ew_respawnaudiosinks: EventWriter<audio::RespawnSinksEvent>,
mut settings: ResMut<Settings>,
) {
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);