diff --git a/src/artifacts.rs b/src/artifacts.rs index 3be167f..ed260a2 100644 --- a/src/artifacts.rs +++ b/src/artifacts.rs @@ -1,6 +1,7 @@ use ratatui::style::Color; -use crate::position::Position; + use crate::player::Player; +use crate::position::Position; pub trait Artifact { //! An artifact that can be collected by the player diff --git a/src/level.rs b/src/level.rs index 1225c9f..8e3e6f2 100644 --- a/src/level.rs +++ b/src/level.rs @@ -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, + pub(crate) monsters: Vec>, pub(crate) artifacts: Vec>, /// 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, Option<&mut Monster>, Option<&mut Box<(dyn Artifact + 'static)>>) { + pub fn get_element(&mut self, x: i16, y: i16) -> (Option, 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> = 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"); diff --git a/src/level_generator.rs b/src/level_generator.rs index 7125b67..7d6d9e2 100644 --- a/src/level_generator.rs +++ b/src/level_generator.rs @@ -9,9 +9,9 @@ use rand::prelude::SliceRandom; use rand::Rng; use rand::rngs::ThreadRng; -use crate::level::{Level, StructureElement}; -use crate::monster::Monster; use crate::artifacts::{Artifact, Chest, Potion}; +use crate::level::{Level, StructureElement}; +use crate::monster::{Monster, Orc, Rat}; use crate::position::Position; const ROOMS_VERTICAL: usize = 7; @@ -246,7 +246,7 @@ impl LevelGenerator { let mut rng = rand::thread_rng(); let mut structure = [[StructureElement::Wall; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH]; let mut artifacts: Vec> = Vec::with_capacity(10); - let mut enemies: Vec = Vec::with_capacity(10); + let mut enemies: Vec> = Vec::with_capacity(10); let mut start_x: usize = 0; let mut start_y: usize = 0; let mut end_x: usize = 0; @@ -274,7 +274,7 @@ impl LevelGenerator { let t_x = left + room.offset_x + rng.gen_range(0..room.width); let t_y = top + room.offset_y + rng.gen_range(0..room.height); // TODO randomize enemies here - enemies.push(Monster::new_with_position(2, Position::new(self.level, t_x, t_y))); + enemies.push(Box::new(Orc::new_with_position(2, Position::new(self.level, t_x, t_y)))); } if room.kind == RoomType::End || room.kind == RoomType::StairDown { diff --git a/src/level_widget.rs b/src/level_widget.rs index b8a1bc7..9816c2c 100644 --- a/src/level_widget.rs +++ b/src/level_widget.rs @@ -65,8 +65,9 @@ impl StatefulWidget for LevelWidget { } } } - (_, Some(_), _) => { - self.set_cell(buf, x, y, "M", Color::Red, Color::Gray); + (_, Some(m), _) => { + let (s, c) = m.get_representation(); + self.set_cell(buf, x, y, s, c, Color::Gray); } (_, _, Some(t)) => { let (s, c) = t.get_representation(); diff --git a/src/monster.rs b/src/monster.rs index 326542f..6803199 100644 --- a/src/monster.rs +++ b/src/monster.rs @@ -1,42 +1,104 @@ +use ratatui::prelude::Color; + use crate::position::Position; -pub struct Monster { - life: usize, - position: Position, +pub trait Monster { + fn is_dead(&self) -> bool; + fn get_representation(&self) -> (&str, Color); + fn decrease_life(&mut self, by: usize); + fn get_immutable_position(&self) -> &Position; + #[cfg(test)] + fn get_position(&mut self) -> &mut Position; + #[cfg(test)] + fn get_life(&self) -> usize; } -impl Monster { +macro_rules! default_monster { + ($($t:ty),+ $(,)?) => ($( + impl Monster for $t { + fn is_dead(&self) -> bool { self.life <= 0 } + fn get_representation(&self) -> (&str, Color) { (&self.symbol, self.color) } + fn decrease_life(&mut self, by: usize) { + self.life = self.life.saturating_sub(by); + } + fn get_immutable_position(&self) -> &Position { + &self.position + } + #[cfg(test)] + fn get_position(&mut self) -> &mut Position { + &mut self.position + } + #[cfg(test)] + fn get_life(&self) -> usize { self.life } + } + )+) +} + +pub struct Rat { + life: usize, + position: Position, + symbol: String, + color: Color, +} + +impl Rat { #[cfg(test)] pub fn new(life: usize) -> Self { - Monster { + Self { life, position: Position::new(0, 0, 0), + symbol: String::from("R"), + color: Color::Gray, } } pub fn new_with_position(life: usize, position: Position) -> Self { - Monster { + Self { life, position, + symbol: String::from("R"), + color: Color::Gray, } } #[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); +} +default_monster!(Rat); + +pub struct Orc { + life: usize, + position: Position, + symbol: String, + color: Color, +} + +impl Orc { + #[cfg(test)] + pub fn new(life: usize) -> Self { + Self { + life, + position: Position::new(0, 0, 0), + symbol: String::from("O"), + color: Color::DarkGray, + } + } + pub fn new_with_position(life: usize, position: Position) -> Self { + Self { + life, + position, + symbol: String::from("O"), + color: Color::DarkGray, + } } #[cfg(test)] - pub fn get_position(&mut self) -> &mut Position { - &mut self.position - } - pub fn get_immutable_position(&self) -> &Position { - &self.position - } + pub fn get_life(&self) -> usize { self.life } } +default_monster!(Orc); + + #[test] fn monsters_can_move() { - let mut m = Monster::new(2); + let mut m = Rat::new(2); assert_eq!(m.get_position(), &Position::new(0, 0, 0)); m.get_position().change(1, 2); assert_eq!(m.get_position(), &Position::new(0, 1, 2)); @@ -49,7 +111,7 @@ fn monsters_can_move() { #[test] fn monsters_can_die() { - let mut m = Monster::new(2); + let mut m = Rat::new(2); assert_eq!(m.get_life(), 2); assert_eq!(m.is_dead(), false); m.decrease_life(1); diff --git a/src/player.rs b/src/player.rs index 877f156..2fc038d 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,4 +1,5 @@ use std::cmp::{max, min}; + use crate::position::Position; pub struct Player {