add contact damage and iframes

This commit is contained in:
Jim 2025-08-26 08:16:40 -04:00
parent 6659fe8e5a
commit 9d397451c4
Signed by: jim
GPG key ID: 3236D2F059A7C0AC
9 changed files with 171 additions and 11 deletions

View file

@ -6,5 +6,7 @@ edition = "2024"
[dependencies]
bevy = { workspace = true }
state = { path = "../state" }
[lints]
workspace = true

View file

@ -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<Entity>,
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<Timer>,
}
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<Attacked>,
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<Invulnerable>,
>,
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<Died>, names: Query<&Name>, mut commands: Commands) {
ec.despawn();
}
}
fn tick_invulnerability(
mut commands: Commands,
mut query: Query<(Entity, &mut Invulnerable)>,
time: Res<Time>,
) {
for (entity, mut invulnerable) in query.iter_mut() {
if let Some(timer) = &mut invulnerable.time_remaining {
timer.tick(time.delta());
if timer.finished() {
match commands.get_entity(entity) {
Ok(mut ec) => {
ec.remove::<Invulnerable>();
}
Err(e) => {
error!(
"could not remove invulnerability from {:?} - {:?}",
entity, e
);
}
}
}
}
}
}

View file

@ -0,0 +1,14 @@
[package]
name = "combat_damagers"
version = "0.1.0"
edition = "2024"
[dependencies]
bevy = { workspace = true }
avian3d = { workspace = true }
combat = { path = "../combat" }
state = { path = "../state" }
[lints]
workspace = true

View file

@ -0,0 +1,53 @@
//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),
);
}
}
#[derive(Component)]
pub struct ContactDamage {
pub amount: f32,
}
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,
..default()
},
event.1,
);
}
if let Ok(contact) = query.get(event.1) {
commands.trigger_targets(
Attacked {
attacker: Some(event.1),
amount: contact.amount,
..default()
},
event.0,
);
}
}
}

View file

@ -72,6 +72,7 @@ fn setup(
commands.entity(trigger.target()).trigger(Attacked {
attacker: None,
amount: 8.,
..default()
});
},
);

View file

@ -32,6 +32,8 @@ impl Plugin for StatePlugin {
GameSystemSet::Plan.run_if(in_state(Phase::Plan)),
GameSystemSet::Execute.run_if(in_state(Phase::Execute)),
),
GameSystemSet::StatusEffects,
GameSystemSet::DealDamage,
GameSystemSet::PostTick,
(
GameSystemSet::PostPlan.run_if(in_state(Phase::Plan)),
@ -96,6 +98,8 @@ pub enum GameSystemSet {
Tick,
Execute,
Plan,
StatusEffects,
DealDamage,
PostTick,
PostExecute,
PostPlan,