combat system

This commit is contained in:
Jim 2025-08-24 19:45:33 -04:00
parent 928faf9570
commit f5d022d1d0
Signed by: jim
GPG key ID: 3236D2F059A7C0AC
8 changed files with 150 additions and 10 deletions

10
crates/combat/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "combat"
version = "0.1.0"
edition = "2024"
[dependencies]
bevy = { workspace = true }
[lints]
workspace = true

108
crates/combat/src/lib.rs Normal file
View file

@ -0,0 +1,108 @@
//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();
}
}

View file

@ -5,9 +5,11 @@ edition = "2024"
[dependencies]
bevy = { workspace = true }
avian3d = { workspace = true }
state = { path = "../state" }
camera = { path = "../camera" }
avian3d = { workspace = true }
combat = { path = "../combat" }
[lints]
workspace = true

View file

@ -10,6 +10,7 @@ use bevy::{
render::camera::Exposure,
};
use camera::ControlledCamera;
use combat::{Attacked, Defense, Health};
use state::AppState;
pub struct LevelPlugin;
@ -53,12 +54,20 @@ fn setup(
..default()
})),
Transform::from_xyz(0.0, y as f32 * 1.1, 0.0),
Health::new(5.0),
Defense::new(y as f32 - 5.0),
))
.observe(
|trigger: Trigger<Pointer<Click>>, mut query: Query<&mut LinearVelocity>| {
|trigger: Trigger<Pointer<Click>>,
mut query: Query<&mut LinearVelocity>,
mut commands: Commands| {
if let Ok(mut v) = query.get_mut(trigger.target()) {
v.y += 3.0;
v.y += 5.0;
}
commands.entity(trigger.target()).trigger(Attacked {
attacker: None,
amount: 8.,
});
},
);
}