2020-10-17 22:42:43 +00:00
|
|
|
use crate::{
|
|
|
|
comp::{HealthChange, HealthSource, Loadout},
|
|
|
|
sync::Uid,
|
2020-10-18 18:21:58 +00:00
|
|
|
util::Dir,
|
2020-10-17 22:42:43 +00:00
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2020-10-18 18:21:58 +00:00
|
|
|
use vek::*;
|
2020-10-17 22:42:43 +00:00
|
|
|
|
2020-11-02 00:26:01 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub enum GroupTarget {
|
|
|
|
InGroup,
|
|
|
|
OutOfGroup,
|
|
|
|
}
|
|
|
|
|
2020-11-05 01:21:42 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
2020-10-30 20:41:21 +00:00
|
|
|
pub enum DamageSource {
|
|
|
|
Melee,
|
|
|
|
Healing,
|
|
|
|
Projectile,
|
|
|
|
Explosion,
|
|
|
|
Falling,
|
|
|
|
Shockwave,
|
|
|
|
Energy,
|
2020-11-05 01:21:42 +00:00
|
|
|
Other,
|
2020-10-17 22:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2020-10-30 20:41:21 +00:00
|
|
|
pub struct Damage {
|
|
|
|
pub source: DamageSource,
|
|
|
|
pub value: f32,
|
2020-10-17 22:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Damage {
|
2020-11-04 02:01:12 +00:00
|
|
|
pub fn modify_damage(self, loadout: Option<&Loadout>, uid: Option<Uid>) -> HealthChange {
|
2020-10-30 20:41:21 +00:00
|
|
|
let mut damage = self.value;
|
|
|
|
match self.source {
|
|
|
|
DamageSource::Melee => {
|
2020-10-17 22:42:43 +00:00
|
|
|
// Critical hit
|
|
|
|
let mut critdamage = 0.0;
|
|
|
|
if rand::random() {
|
|
|
|
critdamage = damage * 0.3;
|
|
|
|
}
|
|
|
|
// Armor
|
2020-10-27 22:16:17 +00:00
|
|
|
let damage_reduction = loadout.map_or(0.0, |l| l.get_damage_reduction());
|
2020-10-17 22:42:43 +00:00
|
|
|
damage *= 1.0 - damage_reduction;
|
|
|
|
|
|
|
|
// Critical damage applies after armor for melee
|
|
|
|
if (damage_reduction - 1.0).abs() > f32::EPSILON {
|
|
|
|
damage += critdamage;
|
|
|
|
}
|
|
|
|
|
|
|
|
HealthChange {
|
|
|
|
amount: -damage as i32,
|
2020-11-05 01:21:42 +00:00
|
|
|
cause: HealthSource::Damage {
|
|
|
|
kind: self.source,
|
|
|
|
by: uid,
|
|
|
|
},
|
2020-10-17 22:42:43 +00:00
|
|
|
}
|
|
|
|
},
|
2020-10-30 20:41:21 +00:00
|
|
|
DamageSource::Projectile => {
|
2020-10-17 22:42:43 +00:00
|
|
|
// Critical hit
|
|
|
|
if rand::random() {
|
|
|
|
damage *= 1.2;
|
|
|
|
}
|
|
|
|
// Armor
|
2020-10-27 22:16:17 +00:00
|
|
|
let damage_reduction = loadout.map_or(0.0, |l| l.get_damage_reduction());
|
2020-10-17 22:42:43 +00:00
|
|
|
damage *= 1.0 - damage_reduction;
|
|
|
|
|
|
|
|
HealthChange {
|
|
|
|
amount: -damage as i32,
|
2020-11-05 01:21:42 +00:00
|
|
|
cause: HealthSource::Damage {
|
|
|
|
kind: self.source,
|
|
|
|
by: uid,
|
|
|
|
},
|
2020-10-17 22:42:43 +00:00
|
|
|
}
|
|
|
|
},
|
2020-10-30 20:41:21 +00:00
|
|
|
DamageSource::Explosion => {
|
2020-10-17 22:42:43 +00:00
|
|
|
// Armor
|
2020-10-27 22:16:17 +00:00
|
|
|
let damage_reduction = loadout.map_or(0.0, |l| l.get_damage_reduction());
|
2020-10-17 22:42:43 +00:00
|
|
|
damage *= 1.0 - damage_reduction;
|
|
|
|
|
|
|
|
HealthChange {
|
|
|
|
amount: -damage as i32,
|
2020-11-05 01:21:42 +00:00
|
|
|
cause: HealthSource::Damage {
|
|
|
|
kind: self.source,
|
|
|
|
by: uid,
|
|
|
|
},
|
2020-10-17 22:42:43 +00:00
|
|
|
}
|
|
|
|
},
|
2020-10-30 20:41:21 +00:00
|
|
|
DamageSource::Shockwave => {
|
2020-10-17 22:42:43 +00:00
|
|
|
// Armor
|
2020-10-27 22:16:17 +00:00
|
|
|
let damage_reduction = loadout.map_or(0.0, |l| l.get_damage_reduction());
|
2020-10-17 22:42:43 +00:00
|
|
|
damage *= 1.0 - damage_reduction;
|
|
|
|
|
|
|
|
HealthChange {
|
|
|
|
amount: -damage as i32,
|
2020-11-05 01:21:42 +00:00
|
|
|
cause: HealthSource::Damage {
|
|
|
|
kind: self.source,
|
|
|
|
by: uid,
|
|
|
|
},
|
2020-10-17 22:42:43 +00:00
|
|
|
}
|
|
|
|
},
|
2020-10-30 20:41:21 +00:00
|
|
|
DamageSource::Energy => {
|
2020-10-17 22:42:43 +00:00
|
|
|
// Armor
|
2020-10-27 22:16:17 +00:00
|
|
|
let damage_reduction = loadout.map_or(0.0, |l| l.get_damage_reduction());
|
2020-10-17 22:42:43 +00:00
|
|
|
damage *= 1.0 - damage_reduction;
|
|
|
|
|
|
|
|
HealthChange {
|
|
|
|
amount: -damage as i32,
|
2020-11-05 01:21:42 +00:00
|
|
|
cause: HealthSource::Damage {
|
|
|
|
kind: self.source,
|
|
|
|
by: uid,
|
|
|
|
},
|
2020-10-17 22:42:43 +00:00
|
|
|
}
|
|
|
|
},
|
2020-10-30 20:41:21 +00:00
|
|
|
DamageSource::Healing => HealthChange {
|
|
|
|
amount: damage as i32,
|
2020-11-05 01:21:42 +00:00
|
|
|
cause: HealthSource::Heal { by: uid },
|
2020-10-17 22:42:43 +00:00
|
|
|
},
|
2020-10-30 20:41:21 +00:00
|
|
|
DamageSource::Falling => {
|
2020-10-17 22:42:43 +00:00
|
|
|
// Armor
|
2020-10-27 22:16:17 +00:00
|
|
|
let damage_reduction = loadout.map_or(0.0, |l| l.get_damage_reduction());
|
2020-10-17 22:42:43 +00:00
|
|
|
if (damage_reduction - 1.0).abs() < f32::EPSILON {
|
|
|
|
damage = 0.0;
|
|
|
|
}
|
|
|
|
HealthChange {
|
|
|
|
amount: -damage as i32,
|
|
|
|
cause: HealthSource::World,
|
|
|
|
}
|
|
|
|
},
|
2020-11-05 01:21:42 +00:00
|
|
|
DamageSource::Other => HealthChange {
|
|
|
|
amount: -damage as i32,
|
|
|
|
cause: HealthSource::Damage {
|
|
|
|
kind: self.source,
|
|
|
|
by: uid,
|
|
|
|
},
|
|
|
|
},
|
2020-10-17 22:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-30 20:41:21 +00:00
|
|
|
|
|
|
|
pub fn interpolate_damage(&mut self, frac: f32, min: f32) {
|
|
|
|
let new_damage = min + frac * (self.value - min);
|
|
|
|
self.value = new_damage;
|
|
|
|
}
|
2020-10-17 22:42:43 +00:00
|
|
|
}
|
2020-10-18 18:21:58 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub enum Knockback {
|
|
|
|
Away(f32),
|
|
|
|
Towards(f32),
|
|
|
|
Up(f32),
|
|
|
|
TowardsUp(f32),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Knockback {
|
2020-10-27 22:16:17 +00:00
|
|
|
pub fn calculate_impulse(self, dir: Dir) -> Vec3<f32> {
|
2020-10-18 18:21:58 +00:00
|
|
|
match self {
|
|
|
|
Knockback::Away(strength) => strength * *Dir::slerp(dir, Dir::new(Vec3::unit_z()), 0.5),
|
|
|
|
Knockback::Towards(strength) => {
|
|
|
|
strength * *Dir::slerp(-dir, Dir::new(Vec3::unit_z()), 0.5)
|
|
|
|
},
|
|
|
|
Knockback::Up(strength) => strength * Vec3::unit_z(),
|
|
|
|
Knockback::TowardsUp(strength) => {
|
|
|
|
strength * *Dir::slerp(-dir, Dir::new(Vec3::unit_z()), 0.85)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|