From b3d85ac7b354da8f8ca17672c461a6f1efbf7076 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Mon, 21 Oct 2024 08:26:51 +0200 Subject: [PATCH] Improved fights between player and monster --- src/artifacts.rs | 2 +- src/game.rs | 36 ++++++++++++++++++++++-------------- src/level.rs | 35 +++++++++++++++++++++++------------ src/main.rs | 2 +- 4 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/artifacts.rs b/src/artifacts.rs index 00c17c4..bab05c6 100644 --- a/src/artifacts.rs +++ b/src/artifacts.rs @@ -83,7 +83,7 @@ impl Artifact for Potion { let new = player.get_life(); messages.insert( 0, - format!("picked up potion and gained {} hp.", new - old).to_string(), + format!("picked up potion and gained {} health.", new - old).to_string(), ); self.health = 0; } else { diff --git a/src/game.rs b/src/game.rs index b0f5681..32969a7 100644 --- a/src/game.rs +++ b/src/game.rs @@ -165,24 +165,32 @@ impl Game { None => {} Some(m) => { // TODO fight the monster - - let monster_dmg = m.damage() as i16; let player_dmg = self.player.damage(); - self.player.change_life(-monster_dmg); m.decrease_life(player_dmg); - - // inform player - self.messages.insert( - 0, - format!("{} hits you for {} damage.", m.get_name(), monster_dmg).to_string(), - ); - self.messages.insert( - 0, - format!("you hit {} for {} damage.", m.get_name(), player_dmg).to_string(), - ); - // monster died, player gains experience if m.is_dead() { self.player.gain_experience(m.get_experience_gain()); + self.messages.insert( + 0, + format!( + "you hit {} for {} damage and kill it.", + m.get_name(), + player_dmg + ) + .to_string(), + ); + } else { + self.messages.insert( + 0, + format!("you hit {} for {} damage.", m.get_name(), player_dmg).to_string(), + ); + let monster_dmg = m.damage() as i16; + self.player.change_life(-monster_dmg); + + self.messages.insert( + 0, + format!("{} hits you for {} damage.", m.get_name(), monster_dmg) + .to_string(), + ); } return m.is_dead(); } diff --git a/src/level.rs b/src/level.rs index 235079f..2d183c9 100644 --- a/src/level.rs +++ b/src/level.rs @@ -149,10 +149,7 @@ impl Level { { // TODO handle fight between monster and player let monster_dmg = self.monsters[index].damage() as i16; - let player_dmg = player.damage(); - self.monsters[index].decrease_life(player_dmg); player.change_life(-monster_dmg); - messages.insert( 0, format!( @@ -162,18 +159,32 @@ impl Level { ) .to_string(), ); - messages.insert( - 0, - format!( - "you hit {} for {} damage.", - self.monsters[index].get_name(), - player_dmg - ) - .to_string(), - ); + + let player_dmg = player.damage(); + self.monsters[index].decrease_life(player_dmg); + // if the attack did not kill the opponent, back down if !player.is_dead() { + messages.insert( + 0, + format!( + "you hit {} for {} damage and kill it.", + self.monsters[index].get_name(), + player_dmg + ) + .to_string(), + ); self.monsters[index].get_position().change(-dx, -dy); + } else { + messages.insert( + 0, + format!( + "you hit {} for {} damage.", + self.monsters[index].get_name(), + player_dmg + ) + .to_string(), + ); } } break; diff --git a/src/main.rs b/src/main.rs index bfe7e77..c237013 100644 --- a/src/main.rs +++ b/src/main.rs @@ -108,7 +108,7 @@ fn main() -> Result<()> { game.get_player().get_max_life(), game.get_player().get_experience(), game.get_player().get_gold(), - game.get_player().get_immutable_position().get_level()+1 + game.get_player().get_immutable_position().get_level() + 1 )) .block(block) .wrap(Wrap { trim: true }),