2023-12-03 09:25:03 +01:00
|
|
|
use std::cmp::{max, min};
|
|
|
|
use crate::monster::Monster;
|
2023-11-27 19:21:10 +01:00
|
|
|
use crate::position::Position;
|
|
|
|
|
|
|
|
pub const LEVEL_WIDTH: usize = 50;
|
|
|
|
pub const LEVEL_HEIGHT: usize = 25;
|
|
|
|
|
2023-11-08 06:52:20 +01:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
2023-11-27 21:24:43 +01:00
|
|
|
pub enum StructureElement {
|
2023-11-08 06:52:20 +01:00
|
|
|
Wall,
|
|
|
|
Floor,
|
|
|
|
StairDown,
|
|
|
|
StairUp,
|
2023-11-27 19:21:10 +01:00
|
|
|
Unknown,
|
2023-11-08 06:52:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Level {
|
2023-12-03 09:25:03 +01:00
|
|
|
level: usize,
|
2023-11-27 19:21:10 +01:00
|
|
|
structure: [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
|
|
|
|
discovered: [[bool; LEVEL_HEIGHT]; LEVEL_WIDTH],
|
2023-12-03 09:25:03 +01:00
|
|
|
monsters: Vec<Monster>,
|
2023-11-08 06:52:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Level {
|
2023-12-03 09:25:03 +01:00
|
|
|
pub fn new(level: usize) -> Level {
|
2023-11-27 19:21:10 +01:00
|
|
|
let mut s = [[StructureElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH];
|
|
|
|
for x in 2..LEVEL_WIDTH - 2 {
|
|
|
|
for y in 2..LEVEL_HEIGHT - 2 {
|
|
|
|
s[x][y] = StructureElement::Floor;
|
|
|
|
}
|
|
|
|
}
|
2023-11-08 06:52:20 +01:00
|
|
|
Level {
|
2023-12-03 09:25:03 +01:00
|
|
|
level,
|
2023-11-27 19:21:10 +01:00
|
|
|
structure: s,
|
|
|
|
discovered: [[false; LEVEL_HEIGHT]; LEVEL_WIDTH],
|
2023-12-03 09:25:03 +01:00
|
|
|
monsters: Vec::with_capacity(10),
|
2023-11-08 06:52:20 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-03 09:25:03 +01:00
|
|
|
pub fn get_element(&mut self, x: i16, y: i16) -> (Option<StructureElement>, Option<&mut Monster>) {
|
2023-11-27 19:21:10 +01:00
|
|
|
if x < 0 || y < 0 {
|
2023-12-03 09:25:03 +01:00
|
|
|
return (None, None);
|
2023-11-27 19:21:10 +01:00
|
|
|
}
|
|
|
|
let x = x as usize;
|
|
|
|
let y = y as usize;
|
|
|
|
if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT {
|
2023-12-03 09:25:03 +01:00
|
|
|
return (None, None);
|
2023-11-08 06:52:20 +01:00
|
|
|
}
|
2023-11-27 19:21:10 +01:00
|
|
|
if !self.discovered[x][y] {
|
2023-12-03 09:25:03 +01:00
|
|
|
return (Some(StructureElement::Unknown), None);
|
2023-11-27 19:21:10 +01:00
|
|
|
}
|
2023-12-03 09:25:03 +01:00
|
|
|
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(())
|
2023-11-08 06:52:20 +01:00
|
|
|
}
|
2023-11-27 19:21:10 +01:00
|
|
|
/// discover the area with in the level around the given position
|
|
|
|
pub fn discover(&mut self, pos: &Position) {
|
|
|
|
let x = pos.get_x();
|
|
|
|
let y = pos.get_y();
|
|
|
|
|
|
|
|
// uncover fields directly next to the player
|
|
|
|
for x_r in max(x - 1, 0)..=min(x + 1, LEVEL_WIDTH - 1) {
|
|
|
|
for y_r in max(y - 1, 0)..=min(y + 1, LEVEL_HEIGHT - 1) {
|
|
|
|
self.discovered[x_r][y_r] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uncover fields up to 2 fields right of the player
|
|
|
|
for x_r in x..=min(x + 2, LEVEL_WIDTH - 1) {
|
|
|
|
self.discovered[x_r][y] = true;
|
|
|
|
if self.structure[x_r][y] == StructureElement::Wall {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uncover fields up to 2 fields below of the player
|
|
|
|
for y_r in y..=min(y + 2, LEVEL_HEIGHT - 1) {
|
|
|
|
self.discovered[x][y_r] = true;
|
|
|
|
if self.structure[x][y_r] == StructureElement::Wall {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uncover fields up to 2 fields left of the player
|
|
|
|
for x_r in (max(x, 2)..x + 2).rev() {
|
|
|
|
self.discovered[x_r - 2][y] = true;
|
|
|
|
if self.structure[x_r - 2][y] == StructureElement::Wall {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uncover fields up to 2 fields above of the player
|
|
|
|
for y_r in (max(y, 2)..y + 2).rev() {
|
|
|
|
self.discovered[x][y_r - 2] = true;
|
|
|
|
if self.structure[x][y_r - 2] == StructureElement::Wall {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-08 06:52:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_element() {
|
2023-12-03 09:25:03 +01:00
|
|
|
let mut l = Level::new(0);
|
|
|
|
assert_eq!(l.get_element(-1, -1).0, None);
|
|
|
|
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).0.unwrap(), StructureElement::Unknown);
|
|
|
|
assert_eq!(l.get_element(LEVEL_WIDTH as i16, LEVEL_HEIGHT as i16).0, None);
|
2023-11-27 19:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_discover_get_element() {
|
2023-12-03 09:25:03 +01:00
|
|
|
let mut l = Level::new(0);
|
|
|
|
assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Unknown);
|
2023-11-27 19:21:10 +01:00
|
|
|
let p = Position::new(0, 10, 10);
|
|
|
|
l.discover(&p);
|
2023-12-03 09:25:03 +01:00
|
|
|
assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Floor);
|
|
|
|
assert_eq!(l.get_element(9, 10).0.unwrap(), StructureElement::Floor);
|
|
|
|
assert_eq!(l.get_element(10, 9).0.unwrap(), StructureElement::Floor);
|
|
|
|
assert_eq!(l.get_element(11, 10).0.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);
|
2023-11-08 06:52:20 +01:00
|
|
|
}
|