This commit is contained in:
2025-04-18 23:53:36 +02:00
parent 52e477c7a5
commit 0437f82957
2 changed files with 35 additions and 46 deletions

View File

@@ -1,7 +1,8 @@
use super::{Projectile, TriggerGun};
use super::TriggerGun;
use crate::{
GameState, billboards::Billboard, global_observer, loading_assets::GameAssets,
physics_layers::GameLayer, sounds::PlaySound, utils::sprite_3d_animation::AnimationTimer,
GameState, billboards::Billboard, global_observer, heads_database::HeadsDatabase,
hitpoints::Hit, loading_assets::GameAssets, physics_layers::GameLayer, sounds::PlaySound,
tb_entities::EnemySpawn, utils::sprite_3d_animation::AnimationTimer,
};
use avian3d::prelude::{
Collider, CollisionLayers, CollisionStarted, LayerMask, PhysicsLayer, Sensor,
@@ -14,6 +15,7 @@ use std::f32::consts::PI;
#[derive(Component)]
struct GunProjectile {
time: f32,
owner_head: usize,
}
const MAX_SHOT_AGES: f32 = 1.;
@@ -28,7 +30,7 @@ pub fn plugin(app: &mut App) {
app.add_systems(OnEnter(GameState::Playing), setup);
app.add_systems(
Update,
(update, shot_collision, timeout).run_if(in_state(GameState::Playing)),
(update, shot_collision, timeout, enemy_hit).run_if(in_state(GameState::Playing)),
);
global_observer!(app, on_trigger_gun);
@@ -44,6 +46,34 @@ fn setup(mut commands: Commands, assets: Res<GameAssets>, mut sprite_params: Spr
});
}
fn enemy_hit(
mut commands: Commands,
mut collision_event_reader: EventReader<CollisionStarted>,
query_shot: Query<&GunProjectile>,
query_npc: Query<&EnemySpawn>,
heads_db: Res<HeadsDatabase>,
) {
for CollisionStarted(e1, e2) in collision_event_reader.read() {
if !query_shot.contains(*e1) && !query_shot.contains(*e2) {
continue;
}
if !query_npc.contains(*e1) && !query_npc.contains(*e2) {
continue;
}
let (enemy_entity, projectile) = if query_npc.contains(*e1) {
(*e1, query_shot.get(*e2))
} else {
(*e2, query_shot.get(*e1))
};
if let Ok(head) = projectile.map(|p| p.owner_head) {
let damage = heads_db.head_stats(head).damage;
commands.entity(enemy_entity).trigger(Hit { damage });
}
}
}
fn on_trigger_gun(
trigger: Trigger<TriggerGun>,
mut commands: Commands,
@@ -75,8 +105,6 @@ fn on_trigger_gun(
Name::new("projectile-gun"),
GunProjectile {
time: time.elapsed_secs(),
},
Projectile {
owner_head: state.head,
},
Collider::capsule_endpoints(0.5, Vec3::new(0., 0., 0.), Vec3::new(0., 0., -3.)),
@@ -112,7 +140,7 @@ fn update(mut query: Query<&mut Transform, With<GunProjectile>>) {
fn timeout(mut commands: Commands, query: Query<(Entity, &GunProjectile)>, time: Res<Time>) {
let current_time = time.elapsed_secs();
for (e, GunProjectile { time }) in query.iter() {
for (e, GunProjectile { time, .. }) in query.iter() {
if current_time > time + MAX_SHOT_AGES {
commands.entity(e).despawn_recursive();
}