coreheart/crates/combat_damagers/src/lib.rs
2025-08-31 19:00:19 -04:00

93 lines
2.5 KiB
Rust
Executable file

//bevy system signatures often violate these rules
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
use avian3d::prelude::*;
use bevy::prelude::*;
use combat::Attacked;
use state::GameSystemSet;
pub struct CombatDamagersPlugin;
impl Plugin for CombatDamagersPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
FixedUpdate,
deal_contact_damage.in_set(GameSystemSet::DealDamage),
);
}
}
#[derive(Component, Default)]
#[require(CollisionEventsEnabled)]
pub struct ContactDamage {
pub amount: f32,
pub skip_invulnerability: bool,
pub self_damage: f32,
}
impl ContactDamage {
pub fn new(amount: f32) -> Self {
Self {
amount,
..default()
}
}
pub fn projectile(amount: f32, self_damage: f32) -> Self {
Self {
amount,
skip_invulnerability: true,
self_damage,
}
}
}
fn deal_contact_damage(
mut reader: EventReader<CollisionStarted>,
query: Query<&ContactDamage>,
mut commands: Commands,
) {
for event in reader.read() {
if let Ok(contact) = query.get(event.0) {
commands.trigger_targets(
Attacked {
attacker: Some(event.0),
amount: contact.amount,
skip_invulnerability: contact.skip_invulnerability,
},
event.1,
);
if contact.self_damage > 0. {
commands.trigger_targets(
Attacked {
attacker: None,
amount: contact.self_damage,
skip_invulnerability: true,
},
event.0,
);
}
}
if let Ok(contact) = query.get(event.1) {
commands.trigger_targets(
Attacked {
attacker: Some(event.1),
amount: contact.amount,
skip_invulnerability: contact.skip_invulnerability,
},
event.0,
);
if contact.self_damage > 0. {
commands.trigger_targets(
Attacked {
attacker: None,
amount: contact.self_damage,
skip_invulnerability: true,
},
event.1,
);
}
}
}
}