proper cash heal calc

This commit is contained in:
2025-04-08 10:11:20 +02:00
parent 70751e5fce
commit ff85c81e71
2 changed files with 57 additions and 8 deletions

View File

@@ -8,6 +8,12 @@ pub fn plugin(app: &mut App) {
global_observer!(app, on_heal_trigger); global_observer!(app, on_heal_trigger);
} }
#[derive(Debug, PartialEq, Eq)]
struct HealAction {
cost: i32,
damage_healed: u32,
}
fn on_heal_trigger( fn on_heal_trigger(
_trigger: Trigger<TriggerCashHeal>, _trigger: Trigger<TriggerCashHeal>,
mut cmds: Commands, mut cmds: Commands,
@@ -18,19 +24,58 @@ fn on_heal_trigger(
return; return;
}; };
if hp.max() { if hp.max() || cash.cash == 0 {
return; return;
} }
let cost = 10; let action = heal(cash.cash, hp.get().1 - hp.get().0);
if cash.cash < cost { hp.heal(action.damage_healed);
return;
}
hp.set_health(100); cash.cash = cash.cash.saturating_sub(action.cost);
cash.cash = cash.cash.saturating_sub(cost);
//TODO: trigger ui cost animation
cmds.trigger(PlaySound::CashHeal); cmds.trigger(PlaySound::CashHeal);
} }
fn heal(cash: i32, damage: u32) -> HealAction {
let cost = (damage as f32 / 10. * 25.) as i32;
if cash >= cost {
HealAction {
cost,
damage_healed: damage,
}
} else {
let damage_healed = (cash as f32 * 10. / 25.) as u32;
HealAction {
cost: cash,
damage_healed,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_heal() {
assert_eq!(
heal(100, 10),
HealAction {
cost: 25,
damage_healed: 10
}
);
assert_eq!(
heal(100, 90),
HealAction {
cost: 100,
damage_healed: 40
}
);
}
}

View File

@@ -29,6 +29,10 @@ impl Hitpoints {
self.current = v; self.current = v;
} }
pub fn heal(&mut self, v: u32) {
self.current += v;
}
pub fn get(&self) -> (u32, u32) { pub fn get(&self) -> (u32, u32) {
(self.current, self.max) (self.current, self.max)
} }