add gold to the player

This commit is contained in:
Joachim Lusiardi 2023-12-03 21:20:16 +01:00
parent 220607133a
commit 07c629bdaa
1 changed files with 20 additions and 0 deletions

View File

@ -6,6 +6,7 @@ pub struct Player {
position: Position, position: Position,
life: i16, life: i16,
max_life: i16, max_life: i16,
gold: usize,
} }
impl Player { impl Player {
@ -15,6 +16,7 @@ impl Player {
position: Position::new(0, 0, 0), position: Position::new(0, 0, 0),
life: max_life, life: max_life,
max_life, max_life,
gold: 0,
} }
} }
pub fn get_name(&self) -> String { pub fn get_name(&self) -> String {
@ -29,6 +31,12 @@ impl Player {
pub fn get_position(&mut self) -> &mut Position { pub fn get_position(&mut self) -> &mut Position {
&mut self.position &mut self.position
} }
/// add the given amount to the players gold stash
pub fn retrieve_gold(&mut self, amount: usize) { self.gold += amount }
/// return the size of the players gold stash
pub fn get_gold(&self) -> usize { self.gold }
} }
#[test] #[test]
@ -38,10 +46,21 @@ fn test_get_name() {
position: Position::new(0, 1, 1), position: Position::new(0, 1, 1),
life: 5, life: 5,
max_life: 10, max_life: 10,
gold: 0,
}; };
assert_eq!(p.get_name(), "Teddy Tester"); assert_eq!(p.get_name(), "Teddy Tester");
} }
#[test]
fn test_can_receive_gold() {
let mut p = Player::new("Teddy Tester", 10);
assert_eq!(p.get_gold(), 0);
p.retrieve_gold(13);
p.retrieve_gold(10);
assert_eq!(p.get_gold(), 23);
}
#[test] #[test]
fn test_change_life() { fn test_change_life() {
let mut p = Player { let mut p = Player {
@ -49,6 +68,7 @@ fn test_change_life() {
position: Position::new(0, 1, 1), position: Position::new(0, 1, 1),
life: 5, life: 5,
max_life: 10, max_life: 10,
gold: 0,
}; };
assert_eq!(p.life, 5); assert_eq!(p.life, 5);
p.change_life(-2); p.change_life(-2);