diff --git a/src/minions.rs b/src/minions.rs index 2525bf8..31f19a8 100644 --- a/src/minions.rs +++ b/src/minions.rs @@ -12,6 +12,8 @@ const DESTROY_MINIONS_AFTER_SECS: f32 = 120.; const DECAY_VALUE_PER_SEC: f32 = 10.; const REWARDS_GOLD: f32 = 1.; +const EXPLOSION_AUDIO_ID: &str = "sounds/explosion.ogg"; + pub struct MinionsPlugin; // TODO: move this into common @@ -25,21 +27,30 @@ struct Minion { had_exploded: bool, } +#[derive(Resource)] +struct AudioExplosion(Handle); + impl Plugin for MinionsPlugin { fn build(&self, app: &mut App) { - app.add_systems( - Update, - ( - update_move_minions, - check_collisions_minions, - decay_life, - explosion_damage, - ), - ) - .add_systems(PostUpdate, (destroy_minions, destroy_after_timer)); + app.add_systems(Startup, setup_audio) + .add_systems( + Update, + ( + update_move_minions, + check_collisions_minions, + decay_life, + explosion_damage, + ), + ) + .add_systems(PostUpdate, (destroy_minions, destroy_after_timer)); } } +fn setup_audio(mut commands: Commands, server: Res) { + let handle = server.load(EXPLOSION_AUDIO_ID); + commands.insert_resource(AudioExplosion(handle)); +} + #[derive(Bundle)] pub struct MinionBundle { minion: Minion, @@ -118,7 +129,7 @@ impl ExplosionBundle { pub fn new( meshes: &mut ResMut>, materials: &mut ResMut>, - asset_server: &Res, + audio_asset: &Handle, mut translation: Vec3, team: Team, ) -> Self { @@ -141,7 +152,7 @@ impl ExplosionBundle { timer: Timer::from_seconds(0.2, bevy::time::TimerMode::Once), }, audio: AudioBundle { - source: asset_server.load("sounds/explosion.ogg"), + source: audio_asset.clone(), settings: PlaybackSettings::ONCE.with_spatial(true), }, } @@ -245,7 +256,7 @@ fn check_collisions_minions( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, - asset_server: Res, + audio_explosion: Res, mut collision_events: EventReader, // queries mut query_minions: Query<(&Transform, &Team, &mut Minion), With>, @@ -275,7 +286,7 @@ fn check_collisions_minions( commands.spawn(ExplosionBundle::new( &mut meshes, &mut materials, - &asset_server, + &audio_explosion.0, transform_a.translation, team_a.clone(), )); @@ -313,7 +324,7 @@ fn check_collisions_minions( commands.spawn(ExplosionBundle::new( &mut meshes, &mut materials, - &asset_server, + &audio_explosion.0, minion_transform.translation, minion_team.clone(), )); diff --git a/web/index.html b/web/index.html index 9c6ec15..8f4124b 100644 --- a/web/index.html +++ b/web/index.html @@ -1,50 +1,70 @@ - + + An other bevy 2D game

An other bevy 2D game

+ -