aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/damage.rs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-03-24 14:32:01 +0100
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-03-24 14:34:41 +0100
commitf7428e317d282856d05ffc37b6ba474c5e9973ce (patch)
treecf016b07c1145ad4b62c210eb739085c0639f407 /src/shared/damage.rs
parent1751960338fc7a08fec438a9b53c78118a35063a (diff)
feat: damage types
Diffstat (limited to 'src/shared/damage.rs')
-rw-r--r--src/shared/damage.rs37
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)
+ }
+}