186 lines
4.7 KiB
Rust
186 lines
4.7 KiB
Rust
use std::cmp::{max, min};
|
|
|
|
use crate::dungeon_slayer::PlayerStats;
|
|
use crate::position::{HasPosition, Position};
|
|
|
|
pub struct Player {
|
|
name: String,
|
|
position: Position,
|
|
life: i16,
|
|
gold: usize,
|
|
experience: usize,
|
|
pub(crate) player_stats: PlayerStats,
|
|
}
|
|
|
|
impl Player {
|
|
pub fn new(name: &str, player_stats: PlayerStats) -> Player {
|
|
Player {
|
|
name: name.to_string(),
|
|
position: Position::new(0, 0, 0),
|
|
life: player_stats.get_max_life() as i16,
|
|
gold: 0,
|
|
experience: 0,
|
|
player_stats,
|
|
}
|
|
}
|
|
pub fn get_name(&self) -> String {
|
|
return self.name.clone();
|
|
}
|
|
pub fn decrease_life(&mut self, by: u8) {
|
|
self.life = max(0, self.life.saturating_sub(by as i16));
|
|
}
|
|
pub fn increase_life(&mut self, by: u8) {
|
|
self.life = min(self.life.saturating_add(by as i16), self.get_max_life());
|
|
}
|
|
pub fn get_life(&self) -> i16 {
|
|
self.life
|
|
}
|
|
/// returns true if the player is dead (life <= 0)
|
|
pub fn is_dead(&self) -> bool { self.life <= 0 }
|
|
/// returns true if the player's life is at maximum
|
|
pub fn is_healthy(&self) -> bool { self.life == self.get_max_life() }
|
|
pub fn get_max_life(&self) -> i16 {
|
|
self.player_stats.get_max_life() as i16
|
|
}
|
|
|
|
/// 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 }
|
|
|
|
pub fn gain_experience(&mut self, amount: usize) { self.experience += amount }
|
|
|
|
pub fn get_experience(&self) -> usize { self.experience }
|
|
}
|
|
|
|
impl HasPosition for Player {
|
|
fn get_position(&mut self) -> &mut Position {
|
|
&mut self.position
|
|
}
|
|
fn get_immutable_position(&self) -> &Position {
|
|
&self.position
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_get_name() {
|
|
let p = Player {
|
|
name: "Teddy Tester".to_string(),
|
|
position: Position::new(0, 1, 1),
|
|
life: 5,
|
|
gold: 0,
|
|
experience: 0,
|
|
player_stats: PlayerStats {
|
|
body: 0,
|
|
agility: 0,
|
|
mind: 0,
|
|
strength: 0,
|
|
toughness: 0,
|
|
movement: 0,
|
|
dexterity: 0,
|
|
wisdom: 0,
|
|
aura: 0,
|
|
},
|
|
};
|
|
assert_eq!(p.get_name(), "Teddy Tester");
|
|
}
|
|
|
|
#[test]
|
|
fn test_can_receive_gold() {
|
|
let player_stats = PlayerStats {
|
|
body: 8,
|
|
agility: 8,
|
|
mind: 4,
|
|
strength: 3,
|
|
toughness: 3,
|
|
movement: 1,
|
|
dexterity: 1,
|
|
wisdom: 0,
|
|
aura: 0,
|
|
};
|
|
let mut p = Player::new("Teddy Tester", player_stats);
|
|
assert_eq!(p.get_gold(), 0);
|
|
|
|
p.retrieve_gold(13);
|
|
p.retrieve_gold(10);
|
|
assert_eq!(p.get_gold(), 23);
|
|
}
|
|
|
|
#[test]
|
|
fn test_change_life() {
|
|
let mut p = Player {
|
|
name: "Teddy Tester".to_string(),
|
|
position: Position::new(0, 1, 1),
|
|
life: 5,
|
|
gold: 0,
|
|
experience: 0,
|
|
player_stats: PlayerStats {
|
|
body: 0,
|
|
agility: 0,
|
|
mind: 0,
|
|
strength: 0,
|
|
toughness: 0,
|
|
movement: 0,
|
|
dexterity: 0,
|
|
wisdom: 0,
|
|
aura: 0,
|
|
},
|
|
};
|
|
assert_eq!(p.get_life(), 5);
|
|
p.decrease_life(2);
|
|
assert_eq!(p.get_life(), 3);
|
|
p.increase_life(10);
|
|
assert_eq!(p.get_life(), 10);
|
|
p.decrease_life(12);
|
|
assert_eq!(p.get_life(), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn player_can_move() {
|
|
let player_stats = PlayerStats {
|
|
body: 8,
|
|
agility: 8,
|
|
mind: 4,
|
|
strength: 3,
|
|
toughness: 3,
|
|
movement: 1,
|
|
dexterity: 1,
|
|
wisdom: 0,
|
|
aura: 0,
|
|
};
|
|
let mut p = Player::new("Teddy Tester", player_stats);
|
|
assert_eq!(p.get_position(), &Position::new(0, 0, 0));
|
|
p.get_position().change(1, 2);
|
|
assert_eq!(p.get_position(), &Position::new(0, 1, 2));
|
|
p.get_position().change(2, 1);
|
|
assert_eq!(p.get_position(), &Position::new(0, 3, 3));
|
|
p.get_position().set(1, 2, 3);
|
|
p.get_position().change(2, 1);
|
|
assert_eq!(p.get_position(), &Position::new(1, 4, 4));
|
|
assert_eq!(p.get_immutable_position(), &Position::new(1, 4, 4));
|
|
}
|
|
|
|
#[test]
|
|
fn test_max_life() {
|
|
let p = Player {
|
|
name: "Teddy Tester".to_string(),
|
|
position: Position::new(0, 1, 1),
|
|
life: 5,
|
|
gold: 0,
|
|
experience: 0,
|
|
player_stats: PlayerStats {
|
|
body: 0,
|
|
agility: 0,
|
|
mind: 0,
|
|
strength: 0,
|
|
toughness: 0,
|
|
movement: 0,
|
|
dexterity: 0,
|
|
wisdom: 0,
|
|
aura: 0,
|
|
},
|
|
};
|
|
assert_eq!(p.get_max_life(), 10);
|
|
}
|