diff --git a/Cargo.lock b/Cargo.lock index 0a43d59..f81f9be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1766,6 +1766,13 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "combat" +version = "0.1.0" +dependencies = [ + "bevy", +] + [[package]] name = "combine" version = "4.6.7" @@ -1909,6 +1916,7 @@ dependencies = [ "bevy", "bevy-inspector-egui", "camera", + "combat", "input", "level", "physics", @@ -2943,6 +2951,7 @@ dependencies = [ "avian3d", "bevy", "camera", + "combat", "state", ] diff --git a/Cargo.toml b/Cargo.toml index 32aee5f..6587141 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,4 +42,5 @@ setup = { path = "crates/setup" } ui_pause = { path = "crates/ui_pause" } level = { path = "crates/level" } camera = { path = "crates/camera" } +combat = { path = "crates/combat" } #new internal crates go here diff --git a/assets/settings/input_map.ron b/assets/settings/input_map.ron index 4c7789e..0941d5a 100644 --- a/assets/settings/input_map.ron +++ b/assets/settings/input_map.ron @@ -1,7 +1,7 @@ ( map: { - KeyCode(F1): [ - ToggleDebugUI, + MouseButton(Left): [ + CameraPanEnable, ], MouseScroll: [ CameraZoom, @@ -10,17 +10,17 @@ CameraYaw, CameraPanX, ], + KeyCode(Escape): [ + TogglePause, + ], MouseMovementY: [ CameraPanY, ], MouseButton(Middle): [ CameraOrbitEnable, ], - MouseButton(Left): [ - CameraPanEnable, - ], - KeyCode(Escape): [ - TogglePause, + KeyCode(F1): [ + ToggleDebugUI, ], }, ) \ No newline at end of file diff --git a/crates/combat/Cargo.toml b/crates/combat/Cargo.toml new file mode 100644 index 0000000..ced910e --- /dev/null +++ b/crates/combat/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "combat" +version = "0.1.0" +edition = "2024" + +[dependencies] +bevy = { workspace = true } + +[lints] +workspace = true diff --git a/crates/combat/src/lib.rs b/crates/combat/src/lib.rs new file mode 100644 index 0000000..7ab26e2 --- /dev/null +++ b/crates/combat/src/lib.rs @@ -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, + pub amount: f32, +} + +#[derive(Event, Debug)] +pub struct Died { + pub slayer: Option, +} + +#[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, + 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, 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(); + } +} diff --git a/crates/level/Cargo.toml b/crates/level/Cargo.toml index d6007fc..d5d46df 100644 --- a/crates/level/Cargo.toml +++ b/crates/level/Cargo.toml @@ -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 diff --git a/crates/level/src/lib.rs b/crates/level/src/lib.rs index 62312b0..165be91 100644 --- a/crates/level/src/lib.rs +++ b/crates/level/src/lib.rs @@ -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>, mut query: Query<&mut LinearVelocity>| { + |trigger: Trigger>, + 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., + }); }, ); } diff --git a/src/main.rs b/src/main.rs index f2bc431..4f38a30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ fn main() { ui_pause::UiPausePlugin, level::LevelPlugin, camera::CameraPlugin, + combat::CombatPlugin, // new internal crates go here ));