unify explosion handling
This commit is contained in:
@@ -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<Image>,
|
||||
@@ -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<Explosion>,
|
||||
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<GameAssets>, mut sprite_params: Sprite3dParams) {
|
||||
|
||||
@@ -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<Image>,
|
||||
@@ -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<Explosion>,
|
||||
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<GameAssets>, mut sprite_params: Sprite3dParams) {
|
||||
|
||||
Reference in New Issue
Block a user