Skip to content

Commit

Permalink
Add player death SFX
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Jul 28, 2024
1 parent b7f0ee5 commit 5c12458
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/game/actor/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::game::actor::faction::Faction;
use crate::game::actor::movement::input::movement_action;
use crate::game::actor::ActorConfig;
use crate::game::combat::damage::HitboxDamage;
use crate::game::combat::death::DeathSfx;
use crate::game::combat::hit::Hitbox;
use crate::game::combat::hit::HurtSfx;
use crate::game::combat::knockback::HitboxKnockback;
Expand Down Expand Up @@ -62,6 +63,7 @@ pub fn player(key: impl Into<String>) -> impl EntityCommand {
HitboxDamage(15.0),
HitboxKnockback(5.0),
HurtSfx,
DeathSfx,
))
.set_parent(parent)
.with_children(|children| {
Expand Down
29 changes: 28 additions & 1 deletion src/game/combat/death.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use bevy::prelude::*;
use bevy_kira_audio::prelude::*;

use crate::screen::playing::PlayingAssets;
use crate::util::prelude::*;

pub(super) fn plugin(app: &mut App) {
app.configure::<(IsDead, DespawnOnDeath)>();
app.configure::<(IsDead, DespawnOnDeath, DeathSfx)>();
}

/// An observable event on an actor's death.
Expand Down Expand Up @@ -41,3 +43,28 @@ impl Configure for DespawnOnDeath {
fn despawn_on_death(trigger: Trigger<OnDeath>, mut despawn: ResMut<LateDespawn>) {
despawn.recursive(r!(trigger.get_entity()));
}

#[derive(Component, Reflect)]
#[reflect(Component)]
pub struct DeathSfx;

impl Configure for DeathSfx {
fn configure(app: &mut App) {
app.register_type::<Self>();
app.observe(play_death_sfx);
}
}

fn play_death_sfx(
trigger: Trigger<OnDeath>,
sfx_query: Query<(), With<DeathSfx>>,
audio: Res<Audio>,
assets: Res<PlayingAssets>,
) {
let entity = r!(trigger.get_entity());
if !sfx_query.contains(entity) {
return;
}

audio.play(assets.sfx_restart.clone()).with_volume(1.0);
}
6 changes: 3 additions & 3 deletions src/game/combat/hit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ impl Configure for HurtSfx {

fn play_hurt_sfx(
trigger: Trigger<OnHit>,
hurt_query: Query<(), With<HurtSfx>>,
sfx_query: Query<(), With<HurtSfx>>,
audio: Res<Audio>,
assets: Res<PlayingAssets>,
) {
if !hurt_query.contains(trigger.event().1) {
if !sfx_query.contains(trigger.event().1) {
return;
}

audio.play(assets.sfx_hurt.clone()).with_volume(0.6);
audio.play(assets.sfx_hurt.clone()).with_volume(1.0);
}

0 comments on commit 5c12458

Please sign in to comment.