From bb8a24aa91ff4f4b1563b357c79a8f2392594fd2 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Mon, 25 Dec 2023 13:26:45 +0100 Subject: [PATCH] clean up monsters --- src/level.rs | 27 +++++++++++---------------- src/monster.rs | 28 ++-------------------------- 2 files changed, 13 insertions(+), 42 deletions(-) diff --git a/src/level.rs b/src/level.rs index 8e3e6f2..1f4d606 100644 --- a/src/level.rs +++ b/src/level.rs @@ -3,9 +3,9 @@ use std::cmp::{max, min}; #[cfg(test)] use crate::artifacts::{Chest, Potion}; use crate::artifacts::Artifact; -use crate::monster::Monster; #[cfg(test)] -use crate::monster::Rat; +use crate::monster::{Orc, Rat}; +use crate::monster::Monster; use crate::position::Position; pub const LEVEL_WIDTH: usize = 50; @@ -193,20 +193,17 @@ fn test_discover_get_element() { #[test] fn test_discover_can_add_monster() { let mut l = Level::new(0); - let mut m = Rat::new(2); - m.get_position().set(1, 2, 3); + let m = Rat::new_with_position(Position::new(1, 2, 3)); 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); assert_eq!(l.add_monster(m), Ok(())); - let mut m = Rat::new(2); - m.get_position().set(0, 2, 3); + let m = Rat::new_with_position(Position::new(0, 2, 3)); assert_eq!(l.add_monster(m), Err("Position already used".to_string())); - let mut m = Rat::new(2); - m.get_position().set(0, 2, 4); + let m = Rat::new_with_position(Position::new(0, 2, 4)); 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!(l.get_element(10, 10).1.is_none()); - let mut m = Rat::new(23); - m.get_position().set(0, 10, 10); + let m = Rat::new_with_position(Position::new(0, 10, 10)); assert_eq!(l.add_monster(m), Ok(())); let elem = l.get_element(10, 10); assert_eq!(elem.0.unwrap(), StructureElement::Floor); assert!(elem.1.is_some()); let m = elem.1.unwrap(); - assert_eq!(m.get_life(), 23); + assert_eq!(m.get_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] @@ -270,8 +266,7 @@ fn test_discover_get_monster_can_move() { let p = Position::new(0, 10, 10); l.discover(&p); - let mut m = Rat::new(23); - m.get_position().set(0, 10, 10); + let m = Rat::new_with_position(Position::new(0, 10, 10)); l.add_monster(m).expect("Panic because of"); let m = l.get_element(10, 10).1.unwrap(); @@ -281,5 +276,5 @@ fn test_discover_get_monster_can_move() { assert!(m.is_none()); let m = l.get_element(11, 11).1; assert!(m.is_some()); - assert_eq!(m.unwrap().get_life(), 23); + assert_eq!(m.unwrap().get_life(), 2); } diff --git a/src/monster.rs b/src/monster.rs index 02e24c4..8894d57 100644 --- a/src/monster.rs +++ b/src/monster.rs @@ -45,16 +45,6 @@ pub struct 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 { Self { life: 2, @@ -64,8 +54,6 @@ impl Rat { experience_gain: 5, } } - #[cfg(test)] - pub fn get_life(&self) -> usize { self.life } } default_monster!(Rat); @@ -78,16 +66,6 @@ pub struct 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 { Self { life: 4, @@ -97,8 +75,6 @@ impl Orc { experience_gain: 10, } } - #[cfg(test)] - pub fn get_life(&self) -> usize { self.life } } default_monster!(Orc); @@ -106,7 +82,7 @@ default_monster!(Orc); #[test] 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)); m.get_position().change(1, 2); assert_eq!(m.get_position(), &Position::new(0, 1, 2)); @@ -119,7 +95,7 @@ fn monsters_can_move() { #[test] 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.is_dead(), false); m.decrease_life(1);