From f7428e317d282856d05ffc37b6ba474c5e9973ce Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Sun, 24 Mar 2024 14:32:01 +0100 Subject: feat: damage types --- src/shared/damage.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/shared/damage.rs (limited to 'src/shared/damage.rs') diff --git a/src/shared/damage.rs b/src/shared/damage.rs new file mode 100644 index 0000000..c336d87 --- /dev/null +++ b/src/shared/damage.rs @@ -0,0 +1,37 @@ +use crate::shared::stats::*; +use crate::shared::*; + +#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)] +pub enum Damage { + Magical(f32), + Physical(f32), + True(f32), +} + +impl Damage { + pub fn apply(self, stats: Stats) -> f32 { + match self { + Damage::Magical(magical_damage) => { + magical_damage * damage_multiplier(stats.magic_resist) + } + Damage::Physical(physical_damage) => physical_damage * damage_multiplier(stats.armor), + Damage::True(true_damage) => true_damage, + } + } + + pub fn scale(self, factor: f32) -> Self { + match self { + Damage::Magical(magical_damage) => Damage::Magical(magical_damage * factor), + Damage::Physical(physical_damage) => Damage::Physical(physical_damage * factor), + Damage::True(true_damage) => Damage::True(true_damage * factor), + } + } +} + +fn damage_multiplier(resistance: f32) -> f32 { + if resistance >= 0. { + 100. / (100. + resistance) + } else { + 2. - 100. / (100. - resistance) + } +} -- cgit v1.2.3