Improved fights between player and monster

This commit is contained in:
Joachim Lusiardi 2024-10-21 08:26:51 +02:00
parent cefb0cc5f2
commit b3d85ac7b3
4 changed files with 47 additions and 28 deletions

View File

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

View File

@ -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
if m.is_dead() {
self.player.gain_experience(m.get_experience_gain());
self.messages.insert(
0,
format!("{} hits you for {} damage.", m.get_name(), monster_dmg).to_string(),
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(),
);
// monster died, player gains experience
if m.is_dead() {
self.player.gain_experience(m.get_experience_gain());
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();
}

View File

@ -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,6 +159,23 @@ impl Level {
)
.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!(
@ -171,9 +185,6 @@ impl Level {
)
.to_string(),
);
// if the attack did not kill the opponent, back down
if !player.is_dead() {
self.monsters[index].get_position().change(-dx, -dy);
}
}
break;