clean up code
This commit is contained in:
parent
8b21e179c2
commit
f87712e8d2
@ -1,4 +1,3 @@
|
||||
use std::ops::Deref;
|
||||
use crate::artifacts::Artifact;
|
||||
use crate::level::{Level, StructureElement};
|
||||
use crate::level_generator::LevelGenerator;
|
||||
|
65
src/level.rs
65
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());
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user