109 lines
2.5 KiB
Rust
109 lines
2.5 KiB
Rust
|
|
//bevy system signatures often violate these rules
|
||
|
|
#![allow(clippy::type_complexity)]
|
||
|
|
#![allow(clippy::too_many_arguments)]
|
||
|
|
|
||
|
|
use bevy::{audio::Sample, prelude::*};
|
||
|
|
|
||
|
|
pub struct CombatPlugin;
|
||
|
|
|
||
|
|
impl Plugin for CombatPlugin {
|
||
|
|
fn build(&self, app: &mut App) {
|
||
|
|
app.add_observer(on_attack).add_observer(on_die);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Event, Debug)]
|
||
|
|
pub struct Attacked {
|
||
|
|
pub attacker: Option<Entity>,
|
||
|
|
pub amount: f32,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Event, Debug)]
|
||
|
|
pub struct Died {
|
||
|
|
pub slayer: Option<Entity>,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Component, Clone, Debug)]
|
||
|
|
pub struct Health {
|
||
|
|
pub max: f32,
|
||
|
|
pub current: f32,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Health {
|
||
|
|
pub fn new(health: f32) -> Self {
|
||
|
|
Self {
|
||
|
|
max: health,
|
||
|
|
current: health,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Default for Health {
|
||
|
|
fn default() -> Self {
|
||
|
|
Self {
|
||
|
|
max: 1.0,
|
||
|
|
current: 1.0,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Component, Clone, Debug, Default)]
|
||
|
|
#[require(Health)]
|
||
|
|
pub struct Defense {
|
||
|
|
pub amount: f32,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Defense {
|
||
|
|
pub fn calc_damage(&self, incoming_damage: f32) -> f32 {
|
||
|
|
(incoming_damage - self.amount * 0.5).max(0.0)
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn new(amount: f32) -> Self {
|
||
|
|
Self { amount }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn on_attack(
|
||
|
|
trigger: Trigger<Attacked>,
|
||
|
|
mut commands: Commands,
|
||
|
|
mut query: Query<(&mut Health, Option<&Defense>)>,
|
||
|
|
names: Query<&Name>,
|
||
|
|
) {
|
||
|
|
let Ok((mut health, opt_defense)) = query.get_mut(trigger.target()) else {
|
||
|
|
warn!("tried to attack entity without health");
|
||
|
|
return;
|
||
|
|
};
|
||
|
|
let defense = opt_defense.cloned().unwrap_or_default();
|
||
|
|
let damage_taken = defense.calc_damage(trigger.event().amount);
|
||
|
|
health.current = (health.current - damage_taken).max(0.0);
|
||
|
|
info!(
|
||
|
|
"{:?} attacked {:?} for {} damage. New health = {}",
|
||
|
|
trigger
|
||
|
|
.event()
|
||
|
|
.attacker
|
||
|
|
.map(|e| names.get(e).ok())
|
||
|
|
.flatten(),
|
||
|
|
names.get(trigger.target()),
|
||
|
|
damage_taken,
|
||
|
|
health.current
|
||
|
|
);
|
||
|
|
if health.current <= 0.
|
||
|
|
&& let Ok(mut ec) = commands.get_entity(trigger.target())
|
||
|
|
{
|
||
|
|
ec.trigger(Died {
|
||
|
|
slayer: trigger.event().attacker,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn on_die(trigger: Trigger<Died>, names: Query<&Name>, mut commands: Commands) {
|
||
|
|
if let Ok(mut ec) = commands.get_entity(trigger.target()) {
|
||
|
|
info!(
|
||
|
|
"{:?} killed {:?}!",
|
||
|
|
trigger.event().slayer.map(|e| names.get(e).ok()).flatten(),
|
||
|
|
names.get(trigger.target())
|
||
|
|
);
|
||
|
|
ec.despawn();
|
||
|
|
}
|
||
|
|
}
|