303 lines
7.4 KiB
Rust
Executable file
303 lines
7.4 KiB
Rust
Executable file
//bevy system signatures often violate these rules
|
|
#![allow(clippy::type_complexity)]
|
|
#![allow(clippy::too_many_arguments)]
|
|
|
|
pub mod projectiles;
|
|
|
|
use std::time::Duration;
|
|
|
|
use avian3d::prelude::*;
|
|
use bevy::prelude::*;
|
|
use physics::PhysicsLayers;
|
|
use resources::{Drops, Resources};
|
|
use state::GameSystemSet;
|
|
|
|
pub struct CombatPlugin;
|
|
|
|
impl Plugin for CombatPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.add_plugins(projectiles::ProjectilePlugin)
|
|
.add_observer(on_attack)
|
|
.add_observer(on_die)
|
|
.add_systems(
|
|
FixedUpdate,
|
|
tick_invulnerability.in_set(GameSystemSet::StatusEffects),
|
|
)
|
|
.register_type::<Team>()
|
|
.register_type::<Health>()
|
|
.insert_resource(Difficulty::Chill);
|
|
}
|
|
}
|
|
|
|
#[derive(Resource)]
|
|
pub enum Difficulty {
|
|
Chill,
|
|
Casual,
|
|
Standard,
|
|
Intense,
|
|
}
|
|
|
|
impl Difficulty {
|
|
pub fn core_damage_taken_mult(&self) -> f32 {
|
|
match self {
|
|
Difficulty::Chill => 0.65,
|
|
Difficulty::Casual => 0.8,
|
|
Difficulty::Standard => 1.0,
|
|
Difficulty::Intense => 1.2,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Default, Component, Clone, Copy, Debug, PartialEq, Eq, Reflect)]
|
|
pub enum Team {
|
|
#[default]
|
|
Core = 1,
|
|
Inhabitant = 2,
|
|
}
|
|
|
|
impl Team {
|
|
pub fn collision_layers(&self) -> CollisionLayers {
|
|
match self {
|
|
Team::Core => CollisionLayers::new(PhysicsLayers::Core, LayerMask::ALL),
|
|
Team::Inhabitant => CollisionLayers::new(PhysicsLayers::Inhabitant, LayerMask::ALL),
|
|
}
|
|
}
|
|
pub fn enemy_layers(&self) -> LayerMask {
|
|
match self {
|
|
Team::Core => PhysicsLayers::Inhabitant.into(),
|
|
Team::Inhabitant => PhysicsLayers::Core.into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Event, Debug, Default)]
|
|
pub struct Attacked {
|
|
pub attacker: Option<Entity>,
|
|
pub amount: f32,
|
|
pub skip_invulnerability: bool,
|
|
}
|
|
|
|
#[derive(Event, Debug)]
|
|
pub struct GaveDamage {
|
|
pub target: Entity,
|
|
pub amount: f32,
|
|
pub skip_invulnerability: bool,
|
|
}
|
|
|
|
#[derive(Event, Debug)]
|
|
pub struct Died {
|
|
pub slayer: Option<Entity>,
|
|
}
|
|
|
|
#[derive(Component, Clone, Copy, Debug, Reflect)]
|
|
#[require(InvulnerabilityTime, Defense)]
|
|
pub struct Health {
|
|
pub max: f32,
|
|
pub current: f32,
|
|
}
|
|
|
|
impl Health {
|
|
pub fn new(health: f32) -> Self {
|
|
Self {
|
|
max: health,
|
|
current: health,
|
|
}
|
|
}
|
|
|
|
pub fn percentage(&self) -> f32 {
|
|
if self.max == 0. {
|
|
return 1.;
|
|
}
|
|
self.current / self.max
|
|
}
|
|
|
|
pub fn max_out(&mut self) {
|
|
self.current = self.max;
|
|
}
|
|
}
|
|
|
|
impl Default for Health {
|
|
fn default() -> Self {
|
|
Self {
|
|
max: 1.0,
|
|
current: 1.0,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[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, Copy, Debug, Default)]
|
|
pub struct Defense {
|
|
pub amount: f32,
|
|
}
|
|
|
|
impl Defense {
|
|
pub fn calc_damage(&self, incoming_damage: f32) -> f32 {
|
|
let min_damage = 0.1;
|
|
if incoming_damage < min_damage {
|
|
return 0.;
|
|
}
|
|
(incoming_damage - self.amount * 0.5).max(min_damage)
|
|
}
|
|
|
|
pub fn new(amount: f32) -> Self {
|
|
Self { amount }
|
|
}
|
|
}
|
|
|
|
fn on_attack(
|
|
trigger: Trigger<Attacked>,
|
|
mut commands: Commands,
|
|
attacker_query: Query<&Team>,
|
|
mut attacked_query: Query<
|
|
(
|
|
&mut Health,
|
|
&InvulnerabilityTime,
|
|
Option<&Defense>,
|
|
Option<&Team>,
|
|
),
|
|
Without<Invulnerable>,
|
|
>,
|
|
names: Query<&Name>,
|
|
difficulty: Res<Difficulty>,
|
|
) {
|
|
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))
|
|
&& let Some(team) = opt_team
|
|
&& team == attacker_team
|
|
{
|
|
return;
|
|
}
|
|
let defense = opt_defense.cloned().unwrap_or_default();
|
|
let damage_amount = if matches!(opt_team, Some(Team::Core)) {
|
|
info!(
|
|
"reduced damage by {:?}",
|
|
difficulty.core_damage_taken_mult()
|
|
);
|
|
trigger.event().amount * difficulty.core_damage_taken_mult()
|
|
} else {
|
|
trigger.event().amount
|
|
};
|
|
let damage_taken = defense.calc_damage(damage_amount);
|
|
let old_health = health.current;
|
|
health.current = (old_health - 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 let Some(attacker) = trigger.event().attacker {
|
|
commands.trigger_targets(
|
|
GaveDamage {
|
|
target: trigger.target(),
|
|
amount: old_health - health.current,
|
|
skip_invulnerability: trigger.event().skip_invulnerability,
|
|
},
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
|
|
fn on_die(
|
|
trigger: Trigger<Died>,
|
|
names: Query<&Name>,
|
|
drops: Query<&Drops>,
|
|
mut resources: ResMut<Resources>,
|
|
mut commands: Commands,
|
|
) {
|
|
if let Ok(drop) = drops.get(trigger.target()) {
|
|
resources.0 = resources.0 + drop.0;
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
|
|
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
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Component, Deref, DerefMut, Clone, Copy)]
|
|
pub struct Range(pub f32);
|
|
|
|
impl Range {
|
|
pub fn within(&self, a: Vec3, b: Vec3) -> bool {
|
|
a.distance_squared(b) < self.0 * self.0
|
|
}
|
|
}
|
|
|
|
impl Default for Range {
|
|
fn default() -> Self {
|
|
Self(5.)
|
|
}
|
|
}
|