clean up monsters

This commit is contained in:
Joachim Lusiardi 2023-12-25 13:26:45 +01:00
parent 67faae44b1
commit bb8a24aa91
2 changed files with 13 additions and 42 deletions

View File

@ -3,9 +3,9 @@ use std::cmp::{max, min};
#[cfg(test)] #[cfg(test)]
use crate::artifacts::{Chest, Potion}; use crate::artifacts::{Chest, Potion};
use crate::artifacts::Artifact; use crate::artifacts::Artifact;
use crate::monster::Monster;
#[cfg(test)] #[cfg(test)]
use crate::monster::Rat; use crate::monster::{Orc, Rat};
use crate::monster::Monster;
use crate::position::Position; use crate::position::Position;
pub const LEVEL_WIDTH: usize = 50; pub const LEVEL_WIDTH: usize = 50;
@ -193,20 +193,17 @@ fn test_discover_get_element() {
#[test] #[test]
fn test_discover_can_add_monster() { fn test_discover_can_add_monster() {
let mut l = Level::new(0); let mut l = Level::new(0);
let mut m = Rat::new(2); let m = Rat::new_with_position(Position::new(1, 2, 3));
m.get_position().set(1, 2, 3);
assert_eq!(l.add_monster(m), Err("Wrong Level".to_string())); assert_eq!(l.add_monster(m), Err("Wrong Level".to_string()));
let mut m = Rat::new(2); let mut m = Orc::new_with_position(Position::new(0, 2, 3));
m.get_position().set(0, 2, 3); m.get_position().set(0, 2, 3);
assert_eq!(l.add_monster(m), Ok(())); assert_eq!(l.add_monster(m), Ok(()));
let mut m = Rat::new(2); let m = Rat::new_with_position(Position::new(0, 2, 3));
m.get_position().set(0, 2, 3);
assert_eq!(l.add_monster(m), Err("Position already used".to_string())); assert_eq!(l.add_monster(m), Err("Position already used".to_string()));
let mut m = Rat::new(2); let m = Rat::new_with_position(Position::new(0, 2, 4));
m.get_position().set(0, 2, 4);
assert_eq!(l.add_monster(m), Ok(())); assert_eq!(l.add_monster(m), Ok(()));
} }
@ -234,18 +231,17 @@ fn test_discover_get_monster() {
assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Floor); assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Floor);
assert!(l.get_element(10, 10).1.is_none()); assert!(l.get_element(10, 10).1.is_none());
let mut m = Rat::new(23); let m = Rat::new_with_position(Position::new(0, 10, 10));
m.get_position().set(0, 10, 10);
assert_eq!(l.add_monster(m), Ok(())); assert_eq!(l.add_monster(m), Ok(()));
let elem = l.get_element(10, 10); let elem = l.get_element(10, 10);
assert_eq!(elem.0.unwrap(), StructureElement::Floor); assert_eq!(elem.0.unwrap(), StructureElement::Floor);
assert!(elem.1.is_some()); assert!(elem.1.is_some());
let m = elem.1.unwrap(); let m = elem.1.unwrap();
assert_eq!(m.get_life(), 23); assert_eq!(m.get_life(), 2);
m.decrease_life(2); m.decrease_life(2);
assert_eq!(l.get_element(10, 10).1.unwrap().get_life(), 21); assert_eq!(l.get_element(10, 10).1.unwrap().get_life(), 0);
} }
#[test] #[test]
@ -270,8 +266,7 @@ fn test_discover_get_monster_can_move() {
let p = Position::new(0, 10, 10); let p = Position::new(0, 10, 10);
l.discover(&p); l.discover(&p);
let mut m = Rat::new(23); let m = Rat::new_with_position(Position::new(0, 10, 10));
m.get_position().set(0, 10, 10);
l.add_monster(m).expect("Panic because of"); l.add_monster(m).expect("Panic because of");
let m = l.get_element(10, 10).1.unwrap(); let m = l.get_element(10, 10).1.unwrap();
@ -281,5 +276,5 @@ fn test_discover_get_monster_can_move() {
assert!(m.is_none()); assert!(m.is_none());
let m = l.get_element(11, 11).1; let m = l.get_element(11, 11).1;
assert!(m.is_some()); assert!(m.is_some());
assert_eq!(m.unwrap().get_life(), 23); assert_eq!(m.unwrap().get_life(), 2);
} }

View File

@ -45,16 +45,6 @@ pub struct Rat {
} }
impl Rat { impl Rat {
#[cfg(test)]
pub fn new(life: usize) -> Self {
Self {
life,
position: Position::new(0, 0, 0),
symbol: String::from("R"),
color: Color::Black,
experience_gain: 5,
}
}
pub fn new_with_position(position: Position) -> Self { pub fn new_with_position(position: Position) -> Self {
Self { Self {
life: 2, life: 2,
@ -64,8 +54,6 @@ impl Rat {
experience_gain: 5, experience_gain: 5,
} }
} }
#[cfg(test)]
pub fn get_life(&self) -> usize { self.life }
} }
default_monster!(Rat); default_monster!(Rat);
@ -78,16 +66,6 @@ pub struct Orc {
} }
impl Orc { impl Orc {
#[cfg(test)]
pub fn new(life: usize) -> Self {
Self {
life,
position: Position::new(0, 0, 0),
symbol: String::from("O"),
color: Color::DarkGray,
experience_gain: 10,
}
}
pub fn new_with_position(position: Position) -> Self { pub fn new_with_position(position: Position) -> Self {
Self { Self {
life: 4, life: 4,
@ -97,8 +75,6 @@ impl Orc {
experience_gain: 10, experience_gain: 10,
} }
} }
#[cfg(test)]
pub fn get_life(&self) -> usize { self.life }
} }
default_monster!(Orc); default_monster!(Orc);
@ -106,7 +82,7 @@ default_monster!(Orc);
#[test] #[test]
fn monsters_can_move() { fn monsters_can_move() {
let mut m = Rat::new(2); let mut m = Rat::new_with_position(Position::new(0,0,0));
assert_eq!(m.get_position(), &Position::new(0, 0, 0)); assert_eq!(m.get_position(), &Position::new(0, 0, 0));
m.get_position().change(1, 2); m.get_position().change(1, 2);
assert_eq!(m.get_position(), &Position::new(0, 1, 2)); assert_eq!(m.get_position(), &Position::new(0, 1, 2));
@ -119,7 +95,7 @@ fn monsters_can_move() {
#[test] #[test]
fn monsters_can_die() { fn monsters_can_die() {
let mut m = Rat::new(2); let mut m = Rat::new_with_position(Position::new(0,0,0));
assert_eq!(m.get_life(), 2); assert_eq!(m.get_life(), 2);
assert_eq!(m.is_dead(), false); assert_eq!(m.is_dead(), false);
m.decrease_life(1); m.decrease_life(1);