From a69e89c8066b62ade0797e846cf85874583179ac Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sat, 30 Dec 2023 16:44:23 +0100 Subject: [PATCH] Only consume potions if they have an effect --- src/artifacts.rs | 7 +++++-- src/player.rs | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/artifacts.rs b/src/artifacts.rs index 7eb9a80..3d5fce3 100644 --- a/src/artifacts.rs +++ b/src/artifacts.rs @@ -68,8 +68,11 @@ impl Artifact for Potion { } fn get_immutable_position(&self) -> &Position { &self.position } fn collect(&mut self, player: &mut Player) { - player.change_life(self.health.try_into().unwrap()); - self.health = 0; + // only consume potion of the player can gain at least one health point + if !player.is_healthy() { + player.change_life(self.health.try_into().unwrap()); + self.health = 0; + } } fn was_collected(&self) -> bool { diff --git a/src/player.rs b/src/player.rs index 63338ff..0801005 100644 --- a/src/player.rs +++ b/src/player.rs @@ -28,11 +28,13 @@ impl Player { pub fn change_life(&mut self, by: i16) { self.life = max(0, min(self.max_life, self.life + by)); } - /// returns true if the player is dead (life <= 0) pub fn get_life(&self) -> i16 { self.life } + /// returns true if the player is dead (life <= 0) pub fn is_dead(&self) -> bool { self.life <= 0 } + /// returns true if the player's life is at maximum + pub fn is_healthy(&self) -> bool { self.life == self.max_life } pub fn get_max_life(&self) -> i16 { self.max_life }