Compare commits
8 Commits
make_monst
...
include_gi
| Author | SHA1 | Date | |
|---|---|---|---|
| 85496e3200 | |||
| a69e89c806 | |||
| b3d64f7438 | |||
| b88bc67c50 | |||
| 2098bedabe | |||
| c0d51f501f | |||
| bb8a24aa91 | |||
| 67faae44b1 |
7
build.rs
Normal file
7
build.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
use std::process::Command;
|
||||
|
||||
fn main() {
|
||||
let output = Command::new("git").args(&["rev-parse", "--short", "HEAD"]).output().unwrap();
|
||||
let git_hash = String::from_utf8(output.stdout).unwrap();
|
||||
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
|
||||
}
|
||||
@@ -68,9 +68,12 @@ impl Artifact for Potion {
|
||||
}
|
||||
fn get_immutable_position(&self) -> &Position { &self.position }
|
||||
fn collect(&mut self, player: &mut Player) {
|
||||
// only consume potion of the player can gain at least one health point
|
||||
if !player.is_healthy() {
|
||||
player.change_life(self.health.try_into().unwrap());
|
||||
self.health = 0;
|
||||
}
|
||||
}
|
||||
|
||||
fn was_collected(&self) -> bool {
|
||||
self.health == 0
|
||||
|
||||
23
src/game.rs
23
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;
|
||||
|
||||
@@ -43,8 +43,6 @@ impl Game {
|
||||
g
|
||||
}
|
||||
|
||||
/// returns true if the player is dead (life <= 0)
|
||||
fn player_is_dead(&self) -> bool { self.get_player().get_life() <= 0 }
|
||||
/// returns true if the player is standing on the End element
|
||||
fn player_reached_goal(&mut self) -> bool {
|
||||
match self.next_element(0, 0) {
|
||||
@@ -60,7 +58,7 @@ impl Game {
|
||||
}
|
||||
/// returns the state of the game (depending on player's life and position)
|
||||
pub fn get_game_state(&mut self) -> GameState {
|
||||
if self.player_is_dead() {
|
||||
if self.player.is_dead() {
|
||||
return GameState::LOST;
|
||||
}
|
||||
if self.player_reached_goal() {
|
||||
@@ -80,19 +78,8 @@ impl Game {
|
||||
/// limitation as walls.
|
||||
fn can_move(&mut self, dx: i16, dy: i16) -> bool {
|
||||
let player_pos = &self.player.get_position();
|
||||
let new_x: i16 = player_pos.get_x() as i16 + dx;
|
||||
let new_y: i16 = player_pos.get_y() as i16 + dy;
|
||||
let level = &mut self.levels[player_pos.get_level()];
|
||||
match level.get_element(new_x, new_y) {
|
||||
(None, _, _) => { return false; }
|
||||
(Some(t), _, _) => {
|
||||
match t {
|
||||
StructureElement::Wall => { return false; }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
};
|
||||
true
|
||||
level.can_player_move(&self.player, dx, dy)
|
||||
}
|
||||
/// returns the position (as tuple) of the next level's start point.
|
||||
fn next_start(&self) -> (usize, usize, usize) {
|
||||
@@ -190,11 +177,11 @@ impl Game {
|
||||
}
|
||||
}
|
||||
/// updates the player's current level. This will remove collected artifacts and dead monsters.
|
||||
pub fn update_level(&mut self) {
|
||||
pub fn update_level(&mut self, ticks: u128) {
|
||||
let player_pos = &self.player.get_immutable_position();
|
||||
let player_level = player_pos.get_level();
|
||||
let level = &mut self.levels[player_level];
|
||||
level.update();
|
||||
level.update(ticks, &mut self.player);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
178
src/level.rs
178
src/level.rs
@@ -1,11 +1,16 @@
|
||||
use std::cmp::{max, min};
|
||||
|
||||
use rand::Rng;
|
||||
use rand::rngs::ThreadRng;
|
||||
|
||||
#[cfg(test)]
|
||||
use crate::artifacts::{Chest, Potion};
|
||||
use crate::artifacts::Artifact;
|
||||
use crate::monster::Monster;
|
||||
#[cfg(test)]
|
||||
use crate::monster::Rat;
|
||||
use crate::monster::{Orc, Rat};
|
||||
use crate::monster::Monster;
|
||||
use crate::player::Player;
|
||||
use crate::position::HasPosition;
|
||||
use crate::position::Position;
|
||||
|
||||
pub const LEVEL_WIDTH: usize = 50;
|
||||
@@ -32,27 +37,10 @@ pub struct Level {
|
||||
pub(crate) start: (usize, usize),
|
||||
/// the position of the end in the level (either stair down or end point)
|
||||
pub(crate) end: (usize, usize),
|
||||
pub(crate) rng: ThreadRng,
|
||||
}
|
||||
|
||||
impl Level {
|
||||
#[cfg(test)]
|
||||
pub fn new(level: usize) -> Level {
|
||||
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;
|
||||
}
|
||||
}
|
||||
Level {
|
||||
level,
|
||||
structure: s,
|
||||
discovered: [[false; LEVEL_HEIGHT]; LEVEL_WIDTH],
|
||||
monsters: Vec::with_capacity(10),
|
||||
artifacts: Vec::with_capacity(10),
|
||||
start: (0, 0),
|
||||
end: (0, 0),
|
||||
}
|
||||
}
|
||||
pub fn get_element(&mut self, x: i16, y: i16) -> (Option<StructureElement>, Option<&mut Box<(dyn Monster + 'static)>>, Option<&mut Box<(dyn Artifact + 'static)>>) {
|
||||
if x < 0 || y < 0 {
|
||||
return (None, None, None);
|
||||
@@ -80,33 +68,6 @@ impl Level {
|
||||
}
|
||||
(Some(self.structure[x][y]), res_m, res_a)
|
||||
}
|
||||
#[cfg(test)]
|
||||
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());
|
||||
}
|
||||
for m in &mut self.monsters {
|
||||
if m.get_position() == monster.get_position() {
|
||||
return Err("Position already used".to_string());
|
||||
}
|
||||
}
|
||||
self.monsters.push(Box::new(monster));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn add_artifact(&mut self, artifact: impl Artifact + 'static) -> Result<(), String> {
|
||||
if self.level != artifact.get_immutable_position().get_level() {
|
||||
return Err("Wrong Level".to_string());
|
||||
}
|
||||
for a in &mut self.artifacts {
|
||||
if a.get_immutable_position() == artifact.get_immutable_position() {
|
||||
return Err("Position already used".to_string());
|
||||
}
|
||||
}
|
||||
self.artifacts.push(Box::new(artifact));
|
||||
Ok(())
|
||||
}
|
||||
/// discover the area with in the level around the given position
|
||||
pub fn discover(&mut self, pos: &Position) {
|
||||
let x = pos.get_x();
|
||||
@@ -151,14 +112,43 @@ impl Level {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
pub fn update(&mut self, ticks: u128, player: &mut Player) {
|
||||
for (index, a) in &mut self.artifacts.iter().enumerate() {
|
||||
if a.was_collected() {
|
||||
self.artifacts.remove(index);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for index in 0..self.monsters.len() {
|
||||
if self.monsters[index].is_dead() {
|
||||
continue;
|
||||
}
|
||||
if ticks % self.monsters[index].get_ticks_between_steps() != 0 {
|
||||
continue;
|
||||
}
|
||||
loop {
|
||||
// calculate the direction the monster will try to walk
|
||||
let (dx, dy) = match self.rng.gen_range(0..5) {
|
||||
1 => { (1, 0) }
|
||||
2 => { (-1, 0) }
|
||||
3 => { (0, 1) }
|
||||
4 => { (0, -1) }
|
||||
_ => { (0, 0) }
|
||||
};
|
||||
if self.can_monster_move(self.monsters[index].as_ref(), dx, dy) {
|
||||
let (new_x, new_y) = self.monsters[index].get_position().change(dx, dy);
|
||||
if player.get_immutable_position().get_x() == new_x && player.get_immutable_position().get_y() == new_y {
|
||||
self.monsters[index].decrease_life(1);
|
||||
player.change_life(-1);
|
||||
// if the attack did not kill the opponent, back down
|
||||
if !player.is_dead() {
|
||||
self.monsters[index].get_position().change(-dx, -dy);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (index, m) in &mut self.monsters.iter().enumerate() {
|
||||
if m.is_dead() {
|
||||
self.monsters.remove(index);
|
||||
@@ -166,6 +156,73 @@ impl Level {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn can_monster_move(&self, agent: &dyn Monster, dx: i16, dy: i16) -> bool {
|
||||
let agent_pos = agent.get_immutable_position();
|
||||
let new_x: usize = (agent_pos.get_x() as i16 + dx) as usize;
|
||||
let new_y: usize = (agent_pos.get_y() as i16 + dy) as usize;
|
||||
|
||||
if new_x >= LEVEL_WIDTH || new_y >= LEVEL_HEIGHT {
|
||||
return false;
|
||||
}
|
||||
|
||||
for index in 0..self.monsters.len() {
|
||||
let pos = self.monsters[index].get_immutable_position();
|
||||
if pos.get_x() == new_x && pos.get_y() == new_y { return false; }
|
||||
}
|
||||
self.structure[new_x][new_y] != StructureElement::Wall
|
||||
}
|
||||
pub fn can_player_move(&self, agent: &Player, dx: i16, dy: i16) -> bool {
|
||||
let agent_pos = agent.get_immutable_position();
|
||||
let new_x: usize = (agent_pos.get_x() as i16 + dx) as usize;
|
||||
let new_y: usize = (agent_pos.get_y() as i16 + dy) as usize;
|
||||
self.structure[new_x][new_y] != StructureElement::Wall
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub fn new(level: usize) -> Level {
|
||||
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;
|
||||
}
|
||||
}
|
||||
Level {
|
||||
level,
|
||||
structure: s,
|
||||
discovered: [[false; LEVEL_HEIGHT]; LEVEL_WIDTH],
|
||||
monsters: Vec::with_capacity(10),
|
||||
artifacts: Vec::with_capacity(10),
|
||||
start: (0, 0),
|
||||
end: (0, 0),
|
||||
rng: rand::thread_rng(),
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
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());
|
||||
}
|
||||
for m in &mut self.monsters {
|
||||
if m.get_position() == monster.get_position() {
|
||||
return Err("Position already used".to_string());
|
||||
}
|
||||
}
|
||||
self.monsters.push(Box::new(monster));
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub fn add_artifact(&mut self, artifact: impl Artifact + 'static) -> Result<(), String> {
|
||||
if self.level != artifact.get_immutable_position().get_level() {
|
||||
return Err("Wrong Level".to_string());
|
||||
}
|
||||
for a in &mut self.artifacts {
|
||||
if a.get_immutable_position() == artifact.get_immutable_position() {
|
||||
return Err("Position already used".to_string());
|
||||
}
|
||||
}
|
||||
self.artifacts.push(Box::new(artifact));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -193,20 +250,17 @@ fn test_discover_get_element() {
|
||||
#[test]
|
||||
fn test_discover_can_add_monster() {
|
||||
let mut l = Level::new(0);
|
||||
let mut m = Rat::new(2);
|
||||
m.get_position().set(1, 2, 3);
|
||||
let m = Rat::new_with_position(Position::new(1, 2, 3));
|
||||
assert_eq!(l.add_monster(m), Err("Wrong Level".to_string()));
|
||||
|
||||
let mut m = Rat::new(2);
|
||||
let mut m = Orc::new_with_position(Position::new(0, 2, 3));
|
||||
m.get_position().set(0, 2, 3);
|
||||
assert_eq!(l.add_monster(m), Ok(()));
|
||||
|
||||
let mut m = Rat::new(2);
|
||||
m.get_position().set(0, 2, 3);
|
||||
let m = Rat::new_with_position(Position::new(0, 2, 3));
|
||||
assert_eq!(l.add_monster(m), Err("Position already used".to_string()));
|
||||
|
||||
let mut m = Rat::new(2);
|
||||
m.get_position().set(0, 2, 4);
|
||||
let m = Rat::new_with_position(Position::new(0, 2, 4));
|
||||
assert_eq!(l.add_monster(m), Ok(()));
|
||||
}
|
||||
|
||||
@@ -234,18 +288,17 @@ 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 = Rat::new(23);
|
||||
m.get_position().set(0, 10, 10);
|
||||
let m = Rat::new_with_position(Position::new(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);
|
||||
assert_eq!(m.get_life(), 2);
|
||||
|
||||
m.decrease_life(2);
|
||||
assert_eq!(l.get_element(10, 10).1.unwrap().get_life(), 21);
|
||||
assert_eq!(l.get_element(10, 10).1.unwrap().get_life(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -270,8 +323,7 @@ fn test_discover_get_monster_can_move() {
|
||||
let p = Position::new(0, 10, 10);
|
||||
l.discover(&p);
|
||||
|
||||
let mut m = Rat::new(23);
|
||||
m.get_position().set(0, 10, 10);
|
||||
let m = Rat::new_with_position(Position::new(0, 10, 10));
|
||||
l.add_monster(m).expect("Panic because of");
|
||||
|
||||
let m = l.get_element(10, 10).1.unwrap();
|
||||
@@ -281,5 +333,5 @@ fn test_discover_get_monster_can_move() {
|
||||
assert!(m.is_none());
|
||||
let m = l.get_element(11, 11).1;
|
||||
assert!(m.is_some());
|
||||
assert_eq!(m.unwrap().get_life(), 23);
|
||||
assert_eq!(m.unwrap().get_life(), 2);
|
||||
}
|
||||
|
||||
@@ -344,6 +344,7 @@ impl LevelGenerator {
|
||||
artifacts,
|
||||
start: (start_x, start_y),
|
||||
end: (end_x, end_y),
|
||||
rng: rand::thread_rng(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
38
src/main.rs
38
src/main.rs
@@ -1,5 +1,6 @@
|
||||
use std::io::Result;
|
||||
use std::io::stdout;
|
||||
use std::time::Instant;
|
||||
|
||||
use crossterm::{
|
||||
event::{self, KeyCode, KeyEventKind},
|
||||
@@ -19,6 +20,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;
|
||||
@@ -29,6 +31,9 @@ mod level_generator;
|
||||
mod artifacts;
|
||||
mod monster;
|
||||
|
||||
/// length of a game frame in ms
|
||||
pub const FRAME_LENGTH: u64 = 100;
|
||||
|
||||
//
|
||||
fn main() -> Result<()> {
|
||||
let mut game = Game::new(Player::new(realname().as_str(), 10));
|
||||
@@ -38,11 +43,29 @@ fn main() -> Result<()> {
|
||||
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
|
||||
terminal.clear()?;
|
||||
|
||||
let start_time = Instant::now();
|
||||
let mut ticks = 0;
|
||||
loop {
|
||||
terminal.draw(|frame| {
|
||||
let mut area = frame.size();
|
||||
frame.render_widget(Block::default().style(Style::default().bg(Color::Green)), area);
|
||||
|
||||
// don't draw stuff except an info box if the terminal is too small (less than 80x25)
|
||||
// to prevent the read drawing code from crashing the game.
|
||||
if area.width < 80 || area.height < 25 {
|
||||
let block = Block::default()
|
||||
.title("Info")
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(Color::White))
|
||||
.border_type(BorderType::Rounded)
|
||||
.style(Style::default().bg(Color::Black));
|
||||
let paragraph = Paragraph::new("Terminal needs to be at leas 80x25!")
|
||||
.block(block)
|
||||
.wrap(Wrap { trim: true });
|
||||
frame.render_widget(paragraph, area);
|
||||
return;
|
||||
}
|
||||
|
||||
if area.width > 80 {
|
||||
area.x = (area.width - 80) / 2;
|
||||
area.width = 80;
|
||||
@@ -79,7 +102,7 @@ fn main() -> Result<()> {
|
||||
stats_area,
|
||||
);
|
||||
})?;
|
||||
if event::poll(std::time::Duration::from_millis(16))? {
|
||||
if event::poll(std::time::Duration::from_millis(FRAME_LENGTH))? {
|
||||
if let event::Event::Key(key) = event::read()? {
|
||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
||||
break;
|
||||
@@ -97,14 +120,16 @@ fn main() -> Result<()> {
|
||||
game.move_player(new_pos.0, new_pos.1);
|
||||
}
|
||||
game.player_collects_artifact();
|
||||
game.update_level();
|
||||
}
|
||||
}
|
||||
}
|
||||
game.update_level(ticks);
|
||||
if game.get_game_state() != GameState::RUNNING {
|
||||
break;
|
||||
}
|
||||
ticks += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let playtime = start_time.elapsed();
|
||||
loop {
|
||||
let _ = terminal.draw(|frame| {
|
||||
let mut area = frame.size();
|
||||
@@ -134,6 +159,8 @@ fn main() -> Result<()> {
|
||||
};
|
||||
text += format!("\nYou gained {} experience.", game.get_player().get_experience()).as_str();
|
||||
text += format!("\nYou collected {} gold.", game.get_player().get_gold()).as_str();
|
||||
text += format!("\nYou played {} seconds.", playtime.as_secs()).as_str();
|
||||
text += format!("\nYou played game version '{}'.", env!("GIT_HASH")).as_str();
|
||||
let paragraph = Paragraph::new(text).block(block).wrap(Wrap { trim: true });
|
||||
frame.render_widget(paragraph, area);
|
||||
});
|
||||
@@ -145,7 +172,6 @@ fn main() -> Result<()> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stdout().execute(LeaveAlternateScreen)?;
|
||||
disable_raw_mode()?;
|
||||
Ok(())
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
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;
|
||||
fn get_ticks_between_steps(&self) -> u128;
|
||||
#[cfg(test)]
|
||||
fn get_life(&self) -> usize;
|
||||
}
|
||||
@@ -23,15 +22,18 @@ 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
|
||||
}
|
||||
fn get_ticks_between_steps(&self) -> u128 { self.ticks_between_steps }
|
||||
|
||||
#[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
|
||||
}
|
||||
}
|
||||
)+)
|
||||
}
|
||||
@@ -42,19 +44,10 @@ pub struct Rat {
|
||||
symbol: String,
|
||||
color: Color,
|
||||
experience_gain: usize,
|
||||
ticks_between_steps: u128,
|
||||
}
|
||||
|
||||
impl Rat {
|
||||
#[cfg(test)]
|
||||
pub fn new(life: usize) -> Self {
|
||||
Self {
|
||||
life,
|
||||
position: Position::new(0, 0, 0),
|
||||
symbol: String::from("R"),
|
||||
color: Color::Black,
|
||||
experience_gain: 5,
|
||||
}
|
||||
}
|
||||
pub fn new_with_position(position: Position) -> Self {
|
||||
Self {
|
||||
life: 2,
|
||||
@@ -62,10 +55,9 @@ impl Rat {
|
||||
symbol: String::from("R"),
|
||||
color: Color::Black,
|
||||
experience_gain: 5,
|
||||
ticks_between_steps: 5,
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub fn get_life(&self) -> usize { self.life }
|
||||
}
|
||||
default_monster!(Rat);
|
||||
|
||||
@@ -75,19 +67,10 @@ pub struct Orc {
|
||||
symbol: String,
|
||||
color: Color,
|
||||
experience_gain: usize,
|
||||
ticks_between_steps: u128,
|
||||
}
|
||||
|
||||
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,
|
||||
experience_gain: 10,
|
||||
}
|
||||
}
|
||||
pub fn new_with_position(position: Position) -> Self {
|
||||
Self {
|
||||
life: 4,
|
||||
@@ -95,10 +78,9 @@ impl Orc {
|
||||
symbol: String::from("O"),
|
||||
color: Color::DarkGray,
|
||||
experience_gain: 10,
|
||||
ticks_between_steps: 10,
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub fn get_life(&self) -> usize { self.life }
|
||||
}
|
||||
|
||||
default_monster!(Orc);
|
||||
@@ -106,7 +88,7 @@ default_monster!(Orc);
|
||||
|
||||
#[test]
|
||||
fn monsters_can_move() {
|
||||
let mut m = Rat::new(2);
|
||||
let mut m = Rat::new_with_position(Position::new(0,0,0));
|
||||
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));
|
||||
@@ -119,7 +101,7 @@ fn monsters_can_move() {
|
||||
|
||||
#[test]
|
||||
fn monsters_can_die() {
|
||||
let mut m = Rat::new(2);
|
||||
let mut m = Rat::new_with_position(Position::new(0,0,0));
|
||||
assert_eq!(m.get_life(), 2);
|
||||
assert_eq!(m.is_dead(), false);
|
||||
m.decrease_life(1);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::cmp::{max, min};
|
||||
|
||||
use crate::position::Position;
|
||||
use crate::position::{HasPosition, Position};
|
||||
|
||||
pub struct Player {
|
||||
name: String,
|
||||
@@ -31,15 +31,13 @@ impl Player {
|
||||
pub fn get_life(&self) -> i16 {
|
||||
self.life
|
||||
}
|
||||
/// returns true if the player is dead (life <= 0)
|
||||
pub fn is_dead(&self) -> bool { self.life <= 0 }
|
||||
/// returns true if the player's life is at maximum
|
||||
pub fn is_healthy(&self) -> bool { self.life == self.max_life }
|
||||
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 +50,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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user