monsters as trait

This commit is contained in:
2023-12-18 16:11:19 +01:00
parent 7ac3b76e6c
commit 73315a7636
6 changed files with 104 additions and 36 deletions

View File

@@ -1,8 +1,11 @@
use std::cmp::{max, min};
use crate::artifacts::Artifact;
#[cfg(test)]
use crate::artifacts::{Chest, Potion};
use crate::artifacts::Artifact;
use crate::monster::Monster;
#[cfg(test)]
use crate::monster::Rat;
use crate::position::Position;
pub const LEVEL_WIDTH: usize = 50;
@@ -23,7 +26,7 @@ pub struct Level {
pub(crate) level: usize,
pub(crate) structure: [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) discovered: [[bool; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) monsters: Vec<Monster>,
pub(crate) monsters: Vec<Box<dyn Monster>>,
pub(crate) artifacts: Vec<Box<dyn Artifact>>,
/// the position of the start in the level (either stair up or start point)
pub(crate) start: (usize, usize),
@@ -50,7 +53,7 @@ impl Level {
end: (0, 0),
}
}
pub fn get_element(&mut self, x: i16, y: i16) -> (Option<StructureElement>, Option<&mut Monster>, Option<&mut Box<(dyn Artifact + 'static)>>) {
pub fn get_element(&mut self, x: i16, y: i16) -> (Option<StructureElement>, Option<&mut Box<(dyn Monster + 'static)>>, Option<&mut Box<(dyn Artifact + 'static)>>) {
if x < 0 || y < 0 {
return (None, None, None);
}
@@ -63,7 +66,7 @@ impl Level {
return (Some(StructureElement::Unknown), None, None);
}
let search_pos = &Position::new(self.level, x, y);
let mut res_m: Option<&mut Monster> = None;
let mut res_m: Option<&mut Box<dyn Monster>> = None;
for m in &mut self.monsters {
if m.get_immutable_position() == search_pos {
res_m = Some(m);
@@ -78,7 +81,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> {
pub fn add_monster(&mut self, mut monster: impl Monster + 'static) -> Result<(), String> {
if self.level != monster.get_position().get_level() {
return Err("Wrong Level".to_string());
}
@@ -87,7 +90,7 @@ impl Level {
return Err("Position already used".to_string());
}
}
self.monsters.push(monster);
self.monsters.push(Box::new(monster));
Ok(())
}
@@ -190,19 +193,19 @@ fn test_discover_get_element() {
#[test]
fn test_discover_can_add_monster() {
let mut l = Level::new(0);
let mut m = Monster::new(2);
let mut m = Rat::new(2);
m.get_position().set(1, 2, 3);
assert_eq!(l.add_monster(m), Err("Wrong Level".to_string()));
let mut m = Monster::new(2);
let mut m = Rat::new(2);
m.get_position().set(0, 2, 3);
assert_eq!(l.add_monster(m), Ok(()));
let mut m = Monster::new(2);
let mut m = Rat::new(2);
m.get_position().set(0, 2, 3);
assert_eq!(l.add_monster(m), Err("Position already used".to_string()));
let mut m = Monster::new(2);
let mut m = Rat::new(2);
m.get_position().set(0, 2, 4);
assert_eq!(l.add_monster(m), Ok(()));
}
@@ -231,7 +234,7 @@ 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 = Monster::new(23);
let mut m = Rat::new(23);
m.get_position().set(0, 10, 10);
assert_eq!(l.add_monster(m), Ok(()));
@@ -267,7 +270,7 @@ fn test_discover_get_monster_can_move() {
let p = Position::new(0, 10, 10);
l.discover(&p);
let mut m = Monster::new(23);
let mut m = Rat::new(23);
m.get_position().set(0, 10, 10);
l.add_monster(m).expect("Panic because of");