extract has_position trait

This commit is contained in:
Joachim Lusiardi 2023-12-25 13:47:15 +01:00
parent bb8a24aa91
commit c0d51f501f
7 changed files with 34 additions and 18 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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
}
}
)+)
}

View File

@ -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 {

View File

@ -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,