54 lines
1.2 KiB
Rust
54 lines
1.2 KiB
Rust
|
use std::cmp::{max, min};
|
||
|
use crate::position::Position;
|
||
|
|
||
|
pub struct Player {
|
||
|
name: String,
|
||
|
position: Position,
|
||
|
life: i16,
|
||
|
max_life: i16,
|
||
|
}
|
||
|
|
||
|
impl Player {
|
||
|
pub fn new(name: &str, max_life: i16) -> Player {
|
||
|
Player {
|
||
|
name: name.to_string(),
|
||
|
position: Position::new(0, 1, 1),
|
||
|
life: max_life,
|
||
|
max_life,
|
||
|
}
|
||
|
}
|
||
|
pub fn get_name(&self) -> String {
|
||
|
return self.name.clone();
|
||
|
}
|
||
|
pub fn change_life(&mut self, by: i16) {
|
||
|
self.life = max(0, min(self.max_life, self.life + by));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_get_name() {
|
||
|
let p = Player {
|
||
|
name: "Teddy Tester".to_string(),
|
||
|
position: Position::new(0, 1, 1),
|
||
|
life: 5,
|
||
|
max_life: 10,
|
||
|
};
|
||
|
assert_eq!(p.get_name(), "Teddy Tester");
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_change_life() {
|
||
|
let mut p = Player {
|
||
|
name: "Teddy Tester".to_string(),
|
||
|
position: Position::new(0, 1, 1),
|
||
|
life: 5,
|
||
|
max_life: 10,
|
||
|
};
|
||
|
assert_eq!(p.life, 5);
|
||
|
p.change_life(-2);
|
||
|
assert_eq!(p.life, 3);
|
||
|
p.change_life(10);
|
||
|
assert_eq!(p.life, 10);
|
||
|
p.change_life(-12);
|
||
|
assert_eq!(p.life, 0);
|
||
|
}
|