aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/damage.rs
blob: c336d87987d6c4512a9fffa85db98033e6ca115d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)
    }
}