step to xp

This commit is contained in:
Joachim Lusiardi 2023-12-22 20:10:30 +01:00
parent 6082a740e9
commit de5ea76913
4 changed files with 24 additions and 1 deletions

View File

@ -168,6 +168,10 @@ impl Game {
// TODO fight the monster
self.player.change_life(-1);
m.decrease_life(1);
// monster died, player gains experience
if m.is_dead() {
self.player.gain_experience(m.get_experience_gain());
}
return m.is_dead();
}
};

View File

@ -67,10 +67,11 @@ fn main() -> Result<()> {
height: 25,
};
frame.render_widget(
Paragraph::new(format!("{}\nHealth: {}/{}\nGold: {}\nLevel: {}",
Paragraph::new(format!("{}\nHealth: {}/{}\nExp: {}\nGold: {}\nLevel: {}",
game.get_player().get_name(),
game.get_player().get_life(),
game.get_player().get_max_life(),
game.get_player().get_experience(),
game.get_player().get_gold(),
game.get_player().get_immutable_position().get_level()))
.white()
@ -131,6 +132,7 @@ fn main() -> Result<()> {
"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!("\nYou collected {} gold.", game.get_player().get_gold()).as_str();
let paragraph = Paragraph::new(text).block(block).wrap(Wrap { trim: true });
frame.render_widget(paragraph, area);

View File

@ -7,6 +7,7 @@ pub trait Monster {
fn get_representation(&self) -> (&str, Color);
fn decrease_life(&mut self, by: usize);
fn get_immutable_position(&self) -> &Position;
fn get_experience_gain(&self) -> usize;
#[cfg(test)]
fn get_position(&mut self) -> &mut Position;
#[cfg(test)]
@ -17,6 +18,7 @@ macro_rules! default_monster {
($($t:ty),+ $(,)?) => ($(
impl Monster for $t {
fn is_dead(&self) -> bool { self.life <= 0 }
fn get_experience_gain(&self) -> usize { self.experience_gain }
fn get_representation(&self) -> (&str, Color) { (&self.symbol, self.color) }
fn decrease_life(&mut self, by: usize) {
self.life = self.life.saturating_sub(by);
@ -39,6 +41,7 @@ pub struct Rat {
position: Position,
symbol: String,
color: Color,
experience_gain: usize,
}
impl Rat {
@ -49,6 +52,7 @@ impl Rat {
position: Position::new(0, 0, 0),
symbol: String::from("R"),
color: Color::Black,
experience_gain: 5,
}
}
pub fn new_with_position(position: Position) -> Self {
@ -57,6 +61,7 @@ impl Rat {
position,
symbol: String::from("R"),
color: Color::Black,
experience_gain: 5,
}
}
#[cfg(test)]
@ -69,6 +74,7 @@ pub struct Orc {
position: Position,
symbol: String,
color: Color,
experience_gain: usize,
}
impl Orc {
@ -79,6 +85,7 @@ impl Orc {
position: Position::new(0, 0, 0),
symbol: String::from("O"),
color: Color::DarkGray,
experience_gain: 10,
}
}
pub fn new_with_position(position: Position) -> Self {
@ -87,6 +94,7 @@ impl Orc {
position,
symbol: String::from("O"),
color: Color::DarkGray,
experience_gain: 10,
}
}
#[cfg(test)]

View File

@ -8,6 +8,7 @@ pub struct Player {
life: i16,
max_life: i16,
gold: usize,
experience: usize,
}
impl Player {
@ -18,6 +19,7 @@ impl Player {
life: max_life,
max_life,
gold: 0,
experience: 0,
}
}
pub fn get_name(&self) -> String {
@ -44,6 +46,10 @@ impl Player {
/// return the size of the players gold stash
pub fn get_gold(&self) -> usize { self.gold }
pub fn gain_experience(&mut self, amount: usize) { self.experience += amount }
pub fn get_experience(&self) -> usize { self.experience }
}
#[test]
@ -54,6 +60,7 @@ fn test_get_name() {
life: 5,
max_life: 10,
gold: 0,
experience: 0,
};
assert_eq!(p.get_name(), "Teddy Tester");
}
@ -76,6 +83,7 @@ fn test_change_life() {
life: 5,
max_life: 10,
gold: 0,
experience: 0,
};
assert_eq!(p.get_life(), 5);
p.change_life(-2);
@ -108,6 +116,7 @@ fn test_max_life() {
life: 5,
max_life: 10,
gold: 0,
experience: 0,
};
assert_eq!(p.get_max_life(), 10);
}