Monster in Level

This commit is contained in:
Joachim Lusiardi 2023-12-03 09:25:03 +01:00
parent ee1f11cd1f
commit 1ba1ed7de4
3 changed files with 92 additions and 25 deletions

View File

@ -11,8 +11,8 @@ pub struct Game<'game> {
impl Game<'_> { impl Game<'_> {
pub fn new(p: &mut Player) -> Game { pub fn new(p: &mut Player) -> Game {
let mut v: Vec<Level> = Vec::with_capacity(LEVELS); let mut v: Vec<Level> = Vec::with_capacity(LEVELS);
for _ in 0..LEVELS { for d in 0..LEVELS {
let l = Level::new(); let l = Level::new(d);
v.push(l); v.push(l);
} }
Game { Game {

View File

@ -1,4 +1,5 @@
use std::cmp::{max, min}; use std::cmp::{max, min};
use crate::monster::Monster;
use crate::position::Position; use crate::position::Position;
pub const LEVEL_WIDTH: usize = 50; pub const LEVEL_WIDTH: usize = 50;
@ -13,14 +14,15 @@ pub enum StructureElement {
Unknown, Unknown,
} }
#[derive(Debug, PartialEq)]
pub struct Level { pub struct Level {
level: usize,
structure: [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH], structure: [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
discovered: [[bool; LEVEL_HEIGHT]; LEVEL_WIDTH], discovered: [[bool; LEVEL_HEIGHT]; LEVEL_WIDTH],
monsters: Vec<Monster>,
} }
impl Level { impl Level {
pub fn new() -> Level { pub fn new(level: usize) -> Level {
let mut s = [[StructureElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH]; let mut s = [[StructureElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH];
for x in 2..LEVEL_WIDTH - 2 { for x in 2..LEVEL_WIDTH - 2 {
for y in 2..LEVEL_HEIGHT - 2 { for y in 2..LEVEL_HEIGHT - 2 {
@ -28,23 +30,44 @@ impl Level {
} }
} }
Level { Level {
level,
structure: s, structure: s,
discovered: [[false; LEVEL_HEIGHT]; LEVEL_WIDTH], discovered: [[false; LEVEL_HEIGHT]; LEVEL_WIDTH],
monsters: Vec::with_capacity(10),
} }
} }
pub fn get_element(&self, x: i16, y: i16) -> Option<StructureElement> { pub fn get_element(&mut self, x: i16, y: i16) -> (Option<StructureElement>, Option<&mut Monster>) {
if x < 0 || y < 0 { if x < 0 || y < 0 {
return None; return (None, None);
} }
let x = x as usize; let x = x as usize;
let y = y as usize; let y = y as usize;
if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT { if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT {
return None; return (None, None);
} }
if !self.discovered[x][y] { if !self.discovered[x][y] {
return Some(StructureElement::Unknown); return (Some(StructureElement::Unknown), None);
} }
Some(self.structure[x][y]) let mut res_m: Option<&mut Monster> = None;
for m in &mut self.monsters {
if m.get_position() == &mut Position::new(self.level, x, y) {
res_m = Some(m);
}
}
(Some(self.structure[x][y]), res_m)
}
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());
}
for m in &mut self.monsters {
if m.get_position() == monster.get_position() {
return Err("Position already used".to_string());
}
}
self.monsters.push(monster);
Ok(())
} }
/// discover the area with in the level around the given position /// discover the area with in the level around the given position
pub fn discover(&mut self, pos: &Position) { pub fn discover(&mut self, pos: &Position) {
@ -94,22 +117,64 @@ impl Level {
#[test] #[test]
fn test_get_element() { fn test_get_element() {
let l = Level::new(); let mut l = Level::new(0);
assert_eq!(l.get_element(-1, -1), None); assert_eq!(l.get_element(-1, -1).0, None);
assert_eq!(l.get_element(0, 0).unwrap(), StructureElement::Unknown); assert_eq!(l.get_element(0, 0).0.unwrap(), StructureElement::Unknown);
assert_eq!(l.get_element(LEVEL_WIDTH as i16 - 1, LEVEL_HEIGHT as i16 - 1).unwrap(), StructureElement::Unknown); assert_eq!(l.get_element(LEVEL_WIDTH as i16 - 1, LEVEL_HEIGHT as i16 - 1).0.unwrap(), StructureElement::Unknown);
assert_eq!(l.get_element(LEVEL_WIDTH as i16, LEVEL_HEIGHT as i16), None); assert_eq!(l.get_element(LEVEL_WIDTH as i16, LEVEL_HEIGHT as i16).0, None);
} }
#[test] #[test]
fn test_discover_get_element() { fn test_discover_get_element() {
let mut l = Level::new(); let mut l = Level::new(0);
assert_eq!(l.get_element(10, 10).unwrap(), StructureElement::Unknown); assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Unknown);
let p = Position::new(0, 10, 10); let p = Position::new(0, 10, 10);
l.discover(&p); l.discover(&p);
assert_eq!(l.get_element(10, 10).unwrap(), StructureElement::Floor); assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(9, 10).unwrap(), StructureElement::Floor); assert_eq!(l.get_element(9, 10).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(10, 9).unwrap(), StructureElement::Floor); assert_eq!(l.get_element(10, 9).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(11, 10).unwrap(), StructureElement::Floor); assert_eq!(l.get_element(11, 10).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(10, 11).unwrap(), StructureElement::Floor); assert_eq!(l.get_element(10, 11).0.unwrap(), StructureElement::Floor);
}
#[test]
fn test_discover_can_add_monster() {
let mut l = Level::new(0);
let mut m = Monster::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);
m.get_position().set(0, 2, 3);
assert_eq!(l.add_monster(m), Ok(()));
let mut m = Monster::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);
m.get_position().set(0, 2, 4);
assert_eq!(l.add_monster(m), Ok(()));
}
#[test]
fn test_discover_get_monster() {
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).1.is_none());
let mut m = Monster::new(23);
m.get_position().set(0, 10, 10);
assert_eq!(l.add_monster(m), Ok(()));
let elem =l.get_element(10, 10);
assert_eq!(elem.0.unwrap(), StructureElement::Floor);
assert!(elem.1.is_some());
let m = elem.1.unwrap();
assert_eq!(m.get_life(), 23);
m.decrease_life(2);
assert_eq!(l.get_element(10, 10).1.unwrap().get_life(), 21);
} }

View File

@ -23,11 +23,13 @@ impl Position {
self.y = y; self.y = y;
} }
pub fn get_level(&self) -> usize {
self.level
}
pub fn get_x(&self) -> usize { pub fn get_x(&self) -> usize {
self.x self.x
} }
pub fn get_y(&self) -> usize { pub fn get_y(&self) -> usize { self.y }
self.y
}
} }