add contact damage and iframes
This commit is contained in:
parent
6659fe8e5a
commit
9d397451c4
9 changed files with 171 additions and 11 deletions
|
|
@ -6,5 +6,7 @@ edition = "2024"
|
|||
[dependencies]
|
||||
bevy = { workspace = true }
|
||||
|
||||
state = { path = "../state" }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
|||
|
|
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue