2025-08-26 08:16:40 -04:00
|
|
|
//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),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-31 19:00:19 -04:00
|
|
|
#[derive(Component, Default)]
|
2025-08-26 08:26:59 -04:00
|
|
|
#[require(CollisionEventsEnabled)]
|
2025-08-26 08:16:40 -04:00
|
|
|
pub struct ContactDamage {
|
|
|
|
|
pub amount: f32,
|
2025-08-31 19:00:19 -04:00
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-26 08:16:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
2025-08-31 19:00:19 -04:00
|
|
|
skip_invulnerability: contact.skip_invulnerability,
|
2025-08-26 08:16:40 -04:00
|
|
|
},
|
|
|
|
|
event.1,
|
|
|
|
|
);
|
2025-08-31 19:00:19 -04:00
|
|
|
if contact.self_damage > 0. {
|
|
|
|
|
commands.trigger_targets(
|
|
|
|
|
Attacked {
|
|
|
|
|
attacker: None,
|
|
|
|
|
amount: contact.self_damage,
|
|
|
|
|
skip_invulnerability: true,
|
|
|
|
|
},
|
|
|
|
|
event.0,
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-08-26 08:16:40 -04:00
|
|
|
}
|
|
|
|
|
if let Ok(contact) = query.get(event.1) {
|
|
|
|
|
commands.trigger_targets(
|
|
|
|
|
Attacked {
|
|
|
|
|
attacker: Some(event.1),
|
|
|
|
|
amount: contact.amount,
|
2025-08-31 19:00:19 -04:00
|
|
|
skip_invulnerability: contact.skip_invulnerability,
|
2025-08-26 08:16:40 -04:00
|
|
|
},
|
|
|
|
|
event.0,
|
|
|
|
|
);
|
2025-08-31 19:00:19 -04:00
|
|
|
if contact.self_damage > 0. {
|
|
|
|
|
commands.trigger_targets(
|
|
|
|
|
Attacked {
|
|
|
|
|
attacker: None,
|
|
|
|
|
amount: contact.self_damage,
|
|
|
|
|
skip_invulnerability: true,
|
|
|
|
|
},
|
|
|
|
|
event.1,
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-08-26 08:16:40 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|