Skip to content

Commit 5c12458

Browse files
committed
Add player death SFX
1 parent b7f0ee5 commit 5c12458

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/game/actor/player.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::game::actor::faction::Faction;
1414
use crate::game::actor::movement::input::movement_action;
1515
use crate::game::actor::ActorConfig;
1616
use crate::game::combat::damage::HitboxDamage;
17+
use crate::game::combat::death::DeathSfx;
1718
use crate::game::combat::hit::Hitbox;
1819
use crate::game::combat::hit::HurtSfx;
1920
use crate::game::combat::knockback::HitboxKnockback;
@@ -62,6 +63,7 @@ pub fn player(key: impl Into<String>) -> impl EntityCommand {
6263
HitboxDamage(15.0),
6364
HitboxKnockback(5.0),
6465
HurtSfx,
66+
DeathSfx,
6567
))
6668
.set_parent(parent)
6769
.with_children(|children| {

src/game/combat/death.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use bevy::prelude::*;
2+
use bevy_kira_audio::prelude::*;
23

4+
use crate::screen::playing::PlayingAssets;
35
use crate::util::prelude::*;
46

57
pub(super) fn plugin(app: &mut App) {
6-
app.configure::<(IsDead, DespawnOnDeath)>();
8+
app.configure::<(IsDead, DespawnOnDeath, DeathSfx)>();
79
}
810

911
/// An observable event on an actor's death.
@@ -41,3 +43,28 @@ impl Configure for DespawnOnDeath {
4143
fn despawn_on_death(trigger: Trigger<OnDeath>, mut despawn: ResMut<LateDespawn>) {
4244
despawn.recursive(r!(trigger.get_entity()));
4345
}
46+
47+
#[derive(Component, Reflect)]
48+
#[reflect(Component)]
49+
pub struct DeathSfx;
50+
51+
impl Configure for DeathSfx {
52+
fn configure(app: &mut App) {
53+
app.register_type::<Self>();
54+
app.observe(play_death_sfx);
55+
}
56+
}
57+
58+
fn play_death_sfx(
59+
trigger: Trigger<OnDeath>,
60+
sfx_query: Query<(), With<DeathSfx>>,
61+
audio: Res<Audio>,
62+
assets: Res<PlayingAssets>,
63+
) {
64+
let entity = r!(trigger.get_entity());
65+
if !sfx_query.contains(entity) {
66+
return;
67+
}
68+
69+
audio.play(assets.sfx_restart.clone()).with_volume(1.0);
70+
}

src/game/combat/hit.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ impl Configure for HurtSfx {
6868

6969
fn play_hurt_sfx(
7070
trigger: Trigger<OnHit>,
71-
hurt_query: Query<(), With<HurtSfx>>,
71+
sfx_query: Query<(), With<HurtSfx>>,
7272
audio: Res<Audio>,
7373
assets: Res<PlayingAssets>,
7474
) {
75-
if !hurt_query.contains(trigger.event().1) {
75+
if !sfx_query.contains(trigger.event().1) {
7676
return;
7777
}
7878

79-
audio.play(assets.sfx_hurt.clone()).with_volume(0.6);
79+
audio.play(assets.sfx_hurt.clone()).with_volume(1.0);
8080
}

0 commit comments

Comments
 (0)