Throw projectile (#19)
This commit is contained in:
49
src/abilities/mod.rs
Normal file
49
src/abilities/mod.rs
Normal 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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user