Throw projectile (#19)

This commit is contained in:
extrawurst
2025-03-28 00:21:23 +08:00
committed by GitHub
parent f3358b9511
commit 1fa1b110db
12 changed files with 317 additions and 49 deletions

49
src/abilities/mod.rs Normal file
View File

@@ -0,0 +1,49 @@
mod gun;
mod thrown;
use crate::{GameState, npc::Hit, tb_entities::EnemySpawn};
use avian3d::prelude::*;
use bevy::prelude::*;
#[derive(Component)]
pub struct Projectile {
pub damage: u32,
}
#[derive(Event, Reflect)]
pub enum TriggerState {
Active,
Inactive,
}
pub fn plugin(app: &mut App) {
app.add_plugins(gun::plugin);
app.add_plugins(thrown::plugin);
app.add_systems(Update, enemy_hit.run_if(in_state(GameState::Playing)));
}
fn enemy_hit(
mut commands: Commands,
mut collision_event_reader: EventReader<CollisionStarted>,
query_shot: Query<&Projectile>,
query_npc: Query<&EnemySpawn>,
) {
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))
};
let damage = projectile.map(|p| p.damage).unwrap_or_default();
commands.entity(enemy_entity).trigger(Hit { damage });
}
}