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);
if m.is_dead() {
// inform player self.player.gain_experience(m.get_experience_gain());
self.messages.insert( self.messages.insert(
0, 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( self.messages.insert(
0, 0,
format!("you hit {} for {} damage.", m.get_name(), player_dmg).to_string(), format!("you hit {} for {} damage.", m.get_name(), player_dmg).to_string(),
); );
// monster died, player gains experience let monster_dmg = m.damage() as i16;
if m.is_dead() { self.player.change_life(-monster_dmg);
self.player.gain_experience(m.get_experience_gain());
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,6 +159,23 @@ impl Level {
) )
.to_string(), .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( messages.insert(
0, 0,
format!( format!(
@ -171,9 +185,6 @@ impl Level {
) )
.to_string(), .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; 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 }),