diff --git a/src/game.rs b/src/game.rs index c95e1b5..44c665f 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,7 +1,7 @@ use crate::level::{Level, StructureElement}; use crate::level_generator::LevelGenerator; use crate::player::Player; -use crate::position::Position; +use crate::position::{HasPosition, Position}; pub const LEVELS: usize = 10; diff --git a/src/level.rs b/src/level.rs index 1f4d606..65e83c5 100644 --- a/src/level.rs +++ b/src/level.rs @@ -6,6 +6,8 @@ use crate::artifacts::Artifact; #[cfg(test)] use crate::monster::{Orc, Rat}; use crate::monster::Monster; +#[cfg(test)] +use crate::position::HasPosition; use crate::position::Position; pub const LEVEL_WIDTH: usize = 50; diff --git a/src/level_widget.rs b/src/level_widget.rs index 9816c2c..5154a71 100644 --- a/src/level_widget.rs +++ b/src/level_widget.rs @@ -5,6 +5,7 @@ use ratatui::widgets::{StatefulWidget, Widget}; use crate::game::Game; use crate::level::StructureElement; +use crate::position::HasPosition; const FG_BROWN: Color = Color::Rgb(186, 74, 0); diff --git a/src/main.rs b/src/main.rs index 1ef39ad..ac8313f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ use crate::game::{Game, GameState}; use crate::level_widget::LevelWidget; // use crate::level_widget::LevelWidget; use crate::player::Player; +use crate::position::HasPosition; mod game; mod player; diff --git a/src/monster.rs b/src/monster.rs index 8894d57..2b2fc7f 100644 --- a/src/monster.rs +++ b/src/monster.rs @@ -1,15 +1,15 @@ use ratatui::prelude::Color; -use crate::position::Position; +use crate::position::{HasPosition, Position}; -pub trait Monster { +pub trait Monster: HasPosition { fn is_dead(&self) -> bool; fn get_representation(&self) -> (&str, Color); fn decrease_life(&mut self, by: usize); - fn get_immutable_position(&self) -> &Position; + // fn get_immutable_position(&self) -> &Position; fn get_experience_gain(&self) -> usize; - #[cfg(test)] - fn get_position(&mut self) -> &mut Position; + // #[cfg(test)] + // fn get_position(&mut self) -> &mut Position; #[cfg(test)] fn get_life(&self) -> usize; } @@ -23,15 +23,16 @@ macro_rules! default_monster { 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_life(&self) -> usize { self.life } + } + impl HasPosition for $t { fn get_position(&mut self) -> &mut Position { &mut self.position } - #[cfg(test)] - fn get_life(&self) -> usize { self.life } + fn get_immutable_position(&self) -> &Position { + &self.position + } } )+) } diff --git a/src/player.rs b/src/player.rs index 1457dac..2efd421 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,6 +1,6 @@ use std::cmp::{max, min}; -use crate::position::Position; +use crate::position::{HasPosition, Position}; pub struct Player { name: String, @@ -34,12 +34,6 @@ impl Player { pub fn get_max_life(&self) -> i16 { self.max_life } - pub fn get_position(&mut self) -> &mut Position { - &mut self.position - } - pub fn get_immutable_position(&self) -> &Position { - &self.position - } /// add the given amount to the players gold stash pub fn retrieve_gold(&mut self, amount: usize) { self.gold += amount } @@ -52,6 +46,15 @@ impl Player { pub fn get_experience(&self) -> usize { self.experience } } +impl HasPosition for Player { + fn get_position(&mut self) -> &mut Position { + &mut self.position + } + fn get_immutable_position(&self) -> &Position { + &self.position + } +} + #[test] fn test_get_name() { let p = Player { diff --git a/src/position.rs b/src/position.rs index ea915e5..67ea8e1 100644 --- a/src/position.rs +++ b/src/position.rs @@ -1,5 +1,13 @@ use std::cmp::max; +/// describes an character (PC or NPC) in the dungeon that has a position. +pub trait HasPosition { + /// returns a mutable position + fn get_position(&mut self) -> &mut Position; + /// returns an immutable position + fn get_immutable_position(&self) -> &Position; +} + #[derive(PartialEq, Debug)] pub struct Position { level: usize,