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(); let new = player.get_life();
messages.insert( messages.insert(
0, 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; self.health = 0;
} else { } else {

View File

@ -165,24 +165,32 @@ impl Game {
None => {} None => {}
Some(m) => { Some(m) => {
// TODO fight the monster // TODO fight the monster
let monster_dmg = m.damage() as i16;
let player_dmg = self.player.damage(); let player_dmg = self.player.damage();
self.player.change_life(-monster_dmg);
m.decrease_life(player_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() { if m.is_dead() {
self.player.gain_experience(m.get_experience_gain()); 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(); return m.is_dead();
} }

View File

@ -149,10 +149,7 @@ impl Level {
{ {
// TODO handle fight between monster and player // TODO handle fight between monster and player
let monster_dmg = self.monsters[index].damage() as i16; 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); player.change_life(-monster_dmg);
messages.insert( messages.insert(
0, 0,
format!( format!(
@ -162,18 +159,32 @@ impl Level {
) )
.to_string(), .to_string(),
); );
messages.insert(
0, let player_dmg = player.damage();
format!( self.monsters[index].decrease_life(player_dmg);
"you hit {} for {} damage.",
self.monsters[index].get_name(),
player_dmg
)
.to_string(),
);
// if the attack did not kill the opponent, back down // if the attack did not kill the opponent, back down
if !player.is_dead() { 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); 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; break;

View File

@ -108,7 +108,7 @@ fn main() -> Result<()> {
game.get_player().get_max_life(), game.get_player().get_max_life(),
game.get_player().get_experience(), game.get_player().get_experience(),
game.get_player().get_gold(), 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) .block(block)
.wrap(Wrap { trim: true }), .wrap(Wrap { trim: true }),