add test & fix bug

This commit is contained in:
Joachim Lusiardi 2023-12-06 20:59:03 +01:00
parent 6aee9d974f
commit 7b0eaf6640
1 changed files with 21 additions and 3 deletions

View File

@ -23,9 +23,9 @@ pub struct Level {
pub(crate) discovered: [[bool; LEVEL_HEIGHT]; LEVEL_WIDTH], pub(crate) discovered: [[bool; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) monsters: Vec<Monster>, pub(crate) monsters: Vec<Monster>,
pub(crate) artifacts: Vec<Box<dyn Artifact>>, pub(crate) artifacts: Vec<Box<dyn Artifact>>,
/// the position of the start in the level (eiter stair up or start point) /// the position of the start in the level (either stair up or start point)
pub(crate) start: (usize, usize), pub(crate) start: (usize, usize),
/// the position of the end in the level (eiter stair down or end point) /// the position of the end in the level (either stair down or end point)
pub(crate) end: (usize, usize), pub(crate) end: (usize, usize),
} }
@ -120,7 +120,7 @@ impl Level {
} }
for (index, a) in &mut self.artifacts.iter().enumerate() { for (index, a) in &mut self.artifacts.iter().enumerate() {
if a.get_immutable_position() == position { if a.get_immutable_position() == position {
self.monsters.remove(index); self.artifacts.remove(index);
break; break;
} }
} }
@ -304,3 +304,21 @@ fn test_monster_can_be_removed() {
assert!(level.get_element(10, 10).1.is_none()); 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());
}