From 9d397451c468fbeab4283f7c6f72420bae03f46f Mon Sep 17 00:00:00 2001 From: Jim Date: Tue, 26 Aug 2025 08:16:40 -0400 Subject: [PATCH] add contact damage and iframes --- Cargo.lock | 12 ++++ Cargo.toml | 1 + crates/combat/Cargo.toml | 2 + crates/combat/src/lib.rs | 94 +++++++++++++++++++++++++++---- crates/combat_damagers/Cargo.toml | 14 +++++ crates/combat_damagers/src/lib.rs | 53 +++++++++++++++++ crates/level/src/lib.rs | 1 + crates/state/src/lib.rs | 4 ++ src/main.rs | 1 + 9 files changed, 171 insertions(+), 11 deletions(-) create mode 100644 crates/combat_damagers/Cargo.toml create mode 100644 crates/combat_damagers/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 9ca0c53..c1b7445 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1801,6 +1801,17 @@ name = "combat" version = "0.1.0" dependencies = [ "bevy", + "state", +] + +[[package]] +name = "combat_damagers" +version = "0.1.0" +dependencies = [ + "avian3d", + "bevy", + "combat", + "state", ] [[package]] @@ -1950,6 +1961,7 @@ dependencies = [ "building_impaler", "camera", "combat", + "combat_damagers", "input", "level", "physics", diff --git a/Cargo.toml b/Cargo.toml index 9d86378..0d4f2fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,4 +46,5 @@ combat = { path = "crates/combat" } building_core = { path = "crates/building_core" } building = { path = "crates/building" } building_impaler = { path = "crates/building_impaler" } +combat_damagers = { path = "crates/combat_damagers" } #new internal crates go here diff --git a/crates/combat/Cargo.toml b/crates/combat/Cargo.toml index ced910e..4fd9151 100644 --- a/crates/combat/Cargo.toml +++ b/crates/combat/Cargo.toml @@ -6,5 +6,7 @@ edition = "2024" [dependencies] bevy = { workspace = true } +state = { path = "../state" } + [lints] workspace = true diff --git a/crates/combat/src/lib.rs b/crates/combat/src/lib.rs index 2fca551..2157e22 100644 --- a/crates/combat/src/lib.rs +++ b/crates/combat/src/lib.rs @@ -2,13 +2,21 @@ #![allow(clippy::type_complexity)] #![allow(clippy::too_many_arguments)] +use std::time::Duration; + use bevy::prelude::*; +use state::GameSystemSet; pub struct CombatPlugin; impl Plugin for CombatPlugin { fn build(&self, app: &mut App) { - app.add_observer(on_attack).add_observer(on_die); + app.add_observer(on_attack) + .add_observer(on_die) + .add_systems( + FixedUpdate, + tick_invulnerability.in_set(GameSystemSet::StatusEffects), + ); } } @@ -18,10 +26,11 @@ pub enum Team { Immune, } -#[derive(Event, Debug)] +#[derive(Event, Debug, Default)] pub struct Attacked { pub attacker: Option, pub amount: f32, + pub skip_invulnerability: bool, } #[derive(Event, Debug)] @@ -30,6 +39,7 @@ pub struct Died { } #[derive(Component, Clone, Debug)] +#[require(InvulnerabilityTime)] pub struct Health { pub max: f32, pub current: f32, @@ -53,6 +63,32 @@ impl Default for Health { } } +#[derive(Component, Clone, Debug)] +pub struct InvulnerabilityTime { + pub duration: Duration, +} + +impl Default for InvulnerabilityTime { + fn default() -> Self { + Self { + duration: Duration::from_secs_f32(0.25), + } + } +} + +#[derive(Component, Clone, Debug)] +pub struct Invulnerable { + pub time_remaining: Option, +} + +impl From<&InvulnerabilityTime> for Invulnerable { + fn from(value: &InvulnerabilityTime) -> Self { + Self { + time_remaining: Some(Timer::new(value.duration, TimerMode::Once)), + } + } +} + #[derive(Component, Clone, Debug, Default)] #[require(Health)] pub struct Defense { @@ -77,11 +113,20 @@ fn on_attack( trigger: Trigger, mut commands: Commands, attacker_query: Query<&Team>, - mut attacked_query: Query<(&mut Health, Option<&Defense>, Option<&Team>)>, + mut attacked_query: Query< + ( + &mut Health, + &InvulnerabilityTime, + Option<&Defense>, + Option<&Team>, + ), + Without, + >, names: Query<&Name>, ) { - let Ok((mut health, opt_defense, opt_team)) = attacked_query.get_mut(trigger.target()) else { - warn!("tried to attack entity without health"); + let Ok((mut health, invulnerability, opt_defense, opt_team)) = + attacked_query.get_mut(trigger.target()) + else { return; }; if let Some(Ok(attacker_team)) = trigger.event().attacker.map(|a| attacker_query.get(a)) @@ -105,12 +150,14 @@ fn on_attack( damage_taken, health.current ); - if health.current <= 0. - && let Ok(mut ec) = commands.get_entity(trigger.target()) - { - ec.trigger(Died { - slayer: trigger.event().attacker, - }); + if let Ok(mut ec) = commands.get_entity(trigger.target()) { + if health.current <= 0. { + ec.trigger(Died { + slayer: trigger.event().attacker, + }); + } else if !trigger.event().skip_invulnerability { + ec.insert(Invulnerable::from(invulnerability)); + } } } @@ -124,3 +171,28 @@ fn on_die(trigger: Trigger, names: Query<&Name>, mut commands: Commands) { ec.despawn(); } } + +fn tick_invulnerability( + mut commands: Commands, + mut query: Query<(Entity, &mut Invulnerable)>, + time: Res