add contact damage and iframes
This commit is contained in:
parent
6659fe8e5a
commit
9d397451c4
9 changed files with 171 additions and 11 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
|
@ -1801,6 +1801,17 @@ name = "combat"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
|
"state",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "combat_damagers"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"avian3d",
|
||||||
|
"bevy",
|
||||||
|
"combat",
|
||||||
|
"state",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -1950,6 +1961,7 @@ dependencies = [
|
||||||
"building_impaler",
|
"building_impaler",
|
||||||
"camera",
|
"camera",
|
||||||
"combat",
|
"combat",
|
||||||
|
"combat_damagers",
|
||||||
"input",
|
"input",
|
||||||
"level",
|
"level",
|
||||||
"physics",
|
"physics",
|
||||||
|
|
|
||||||
|
|
@ -46,4 +46,5 @@ combat = { path = "crates/combat" }
|
||||||
building_core = { path = "crates/building_core" }
|
building_core = { path = "crates/building_core" }
|
||||||
building = { path = "crates/building" }
|
building = { path = "crates/building" }
|
||||||
building_impaler = { path = "crates/building_impaler" }
|
building_impaler = { path = "crates/building_impaler" }
|
||||||
|
combat_damagers = { path = "crates/combat_damagers" }
|
||||||
#new internal crates go here
|
#new internal crates go here
|
||||||
|
|
|
||||||
|
|
@ -6,5 +6,7 @@ edition = "2024"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = { workspace = true }
|
bevy = { workspace = true }
|
||||||
|
|
||||||
|
state = { path = "../state" }
|
||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,21 @@
|
||||||
#![allow(clippy::type_complexity)]
|
#![allow(clippy::type_complexity)]
|
||||||
#![allow(clippy::too_many_arguments)]
|
#![allow(clippy::too_many_arguments)]
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
use state::GameSystemSet;
|
||||||
|
|
||||||
pub struct CombatPlugin;
|
pub struct CombatPlugin;
|
||||||
|
|
||||||
impl Plugin for CombatPlugin {
|
impl Plugin for CombatPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
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,
|
Immune,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Event, Debug)]
|
#[derive(Event, Debug, Default)]
|
||||||
pub struct Attacked {
|
pub struct Attacked {
|
||||||
pub attacker: Option<Entity>,
|
pub attacker: Option<Entity>,
|
||||||
pub amount: f32,
|
pub amount: f32,
|
||||||
|
pub skip_invulnerability: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Event, Debug)]
|
#[derive(Event, Debug)]
|
||||||
|
|
@ -30,6 +39,7 @@ pub struct Died {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Clone, Debug)]
|
#[derive(Component, Clone, Debug)]
|
||||||
|
#[require(InvulnerabilityTime)]
|
||||||
pub struct Health {
|
pub struct Health {
|
||||||
pub max: f32,
|
pub max: f32,
|
||||||
pub current: 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)]
|
#[derive(Component, Clone, Debug, Default)]
|
||||||
#[require(Health)]
|
#[require(Health)]
|
||||||
pub struct Defense {
|
pub struct Defense {
|
||||||
|
|
@ -77,11 +113,20 @@ fn on_attack(
|
||||||
trigger: Trigger<Attacked>,
|
trigger: Trigger<Attacked>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
attacker_query: Query<&Team>,
|
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>,
|
names: Query<&Name>,
|
||||||
) {
|
) {
|
||||||
let Ok((mut health, opt_defense, opt_team)) = attacked_query.get_mut(trigger.target()) else {
|
let Ok((mut health, invulnerability, opt_defense, opt_team)) =
|
||||||
warn!("tried to attack entity without health");
|
attacked_query.get_mut(trigger.target())
|
||||||
|
else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
if let Some(Ok(attacker_team)) = trigger.event().attacker.map(|a| attacker_query.get(a))
|
if let Some(Ok(attacker_team)) = trigger.event().attacker.map(|a| attacker_query.get(a))
|
||||||
|
|
@ -105,12 +150,14 @@ fn on_attack(
|
||||||
damage_taken,
|
damage_taken,
|
||||||
health.current
|
health.current
|
||||||
);
|
);
|
||||||
if health.current <= 0.
|
if let Ok(mut ec) = commands.get_entity(trigger.target()) {
|
||||||
&& let Ok(mut ec) = commands.get_entity(trigger.target())
|
if health.current <= 0. {
|
||||||
{
|
ec.trigger(Died {
|
||||||
ec.trigger(Died {
|
slayer: trigger.event().attacker,
|
||||||
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();
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
14
crates/combat_damagers/Cargo.toml
Normal file
14
crates/combat_damagers/Cargo.toml
Normal 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
|
||||||
53
crates/combat_damagers/src/lib.rs
Normal file
53
crates/combat_damagers/src/lib.rs
Normal 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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -72,6 +72,7 @@ fn setup(
|
||||||
commands.entity(trigger.target()).trigger(Attacked {
|
commands.entity(trigger.target()).trigger(Attacked {
|
||||||
attacker: None,
|
attacker: None,
|
||||||
amount: 8.,
|
amount: 8.,
|
||||||
|
..default()
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,8 @@ impl Plugin for StatePlugin {
|
||||||
GameSystemSet::Plan.run_if(in_state(Phase::Plan)),
|
GameSystemSet::Plan.run_if(in_state(Phase::Plan)),
|
||||||
GameSystemSet::Execute.run_if(in_state(Phase::Execute)),
|
GameSystemSet::Execute.run_if(in_state(Phase::Execute)),
|
||||||
),
|
),
|
||||||
|
GameSystemSet::StatusEffects,
|
||||||
|
GameSystemSet::DealDamage,
|
||||||
GameSystemSet::PostTick,
|
GameSystemSet::PostTick,
|
||||||
(
|
(
|
||||||
GameSystemSet::PostPlan.run_if(in_state(Phase::Plan)),
|
GameSystemSet::PostPlan.run_if(in_state(Phase::Plan)),
|
||||||
|
|
@ -96,6 +98,8 @@ pub enum GameSystemSet {
|
||||||
Tick,
|
Tick,
|
||||||
Execute,
|
Execute,
|
||||||
Plan,
|
Plan,
|
||||||
|
StatusEffects,
|
||||||
|
DealDamage,
|
||||||
PostTick,
|
PostTick,
|
||||||
PostExecute,
|
PostExecute,
|
||||||
PostPlan,
|
PostPlan,
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ fn main() {
|
||||||
building_core::BuildingCorePlugin,
|
building_core::BuildingCorePlugin,
|
||||||
building::BuildingPlugin,
|
building::BuildingPlugin,
|
||||||
building_impaler::BuildingImpalerPlugin,
|
building_impaler::BuildingImpalerPlugin,
|
||||||
|
combat_damagers::CombatDamagersPlugin,
|
||||||
// new internal crates go here
|
// new internal crates go here
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue