Skip to content

Commit

Permalink
Support on-hurt sfx
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Jul 28, 2024
1 parent cc6afd9 commit a280360
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/game/actor/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::game::actor::movement::input::movement_action;
use crate::game::actor::ActorConfig;
use crate::game::combat::damage::HitboxDamage;
use crate::game::combat::hit::Hitbox;
use crate::game::combat::hit::HurtSfx;
use crate::game::combat::knockback::HitboxKnockback;
use crate::game::GameLayer;
use crate::game::GameRoot;
Expand Down Expand Up @@ -60,6 +61,7 @@ pub fn player(key: impl Into<String>) -> impl EntityCommand {
Hitbox,
HitboxDamage(15.0),
HitboxKnockback(5.0),
HurtSfx,
))
.set_parent(parent)
.with_children(|children| {
Expand Down
28 changes: 27 additions & 1 deletion src/game/combat/hit.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use avian2d::prelude::*;
use bevy::prelude::*;
use bevy_kira_audio::prelude::*;

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

pub(super) fn plugin(app: &mut App) {
app.configure::<(Hitbox, Hurtbox, OnHit)>();
app.configure::<(Hitbox, Hurtbox, OnHit, HurtSfx)>();
}

#[derive(Component, Reflect)]
Expand Down Expand Up @@ -52,3 +54,27 @@ fn trigger_hit(
}
}
}

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

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

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

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

0 comments on commit a280360

Please sign in to comment.