Compare commits
12 Commits
monsters_a
...
include_gi
| Author | SHA1 | Date | |
|---|---|---|---|
| 85496e3200 | |||
| a69e89c806 | |||
| b3d64f7438 | |||
| b88bc67c50 | |||
| 2098bedabe | |||
| c0d51f501f | |||
| bb8a24aa91 | |||
| 67faae44b1 | |||
| de5ea76913 | |||
| 6082a740e9 | |||
| db96dc5c8a | |||
| 2a86201c3e |
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);
|
||||||
|
}
|
||||||
@@ -22,9 +22,11 @@ pub struct Chest {
|
|||||||
|
|
||||||
impl Chest {
|
impl Chest {
|
||||||
pub fn new(position: Position) -> Self {
|
pub fn new(position: Position) -> Self {
|
||||||
|
let level = position.get_level();
|
||||||
Self {
|
Self {
|
||||||
position,
|
position,
|
||||||
gold: 10,
|
// TODO maybe randomize this?
|
||||||
|
gold: (level + 1) * 10,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,8 +68,11 @@ impl Artifact for Potion {
|
|||||||
}
|
}
|
||||||
fn get_immutable_position(&self) -> &Position { &self.position }
|
fn get_immutable_position(&self) -> &Position { &self.position }
|
||||||
fn collect(&mut self, player: &mut Player) {
|
fn collect(&mut self, player: &mut Player) {
|
||||||
player.change_life(self.health.try_into().unwrap());
|
// only consume potion of the player can gain at least one health point
|
||||||
self.health = 0;
|
if !player.is_healthy() {
|
||||||
|
player.change_life(self.health.try_into().unwrap());
|
||||||
|
self.health = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn was_collected(&self) -> bool {
|
fn was_collected(&self) -> bool {
|
||||||
|
|||||||
60
src/game.rs
60
src/game.rs
@@ -1,9 +1,21 @@
|
|||||||
use crate::level::{Level, StructureElement};
|
use crate::level::{Level, StructureElement};
|
||||||
use crate::level_generator::LevelGenerator;
|
use crate::level_generator::LevelGenerator;
|
||||||
use crate::player::Player;
|
use crate::player::Player;
|
||||||
use crate::position::Position;
|
use crate::position::{HasPosition, Position};
|
||||||
|
|
||||||
|
pub const LEVELS: usize = 10;
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
/// represents a state of a game
|
||||||
|
pub enum GameState {
|
||||||
|
/// the game is ongoing (neither won or lost)
|
||||||
|
RUNNING,
|
||||||
|
/// the player died
|
||||||
|
LOST,
|
||||||
|
/// the player reached the Ω
|
||||||
|
WON,
|
||||||
|
}
|
||||||
|
|
||||||
pub const LEVELS: usize = 2;
|
|
||||||
|
|
||||||
/// the main structure to hold all information about the ongoing game
|
/// the main structure to hold all information about the ongoing game
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
@@ -31,6 +43,29 @@ impl Game {
|
|||||||
g
|
g
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// returns true if the player is standing on the End element
|
||||||
|
fn player_reached_goal(&mut self) -> bool {
|
||||||
|
match self.next_element(0, 0) {
|
||||||
|
None => {}
|
||||||
|
Some(a) => {
|
||||||
|
match a {
|
||||||
|
StructureElement::End => { return true; }
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
false
|
||||||
|
}
|
||||||
|
/// 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() {
|
||||||
|
return GameState::LOST;
|
||||||
|
}
|
||||||
|
if self.player_reached_goal() {
|
||||||
|
return GameState::WON;
|
||||||
|
}
|
||||||
|
GameState::RUNNING
|
||||||
|
}
|
||||||
pub fn get_player(&self) -> &Player {
|
pub fn get_player(&self) -> &Player {
|
||||||
&self.player
|
&self.player
|
||||||
}
|
}
|
||||||
@@ -43,19 +78,8 @@ impl Game {
|
|||||||
/// limitation as walls.
|
/// limitation as walls.
|
||||||
fn can_move(&mut self, dx: i16, dy: i16) -> bool {
|
fn can_move(&mut self, dx: i16, dy: i16) -> bool {
|
||||||
let player_pos = &self.player.get_position();
|
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()];
|
let level = &mut self.levels[player_pos.get_level()];
|
||||||
match level.get_element(new_x, new_y) {
|
level.can_player_move(&self.player, dx, dy)
|
||||||
(None, _, _) => { return false; }
|
|
||||||
(Some(t), _, _) => {
|
|
||||||
match t {
|
|
||||||
StructureElement::Wall => { return false; }
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
/// returns the position (as tuple) of the next level's start point.
|
/// returns the position (as tuple) of the next level's start point.
|
||||||
fn next_start(&self) -> (usize, usize, usize) {
|
fn next_start(&self) -> (usize, usize, usize) {
|
||||||
@@ -131,6 +155,10 @@ impl Game {
|
|||||||
// TODO fight the monster
|
// TODO fight the monster
|
||||||
self.player.change_life(-1);
|
self.player.change_life(-1);
|
||||||
m.decrease_life(1);
|
m.decrease_life(1);
|
||||||
|
// monster died, player gains experience
|
||||||
|
if m.is_dead() {
|
||||||
|
self.player.gain_experience(m.get_experience_gain());
|
||||||
|
}
|
||||||
return m.is_dead();
|
return m.is_dead();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -149,11 +177,11 @@ impl Game {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// updates the player's current level. This will remove collected artifacts and dead monsters.
|
/// 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_pos = &self.player.get_immutable_position();
|
||||||
let player_level = player_pos.get_level();
|
let player_level = player_pos.get_level();
|
||||||
let level = &mut self.levels[player_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 std::cmp::{max, min};
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
use rand::rngs::ThreadRng;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use crate::artifacts::{Chest, Potion};
|
use crate::artifacts::{Chest, Potion};
|
||||||
use crate::artifacts::Artifact;
|
use crate::artifacts::Artifact;
|
||||||
use crate::monster::Monster;
|
|
||||||
#[cfg(test)]
|
#[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;
|
use crate::position::Position;
|
||||||
|
|
||||||
pub const LEVEL_WIDTH: usize = 50;
|
pub const LEVEL_WIDTH: usize = 50;
|
||||||
@@ -32,27 +37,10 @@ pub struct Level {
|
|||||||
pub(crate) start: (usize, usize),
|
pub(crate) start: (usize, usize),
|
||||||
/// the position of the end in the level (either stair down or end point)
|
/// the position of the end in the level (either stair down or end point)
|
||||||
pub(crate) end: (usize, usize),
|
pub(crate) end: (usize, usize),
|
||||||
|
pub(crate) rng: ThreadRng,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Level {
|
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)>>) {
|
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 {
|
if x < 0 || y < 0 {
|
||||||
return (None, None, None);
|
return (None, None, None);
|
||||||
@@ -80,33 +68,6 @@ impl Level {
|
|||||||
}
|
}
|
||||||
(Some(self.structure[x][y]), res_m, res_a)
|
(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
|
/// discover the area with in the level around the given position
|
||||||
pub fn discover(&mut self, pos: &Position) {
|
pub fn discover(&mut self, pos: &Position) {
|
||||||
let x = pos.get_x();
|
let x = pos.get_x();
|
||||||
@@ -151,14 +112,43 @@ impl Level {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn update(&mut self, ticks: u128, player: &mut Player) {
|
||||||
pub fn update(&mut self) {
|
|
||||||
for (index, a) in &mut self.artifacts.iter().enumerate() {
|
for (index, a) in &mut self.artifacts.iter().enumerate() {
|
||||||
if a.was_collected() {
|
if a.was_collected() {
|
||||||
self.artifacts.remove(index);
|
self.artifacts.remove(index);
|
||||||
break;
|
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() {
|
for (index, m) in &mut self.monsters.iter().enumerate() {
|
||||||
if m.is_dead() {
|
if m.is_dead() {
|
||||||
self.monsters.remove(index);
|
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]
|
#[test]
|
||||||
@@ -193,20 +250,17 @@ fn test_discover_get_element() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_discover_can_add_monster() {
|
fn test_discover_can_add_monster() {
|
||||||
let mut l = Level::new(0);
|
let mut l = Level::new(0);
|
||||||
let mut m = Rat::new(2);
|
let m = Rat::new_with_position(Position::new(1, 2, 3));
|
||||||
m.get_position().set(1, 2, 3);
|
|
||||||
assert_eq!(l.add_monster(m), Err("Wrong Level".to_string()));
|
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);
|
m.get_position().set(0, 2, 3);
|
||||||
assert_eq!(l.add_monster(m), Ok(()));
|
assert_eq!(l.add_monster(m), Ok(()));
|
||||||
|
|
||||||
let mut m = Rat::new(2);
|
let m = Rat::new_with_position(Position::new(0, 2, 3));
|
||||||
m.get_position().set(0, 2, 3);
|
|
||||||
assert_eq!(l.add_monster(m), Err("Position already used".to_string()));
|
assert_eq!(l.add_monster(m), Err("Position already used".to_string()));
|
||||||
|
|
||||||
let mut m = Rat::new(2);
|
let m = Rat::new_with_position(Position::new(0, 2, 4));
|
||||||
m.get_position().set(0, 2, 4);
|
|
||||||
assert_eq!(l.add_monster(m), Ok(()));
|
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_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Floor);
|
||||||
assert!(l.get_element(10, 10).1.is_none());
|
assert!(l.get_element(10, 10).1.is_none());
|
||||||
|
|
||||||
let mut m = Rat::new(23);
|
let m = Rat::new_with_position(Position::new(0, 10, 10));
|
||||||
m.get_position().set(0, 10, 10);
|
|
||||||
assert_eq!(l.add_monster(m), Ok(()));
|
assert_eq!(l.add_monster(m), Ok(()));
|
||||||
|
|
||||||
let elem = l.get_element(10, 10);
|
let elem = l.get_element(10, 10);
|
||||||
assert_eq!(elem.0.unwrap(), StructureElement::Floor);
|
assert_eq!(elem.0.unwrap(), StructureElement::Floor);
|
||||||
assert!(elem.1.is_some());
|
assert!(elem.1.is_some());
|
||||||
let m = elem.1.unwrap();
|
let m = elem.1.unwrap();
|
||||||
assert_eq!(m.get_life(), 23);
|
assert_eq!(m.get_life(), 2);
|
||||||
|
|
||||||
m.decrease_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]
|
#[test]
|
||||||
@@ -270,8 +323,7 @@ fn test_discover_get_monster_can_move() {
|
|||||||
let p = Position::new(0, 10, 10);
|
let p = Position::new(0, 10, 10);
|
||||||
l.discover(&p);
|
l.discover(&p);
|
||||||
|
|
||||||
let mut m = Rat::new(23);
|
let m = Rat::new_with_position(Position::new(0, 10, 10));
|
||||||
m.get_position().set(0, 10, 10);
|
|
||||||
l.add_monster(m).expect("Panic because of");
|
l.add_monster(m).expect("Panic because of");
|
||||||
|
|
||||||
let m = l.get_element(10, 10).1.unwrap();
|
let m = l.get_element(10, 10).1.unwrap();
|
||||||
@@ -281,5 +333,5 @@ fn test_discover_get_monster_can_move() {
|
|||||||
assert!(m.is_none());
|
assert!(m.is_none());
|
||||||
let m = l.get_element(11, 11).1;
|
let m = l.get_element(11, 11).1;
|
||||||
assert!(m.is_some());
|
assert!(m.is_some());
|
||||||
assert_eq!(m.unwrap().get_life(), 23);
|
assert_eq!(m.unwrap().get_life(), 2);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -276,11 +276,11 @@ impl LevelGenerator {
|
|||||||
// TODO randomize enemies here
|
// TODO randomize enemies here
|
||||||
match rng.gen_range(1..=100) {
|
match rng.gen_range(1..=100) {
|
||||||
1..=50 => {
|
1..=50 => {
|
||||||
println!("Orc! {} {} {}", self.level, t_x, t_y);
|
enemies.push(Box::new(Orc::new_with_position(Position::new(self.level, t_x, t_y))));
|
||||||
enemies.push(Box::new(Orc::new_with_position(Position::new(self.level, t_x, t_y)))); }
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("Rat! {} {} {}", self.level, t_x, t_y);
|
enemies.push(Box::new(Rat::new_with_position(Position::new(self.level, t_x, t_y))));
|
||||||
enemies.push(Box::new(Rat::new_with_position(Position::new(self.level, t_x, t_y))));}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -344,6 +344,7 @@ impl LevelGenerator {
|
|||||||
artifacts,
|
artifacts,
|
||||||
start: (start_x, start_y),
|
start: (start_x, start_y),
|
||||||
end: (end_x, end_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::game::Game;
|
||||||
use crate::level::StructureElement;
|
use crate::level::StructureElement;
|
||||||
|
use crate::position::HasPosition;
|
||||||
|
|
||||||
const FG_BROWN: Color = Color::Rgb(186, 74, 0);
|
const FG_BROWN: Color = Color::Rgb(186, 74, 0);
|
||||||
|
|
||||||
|
|||||||
83
src/main.rs
83
src/main.rs
@@ -1,5 +1,6 @@
|
|||||||
use std::io::Result;
|
use std::io::Result;
|
||||||
use std::io::stdout;
|
use std::io::stdout;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
event::{self, KeyCode, KeyEventKind},
|
event::{self, KeyCode, KeyEventKind},
|
||||||
@@ -11,13 +12,15 @@ use ratatui::{
|
|||||||
widgets::Paragraph,
|
widgets::Paragraph,
|
||||||
};
|
};
|
||||||
use ratatui::prelude::*;
|
use ratatui::prelude::*;
|
||||||
use ratatui::widgets::Block;
|
use ratatui::widgets::{Block, Borders, BorderType, Wrap};
|
||||||
|
use ratatui::widgets::block::{Position, Title};
|
||||||
use whoami::realname;
|
use whoami::realname;
|
||||||
|
|
||||||
use crate::game::Game;
|
use crate::game::{Game, GameState};
|
||||||
use crate::level_widget::LevelWidget;
|
use crate::level_widget::LevelWidget;
|
||||||
// use crate::level_widget::LevelWidget;
|
// use crate::level_widget::LevelWidget;
|
||||||
use crate::player::Player;
|
use crate::player::Player;
|
||||||
|
use crate::position::HasPosition;
|
||||||
|
|
||||||
mod game;
|
mod game;
|
||||||
mod player;
|
mod player;
|
||||||
@@ -28,6 +31,9 @@ mod level_generator;
|
|||||||
mod artifacts;
|
mod artifacts;
|
||||||
mod monster;
|
mod monster;
|
||||||
|
|
||||||
|
/// length of a game frame in ms
|
||||||
|
pub const FRAME_LENGTH: u64 = 100;
|
||||||
|
|
||||||
//
|
//
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let mut game = Game::new(Player::new(realname().as_str(), 10));
|
let mut game = Game::new(Player::new(realname().as_str(), 10));
|
||||||
@@ -37,11 +43,29 @@ fn main() -> Result<()> {
|
|||||||
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
|
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
|
||||||
terminal.clear()?;
|
terminal.clear()?;
|
||||||
|
|
||||||
|
let start_time = Instant::now();
|
||||||
|
let mut ticks = 0;
|
||||||
loop {
|
loop {
|
||||||
terminal.draw(|frame| {
|
terminal.draw(|frame| {
|
||||||
let mut area = frame.size();
|
let mut area = frame.size();
|
||||||
frame.render_widget(Block::default().style(Style::default().bg(Color::Green)), area);
|
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 {
|
if area.width > 80 {
|
||||||
area.x = (area.width - 80) / 2;
|
area.x = (area.width - 80) / 2;
|
||||||
area.width = 80;
|
area.width = 80;
|
||||||
@@ -66,10 +90,11 @@ fn main() -> Result<()> {
|
|||||||
height: 25,
|
height: 25,
|
||||||
};
|
};
|
||||||
frame.render_widget(
|
frame.render_widget(
|
||||||
Paragraph::new(format!("{}\nHealth: {}/{}\nGold: {}\nLevel: {}",
|
Paragraph::new(format!("{}\nHealth: {}/{}\nExp: {}\nGold: {}\nLevel: {}",
|
||||||
game.get_player().get_name(),
|
game.get_player().get_name(),
|
||||||
game.get_player().get_life(),
|
game.get_player().get_life(),
|
||||||
game.get_player().get_max_life(),
|
game.get_player().get_max_life(),
|
||||||
|
game.get_player().get_experience(),
|
||||||
game.get_player().get_gold(),
|
game.get_player().get_gold(),
|
||||||
game.get_player().get_immutable_position().get_level()))
|
game.get_player().get_immutable_position().get_level()))
|
||||||
.white()
|
.white()
|
||||||
@@ -77,7 +102,7 @@ fn main() -> Result<()> {
|
|||||||
stats_area,
|
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 let event::Event::Key(key) = event::read()? {
|
||||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
||||||
break;
|
break;
|
||||||
@@ -95,12 +120,58 @@ fn main() -> Result<()> {
|
|||||||
game.move_player(new_pos.0, new_pos.1);
|
game.move_player(new_pos.0, new_pos.1);
|
||||||
}
|
}
|
||||||
game.player_collects_artifact();
|
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();
|
||||||
|
let w = area.width / 2;
|
||||||
|
let h = area.height / 2;
|
||||||
|
area.x += w - 20;
|
||||||
|
area.y += h - 10;
|
||||||
|
area.width = 40;
|
||||||
|
area.height = 20;
|
||||||
|
let block = Block::default()
|
||||||
|
.title("Game ended")
|
||||||
|
.title(Title::from("Press `q` to quit!").position(Position::Bottom))
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_style(Style::default().fg(Color::White))
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.style(Style::default().bg(Color::Black));
|
||||||
|
let mut text = match game.get_game_state() {
|
||||||
|
GameState::RUNNING => {
|
||||||
|
"Quitting is for cowards! You'll better try again!".to_string()
|
||||||
|
}
|
||||||
|
GameState::LOST => {
|
||||||
|
"Sorry, you died in the dungeon. Better luck next time!".to_string()
|
||||||
|
}
|
||||||
|
GameState::WON => {
|
||||||
|
"Congratulation! You mastered your way through the dungeon and won the game.".to_string()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
if event::poll(std::time::Duration::from_millis(16))? {
|
||||||
|
if let event::Event::Key(key) = event::read()? {
|
||||||
|
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stdout().execute(LeaveAlternateScreen)?;
|
stdout().execute(LeaveAlternateScreen)?;
|
||||||
disable_raw_mode()?;
|
disable_raw_mode()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
use ratatui::prelude::Color;
|
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 is_dead(&self) -> bool;
|
||||||
fn get_representation(&self) -> (&str, Color);
|
fn get_representation(&self) -> (&str, Color);
|
||||||
fn decrease_life(&mut self, by: usize);
|
fn decrease_life(&mut self, by: usize);
|
||||||
fn get_immutable_position(&self) -> &Position;
|
// fn get_immutable_position(&self) -> &Position;
|
||||||
#[cfg(test)]
|
fn get_experience_gain(&self) -> usize;
|
||||||
fn get_position(&mut self) -> &mut Position;
|
fn get_ticks_between_steps(&self) -> u128;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
fn get_life(&self) -> usize;
|
fn get_life(&self) -> usize;
|
||||||
}
|
}
|
||||||
@@ -17,19 +17,23 @@ macro_rules! default_monster {
|
|||||||
($($t:ty),+ $(,)?) => ($(
|
($($t:ty),+ $(,)?) => ($(
|
||||||
impl Monster for $t {
|
impl Monster for $t {
|
||||||
fn is_dead(&self) -> bool { self.life <= 0 }
|
fn is_dead(&self) -> bool { self.life <= 0 }
|
||||||
|
fn get_experience_gain(&self) -> usize { self.experience_gain }
|
||||||
fn get_representation(&self) -> (&str, Color) { (&self.symbol, self.color) }
|
fn get_representation(&self) -> (&str, Color) { (&self.symbol, self.color) }
|
||||||
fn decrease_life(&mut self, by: usize) {
|
fn decrease_life(&mut self, by: usize) {
|
||||||
self.life = self.life.saturating_sub(by);
|
self.life = self.life.saturating_sub(by);
|
||||||
}
|
}
|
||||||
fn get_immutable_position(&self) -> &Position {
|
fn get_ticks_between_steps(&self) -> u128 { self.ticks_between_steps }
|
||||||
&self.position
|
|
||||||
}
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
fn get_life(&self) -> usize { self.life }
|
||||||
|
}
|
||||||
|
impl HasPosition for $t {
|
||||||
fn get_position(&mut self) -> &mut Position {
|
fn get_position(&mut self) -> &mut Position {
|
||||||
&mut self.position
|
&mut self.position
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
fn get_immutable_position(&self) -> &Position {
|
||||||
fn get_life(&self) -> usize { self.life }
|
&self.position
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)+)
|
)+)
|
||||||
}
|
}
|
||||||
@@ -39,28 +43,21 @@ pub struct Rat {
|
|||||||
position: Position,
|
position: Position,
|
||||||
symbol: String,
|
symbol: String,
|
||||||
color: Color,
|
color: Color,
|
||||||
|
experience_gain: usize,
|
||||||
|
ticks_between_steps: u128,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rat {
|
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn new_with_position(position: Position) -> Self {
|
pub fn new_with_position(position: Position) -> Self {
|
||||||
Self {
|
Self {
|
||||||
life: 2,
|
life: 2,
|
||||||
position,
|
position,
|
||||||
symbol: String::from("R"),
|
symbol: String::from("R"),
|
||||||
color: Color::Black,
|
color: Color::Black,
|
||||||
|
experience_gain: 5,
|
||||||
|
ticks_between_steps: 5,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
|
||||||
pub fn get_life(&self) -> usize { self.life }
|
|
||||||
}
|
}
|
||||||
default_monster!(Rat);
|
default_monster!(Rat);
|
||||||
|
|
||||||
@@ -69,28 +66,21 @@ pub struct Orc {
|
|||||||
position: Position,
|
position: Position,
|
||||||
symbol: String,
|
symbol: String,
|
||||||
color: Color,
|
color: Color,
|
||||||
|
experience_gain: usize,
|
||||||
|
ticks_between_steps: u128,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Orc {
|
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(position: Position) -> Self {
|
pub fn new_with_position(position: Position) -> Self {
|
||||||
Self {
|
Self {
|
||||||
life: 4,
|
life: 4,
|
||||||
position,
|
position,
|
||||||
symbol: String::from("O"),
|
symbol: String::from("O"),
|
||||||
color: Color::DarkGray,
|
color: Color::DarkGray,
|
||||||
|
experience_gain: 10,
|
||||||
|
ticks_between_steps: 10,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
|
||||||
pub fn get_life(&self) -> usize { self.life }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default_monster!(Orc);
|
default_monster!(Orc);
|
||||||
@@ -98,7 +88,7 @@ default_monster!(Orc);
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn monsters_can_move() {
|
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));
|
assert_eq!(m.get_position(), &Position::new(0, 0, 0));
|
||||||
m.get_position().change(1, 2);
|
m.get_position().change(1, 2);
|
||||||
assert_eq!(m.get_position(), &Position::new(0, 1, 2));
|
assert_eq!(m.get_position(), &Position::new(0, 1, 2));
|
||||||
@@ -111,7 +101,7 @@ fn monsters_can_move() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn monsters_can_die() {
|
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.get_life(), 2);
|
||||||
assert_eq!(m.is_dead(), false);
|
assert_eq!(m.is_dead(), false);
|
||||||
m.decrease_life(1);
|
m.decrease_life(1);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
use crate::position::Position;
|
use crate::position::{HasPosition, Position};
|
||||||
|
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
name: String,
|
name: String,
|
||||||
@@ -8,6 +8,7 @@ pub struct Player {
|
|||||||
life: i16,
|
life: i16,
|
||||||
max_life: i16,
|
max_life: i16,
|
||||||
gold: usize,
|
gold: usize,
|
||||||
|
experience: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Player {
|
impl Player {
|
||||||
@@ -18,6 +19,7 @@ impl Player {
|
|||||||
life: max_life,
|
life: max_life,
|
||||||
max_life,
|
max_life,
|
||||||
gold: 0,
|
gold: 0,
|
||||||
|
experience: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn get_name(&self) -> String {
|
pub fn get_name(&self) -> String {
|
||||||
@@ -29,21 +31,32 @@ impl Player {
|
|||||||
pub fn get_life(&self) -> i16 {
|
pub fn get_life(&self) -> i16 {
|
||||||
self.life
|
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 {
|
pub fn get_max_life(&self) -> i16 {
|
||||||
self.max_life
|
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
|
/// add the given amount to the players gold stash
|
||||||
pub fn retrieve_gold(&mut self, amount: usize) { self.gold += amount }
|
pub fn retrieve_gold(&mut self, amount: usize) { self.gold += amount }
|
||||||
|
|
||||||
/// return the size of the players gold stash
|
/// return the size of the players gold stash
|
||||||
pub fn get_gold(&self) -> usize { self.gold }
|
pub fn get_gold(&self) -> usize { self.gold }
|
||||||
|
|
||||||
|
pub fn gain_experience(&mut self, amount: usize) { self.experience += amount }
|
||||||
|
|
||||||
|
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]
|
#[test]
|
||||||
@@ -54,6 +67,7 @@ fn test_get_name() {
|
|||||||
life: 5,
|
life: 5,
|
||||||
max_life: 10,
|
max_life: 10,
|
||||||
gold: 0,
|
gold: 0,
|
||||||
|
experience: 0,
|
||||||
};
|
};
|
||||||
assert_eq!(p.get_name(), "Teddy Tester");
|
assert_eq!(p.get_name(), "Teddy Tester");
|
||||||
}
|
}
|
||||||
@@ -76,6 +90,7 @@ fn test_change_life() {
|
|||||||
life: 5,
|
life: 5,
|
||||||
max_life: 10,
|
max_life: 10,
|
||||||
gold: 0,
|
gold: 0,
|
||||||
|
experience: 0,
|
||||||
};
|
};
|
||||||
assert_eq!(p.get_life(), 5);
|
assert_eq!(p.get_life(), 5);
|
||||||
p.change_life(-2);
|
p.change_life(-2);
|
||||||
@@ -108,6 +123,7 @@ fn test_max_life() {
|
|||||||
life: 5,
|
life: 5,
|
||||||
max_life: 10,
|
max_life: 10,
|
||||||
gold: 0,
|
gold: 0,
|
||||||
|
experience: 0,
|
||||||
};
|
};
|
||||||
assert_eq!(p.get_max_life(), 10);
|
assert_eq!(p.get_max_life(), 10);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
use std::cmp::max;
|
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)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
level: usize,
|
level: usize,
|
||||||
|
|||||||
Reference in New Issue
Block a user