Show last message on death

This commit is contained in:
Joachim Lusiardi 2024-01-14 12:17:23 +01:00
parent efe531f6ce
commit 9bd2f48b2e
3 changed files with 13 additions and 9 deletions

View File

@ -130,14 +130,14 @@ impl Game {
(dx, dy) = (0, 0);
let (next_level, x, y) = self.next_start();
player_level = next_level;
self.messages.insert(0, format!("you climb down to level {}.", next_level));
self.messages.insert(0, format!("You climb down to level {}.", next_level));
self.get_mutable_player().get_position().set(next_level, x, y);
}
StructureElement::StairUp => {
(dx, dy) = (0, 0);
let (next_level, x, y) = self.prev_end();
player_level = next_level;
self.messages.insert(0, format!("you climb up to level {}.", next_level));
self.messages.insert(0, format!("You climb up to level {}.", next_level));
self.get_mutable_player().get_position().set(next_level, x, y);
}
_ => {}
@ -167,15 +167,15 @@ impl Game {
if defense.0 {
damage = damage.saturating_sub(defense.1);
}
self.messages.insert(0, format!("attack on {} successful with {} damage.", m.get_name(), damage).to_string());
self.messages.insert(0, format!("Attack on {} successful with {} damage.", m.get_name(), damage).to_string());
m.decrease_life(damage as usize);
} else {
self.messages.insert(0, format!("attack on {} failed.", m.get_name()).to_string());
self.messages.insert(0, format!("Attack on {} failed.", m.get_name()).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 killed the {}..", m.get_name()).to_string());
self.messages.insert(0, format!("You killed the {}..", m.get_name()).to_string());
}
return m.is_dead();
}

View File

@ -141,10 +141,14 @@ impl Level {
if defense.0 {
damage = damage.saturating_sub(defense.1);
}
messages.insert(0, format!("attack from {} successful with {} damage.", self.monsters[index].get_name(), damage).to_string());
player.decrease_life(damage);
if player.is_dead() {
messages.insert(0, format!("Attack from {} killed you.", self.monsters[index].get_name()).to_string());
} else {
messages.insert(0, format!("Attack from {} successful with {} damage.", self.monsters[index].get_name(), damage).to_string());
}
} else {
messages.insert(0, format!("attack from {} failed.", self.monsters[index].get_name()).to_string());
messages.insert(0, format!("Attack from {} failed.", self.monsters[index].get_name()).to_string());
}
if !player.is_dead() {
self.monsters[index].get_position().change(-dx, -dy);

View File

@ -184,13 +184,13 @@ fn main() -> Result<()> {
"Quitting is for cowards! You'll better try again!".to_string()
}
GameState::LOST => {
"Sorry, you died in the dungeon. Better luck next time!".to_string()
format!("Sorry, you died in the dungeon. Better luck next time!\nLast message:\n{}", game.messages[0]).to_string()
}
GameState::WON => {
"Congratulation! You mastered your way through the dungeon and won the game.".to_string()
}
};
text += format!("\nYou gained {} experience.", game.get_player().get_experience()).as_str();
text += format!("\n\nYou gained {} experience.", game.get_player().get_experience()).as_str();
text += format!("\nYou collected {} gold.", game.get_player().get_gold()).as_str();
text += format!("\nYou played {} seconds.", playtime.as_secs()).as_str();
let paragraph = Paragraph::new(text).block(block).wrap(Wrap { trim: true });