diff --git a/src/cash_heal.rs b/src/cash_heal.rs index 71a667c..8d151a4 100644 --- a/src/cash_heal.rs +++ b/src/cash_heal.rs @@ -8,6 +8,12 @@ pub fn plugin(app: &mut App) { global_observer!(app, on_heal_trigger); } +#[derive(Debug, PartialEq, Eq)] +struct HealAction { + cost: i32, + damage_healed: u32, +} + fn on_heal_trigger( _trigger: Trigger, mut cmds: Commands, @@ -18,19 +24,58 @@ fn on_heal_trigger( return; }; - if hp.max() { + if hp.max() || cash.cash == 0 { return; } - let cost = 10; + let action = heal(cash.cash, hp.get().1 - hp.get().0); - if cash.cash < cost { - return; - } + hp.heal(action.damage_healed); - hp.set_health(100); - - cash.cash = cash.cash.saturating_sub(cost); + cash.cash = cash.cash.saturating_sub(action.cost); + //TODO: trigger ui cost animation 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 + } + ); + } +} diff --git a/src/hitpoints.rs b/src/hitpoints.rs index 569791a..f10364f 100644 --- a/src/hitpoints.rs +++ b/src/hitpoints.rs @@ -29,6 +29,10 @@ impl Hitpoints { self.current = v; } + pub fn heal(&mut self, v: u32) { + self.current += v; + } + pub fn get(&self) -> (u32, u32) { (self.current, self.max) }