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,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 {

View File

@ -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
}