Only consume potions if they have an effect

This commit is contained in:
Joachim Lusiardi 2023-12-30 16:44:23 +01:00
parent b3d64f7438
commit a69e89c806
2 changed files with 8 additions and 3 deletions

View File

@ -68,9 +68,12 @@ impl Artifact for Potion {
} }
fn get_immutable_position(&self) -> &Position { &self.position } fn get_immutable_position(&self) -> &Position { &self.position }
fn collect(&mut self, player: &mut Player) { fn collect(&mut self, player: &mut Player) {
// 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()); player.change_life(self.health.try_into().unwrap());
self.health = 0; self.health = 0;
} }
}
fn was_collected(&self) -> bool { fn was_collected(&self) -> bool {
self.health == 0 self.health == 0

View File

@ -28,11 +28,13 @@ impl Player {
pub fn change_life(&mut self, by: i16) { pub fn change_life(&mut self, by: i16) {
self.life = max(0, min(self.max_life, self.life + by)); 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 { pub fn get_life(&self) -> i16 {
self.life self.life
} }
/// returns true if the player is dead (life <= 0)
pub fn is_dead(&self) -> bool { self.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 { pub fn get_max_life(&self) -> i16 {
self.max_life self.max_life
} }