add sound effects

This commit is contained in:
Jim 2025-09-02 21:13:21 -04:00
parent 86f2345667
commit 587b902e79
Signed by: jim
GPG key ID: 3D7D94BA53088BF4
39 changed files with 234 additions and 40 deletions

View file

@ -9,6 +9,7 @@ use std::time::Duration;
use avian3d::prelude::*;
use bevy::prelude::*;
use physics::PhysicsLayers;
use rand::{Rng, thread_rng};
use resources::{Drops, Resources};
use state::GameSystemSet;
@ -96,6 +97,9 @@ pub struct Health {
pub current: f32,
}
#[derive(Component)]
pub struct DeathAudio(pub Handle<AudioSource>);
impl Health {
pub fn new(health: f32) -> Self {
Self {
@ -245,13 +249,26 @@ fn on_attack(
fn on_die(
trigger: Trigger<Died>,
names: Query<&Name>,
drops: Query<&Drops>,
dying_query: Query<(&Drops, &GlobalTransform, Option<&DeathAudio>)>,
mut resources: ResMut<Resources>,
mut commands: Commands,
) {
if let Ok(drop) = drops.get(trigger.target()) {
if let Ok((drop, gtf, opt_audio)) = dying_query.get(trigger.target()) {
resources.0 = resources.0 + drop.0;
if let Some(audio) = opt_audio {
commands.spawn((
AudioPlayer(audio.0.clone()),
PlaybackSettings {
mode: bevy::audio::PlaybackMode::Despawn,
speed: thread_rng().gen_range(0.8..1.2),
spatial: false,
..default()
},
Transform::from_translation(gtf.translation()),
));
}
}
if let Ok(mut ec) = commands.get_entity(trigger.target()) {
info!(
"{:?} killed {:?}!",

View file

@ -61,6 +61,7 @@ pub struct ProjectileLauncher {
pub damage: f32,
/// will set fields in the fire event to be defaults, responsiblity of the building to set them up
pub custom_targeting: bool,
pub shoot_sound: Handle<AudioSource>,
}
impl ProjectileLauncher {
@ -70,6 +71,7 @@ impl ProjectileLauncher {
fire_offset: Vec3,
lifetime: Duration,
spread: f32,
shoot_sound: Handle<AudioSource>,
) -> Self {
Self {
fire_timer: Timer::new(fire_interval, TimerMode::Once),
@ -78,6 +80,7 @@ impl ProjectileLauncher {
fire_offset,
lifetime,
custom_targeting: false,
shoot_sound: shoot_sound.clone(),
}
}
@ -142,6 +145,16 @@ fn fire_launcher(
},
[launcher_entity],
);
commands.spawn((
AudioPlayer(launcher.shoot_sound.clone()),
PlaybackSettings {
mode: bevy::audio::PlaybackMode::Despawn,
speed: thread_rng().gen_range(0.8..1.2),
spatial: false,
..default()
},
Transform::from_translation(gtf.translation()),
));
continue;
}
let Some((closest_enemy, enemy_pos)) = enemies
@ -180,6 +193,16 @@ fn fire_launcher(
},
[launcher_entity],
);
commands.spawn((
AudioPlayer(launcher.shoot_sound.clone()),
PlaybackSettings {
mode: bevy::audio::PlaybackMode::Despawn,
speed: thread_rng().gen_range(0.8..1.2),
spatial: false,
..default()
},
Transform::from_translation(gtf.translation()),
));
}
}