diff --git a/src/abilities/missile.rs b/src/abilities/missile.rs index c61b206..275a5dc 100644 --- a/src/abilities/missile.rs +++ b/src/abilities/missile.rs @@ -3,11 +3,12 @@ use crate::{ GameState, billboards::Billboard, heads_database::HeadsDatabase, - hitpoints::Hit, loading_assets::GameAssets, physics_layers::GameLayer, sounds::PlaySound, - utils::{global_observer, sprite_3d_animation::AnimationTimer, trail::Trail}, + utils::{ + explosions::Explosion, global_observer, sprite_3d_animation::AnimationTimer, trail::Trail, + }, }; use avian3d::prelude::*; use bevy::{pbr::NotShadowCaster, prelude::*}; @@ -22,13 +23,6 @@ struct MissileProjectile { damage: u32, } -#[derive(Event, Debug)] -struct Explosion { - position: Vec3, - radius: f32, - damage: u32, -} - #[derive(Resource)] struct ShotAssets { image: Handle, @@ -44,33 +38,6 @@ pub fn plugin(app: &mut App) { ); global_observer!(app, on_trigger_missile); - global_observer!(app, on_explosion); -} - -fn on_explosion( - explosion: Trigger, - mut commands: Commands, - spatial_query: SpatialQuery, -) { - let explosion = explosion.event(); - let intersections = { - spatial_query.shape_intersections( - &Collider::sphere(explosion.radius), - explosion.position, - Quat::default(), - &SpatialQueryFilter::default().with_mask(LayerMask( - GameLayer::Npc.to_bits() | GameLayer::Player.to_bits(), - )), - ) - }; - - for entity in intersections.iter() { - if let Ok(mut e) = commands.get_entity(*entity) { - e.trigger(Hit { - damage: explosion.damage, - }); - } - } } fn setup(mut commands: Commands, assets: Res, mut sprite_params: Sprite3dParams) { diff --git a/src/abilities/thrown.rs b/src/abilities/thrown.rs index 7f0fcca..50b1454 100644 --- a/src/abilities/thrown.rs +++ b/src/abilities/thrown.rs @@ -3,11 +3,13 @@ use crate::{ GameState, billboards::Billboard, heads_database::HeadsDatabase, - hitpoints::Hit, loading_assets::GameAssets, physics_layers::GameLayer, sounds::PlaySound, - utils::{auto_rotate::AutoRotation, global_observer, sprite_3d_animation::AnimationTimer}, + utils::{ + auto_rotate::AutoRotation, explosions::Explosion, global_observer, + sprite_3d_animation::AnimationTimer, + }, }; use avian3d::prelude::*; use bevy::{pbr::NotShadowCaster, prelude::*}; @@ -21,13 +23,6 @@ struct ThrownProjectile { damage: u32, } -#[derive(Event, Debug)] -struct Explosion { - position: Vec3, - radius: f32, - damage: u32, -} - #[derive(Resource)] struct ShotAssets { image: Handle, @@ -39,33 +34,6 @@ pub fn plugin(app: &mut App) { app.add_systems(Update, shot_collision.run_if(in_state(GameState::Playing))); global_observer!(app, on_trigger_thrown); - global_observer!(app, on_explosion); -} - -fn on_explosion( - explosion: Trigger, - mut commands: Commands, - spatial_query: SpatialQuery, -) { - let explosion = explosion.event(); - let intersections = { - spatial_query.shape_intersections( - &Collider::sphere(explosion.radius), - explosion.position, - Quat::default(), - &SpatialQueryFilter::default().with_mask(LayerMask( - GameLayer::Npc.to_bits() | GameLayer::Player.to_bits(), - )), - ) - }; - - for entity in intersections.iter() { - if let Ok(mut e) = commands.get_entity(*entity) { - e.trigger(Hit { - damage: explosion.damage, - }); - } - } } fn setup(mut commands: Commands, assets: Res, mut sprite_params: Sprite3dParams) { diff --git a/src/main.rs b/src/main.rs index c1879f3..92be64f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,7 @@ use loading_assets::AudioAssets; use std::io::{Read, Write}; use utils::{billboards, sprite_3d_animation, squish_animation, trail}; -use crate::utils::auto_rotate; +use crate::utils::{auto_rotate, explosions}; #[derive(Resource, Reflect, Debug)] #[reflect(Resource)] @@ -173,6 +173,7 @@ fn main() { app.add_plugins(auto_rotate::plugin); app.add_plugins(heal_effect::plugin); app.add_plugins(tb_entities::plugin); + app.add_plugins(explosions::plugin); app.init_state::(); diff --git a/src/utils/explosions.rs b/src/utils/explosions.rs new file mode 100644 index 0000000..a7f89b4 --- /dev/null +++ b/src/utils/explosions.rs @@ -0,0 +1,41 @@ +use avian3d::prelude::*; +use bevy::prelude::*; + +use crate::{global_observer, hitpoints::Hit, physics_layers::GameLayer}; + +#[derive(Event, Debug)] +pub struct Explosion { + pub position: Vec3, + pub radius: f32, + pub damage: u32, +} + +pub fn plugin(app: &mut App) { + global_observer!(app, on_explosion); +} + +fn on_explosion( + explosion: Trigger, + mut commands: Commands, + spatial_query: SpatialQuery, +) { + let explosion = explosion.event(); + let intersections = { + spatial_query.shape_intersections( + &Collider::sphere(explosion.radius), + explosion.position, + Quat::default(), + &SpatialQueryFilter::default().with_mask(LayerMask( + GameLayer::Npc.to_bits() | GameLayer::Player.to_bits(), + )), + ) + }; + + for entity in intersections.iter() { + if let Ok(mut e) = commands.get_entity(*entity) { + e.trigger(Hit { + damage: explosion.damage, + }); + } + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 0c1276d..f4c1e78 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,5 +1,6 @@ pub mod auto_rotate; pub mod billboards; +pub mod explosions; pub mod observers; pub mod sprite_3d_animation; pub mod squish_animation;