diff options
Diffstat (limited to 'src/shared/damage.rs')
-rw-r--r-- | src/shared/damage.rs | 37 |
1 files changed, 37 insertions, 0 deletions
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) + } +} |