Monsters can be removed
This commit is contained in:
parent
209a277684
commit
900739190d
34
src/level.rs
34
src/level.rs
@ -57,6 +57,22 @@ impl Level {
|
||||
|
||||
(Some(self.structure[x][y]), res_m)
|
||||
}
|
||||
// pub fn remove_monster(&mut self, mut monster: Monster) -> Result<(), String> {
|
||||
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(())
|
||||
}
|
||||
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());
|
||||
@ -69,6 +85,7 @@ impl Level {
|
||||
self.monsters.push(monster);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// discover the area with in the level around the given position
|
||||
pub fn discover(&mut self, pos: &Position) {
|
||||
let x = pos.get_x();
|
||||
@ -198,3 +215,20 @@ 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());
|
||||
}
|
||||
|
@ -22,6 +22,9 @@ impl Monster {
|
||||
pub fn get_position(&mut self) -> &mut Position {
|
||||
&mut self.position
|
||||
}
|
||||
pub fn get_immutable_position(&self) -> &Position {
|
||||
&self.position
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user