From f87712e8d26435d7df9907d1b54dd882cfc1c161 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sun, 17 Dec 2023 19:48:58 +0100 Subject: [PATCH] clean up code --- src/game.rs | 1 - src/level.rs | 65 +++----------------------------------------------- src/monster.rs | 3 +++ 3 files changed, 6 insertions(+), 63 deletions(-) diff --git a/src/game.rs b/src/game.rs index 4f21fd3..722d43b 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,4 +1,3 @@ -use std::ops::Deref; use crate::artifacts::Artifact; use crate::level::{Level, StructureElement}; use crate::level_generator::LevelGenerator; diff --git a/src/level.rs b/src/level.rs index 9a4420c..1225c9f 100644 --- a/src/level.rs +++ b/src/level.rs @@ -32,6 +32,7 @@ pub struct Level { } impl Level { + #[cfg(test)] pub fn new(level: usize) -> Level { let mut s = [[StructureElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH]; for x in 2..LEVEL_WIDTH - 2 { @@ -76,6 +77,7 @@ impl Level { } (Some(self.structure[x][y]), res_m, res_a) } + #[cfg(test)] pub fn add_monster(&mut self, mut monster: Monster) -> Result<(), String> { if self.level != monster.get_position().get_level() { return Err("Wrong Level".to_string()); @@ -89,21 +91,7 @@ impl Level { Ok(()) } - pub fn remove_dead_monster(&mut self, position: &Position) -> Result<(), String> { - if self.level != position.get_level() { - return Err("Wrong Level".to_string()); - } - for (index, m) in &mut self.monsters.iter().enumerate() { - if m.get_immutable_position() == position { - if !m.is_dead() { - return Err("Monster not dead".to_string()); - } - self.monsters.remove(index); - break; - } - } - Ok(()) - } + #[cfg(test)] pub fn add_artifact(&mut self, artifact: impl Artifact + 'static) -> Result<(), String> { if self.level != artifact.get_immutable_position().get_level() { return Err("Wrong Level".to_string()); @@ -116,18 +104,6 @@ impl Level { self.artifacts.push(Box::new(artifact)); Ok(()) } - pub fn remove_artifact(&mut self, position: &Position) -> Result<(), String> { - if self.level != position.get_level() { - return Err("Wrong Level".to_string()); - } - for (index, a) in &mut self.artifacts.iter().enumerate() { - if a.get_immutable_position() == position { - self.artifacts.remove(index); - break; - } - } - Ok(()) - } /// discover the area with in the level around the given position pub fn discover(&mut self, pos: &Position) { let x = pos.get_x(); @@ -304,38 +280,3 @@ fn test_discover_get_monster_can_move() { assert!(m.is_some()); assert_eq!(m.unwrap().get_life(), 23); } - -#[test] -fn test_monster_can_be_removed() { - let mut level = Level::new(0); - level.discover(&Position::new(0, 10, 10)); - - let mut m = Monster::new(2); - m.get_position().set(0, 10, 10); - level.add_monster(m).expect("Panic because of"); - - let m = level.get_element(10, 10).1.unwrap(); - m.decrease_life(3); - - assert_eq!(level.remove_dead_monster(&Position::new(0, 10, 10)), Ok(())); - - assert!(level.get_element(10, 10).1.is_none()); -} - -#[test] -fn test_artefact_can_be_removed() { - let mut l = Level::new(0); - let p = Position::new(0, 10, 10); - l.discover(&p); - assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Floor); - assert!(l.get_element(10, 10).2.is_none()); - - let a = Chest::new(Position::new(0, 10, 10)); - assert_eq!(l.add_artifact(a), Ok(())); - - l.remove_artifact(&Position::new(0, 10, 10)).expect("Foo"); - - let elem = l.get_element(10, 10); - assert_eq!(elem.0.unwrap(), StructureElement::Floor); - assert!(elem.2.is_none()); -} diff --git a/src/monster.rs b/src/monster.rs index 055a839..326542f 100644 --- a/src/monster.rs +++ b/src/monster.rs @@ -6,6 +6,7 @@ pub struct Monster { } impl Monster { + #[cfg(test)] pub fn new(life: usize) -> Self { Monster { life, @@ -18,11 +19,13 @@ impl Monster { position, } } + #[cfg(test)] pub fn get_life(&self) -> usize { self.life } pub fn is_dead(&self) -> bool { self.life <= 0 } pub fn decrease_life(&mut self, by: usize) { self.life = self.life.saturating_sub(by); } + #[cfg(test)] pub fn get_position(&mut self) -> &mut Position { &mut self.position }