aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume <g@bitimplosion.com>2019-11-12 14:57:54 +0100
committerGitHub <noreply@github.com>2019-11-12 14:57:54 +0100
commite8e931794052455d93517d35c75bb98b7829e70d (patch)
tree99df93ff7678775a4cb179fbd6a0b52a15ede426
parent8fa3fa881bc3b954e136295fe6cc7022737ae9db (diff)
parent76fc41b3c803ad4db3c19a76d408ab301438aa1e (diff)
Merge pull request #5 from iagogarrido/master
Add state
-rw-r--r--src/entities.rs8
-rw-r--r--src/main.rs73
-rw-r--r--src/state.rs46
-rw-r--r--src/world.rs53
4 files changed, 124 insertions, 56 deletions
diff --git a/src/entities.rs b/src/entities.rs
index 51cefc1..145741c 100644
--- a/src/entities.rs
+++ b/src/entities.rs
@@ -149,8 +149,8 @@ impl Player for Character {
fn stats(&self) -> String {
format!(
- "{} - hp: {} attack: {} dodge: {} luck: {} experience: {}",
- self.class, self.health, self.attack, self.dodge, self.luck, self.xp
+ "{} - hp: {}/{} attack: {} dodge: {} luck: {} experience: {}",
+ self.class, self.health, self.max_health, self.attack, self.dodge, self.luck, self.xp
)
}
}
@@ -160,8 +160,8 @@ mod tests {
use super::*;
fn test_attack() {
- let player: Character = Player::new("".to_string(), "Rogue".to_string(), 1, 4, 1, 4, (0,0));
+ let bob: Character = Enemy::new("Rogue".to_string(), 1, 4, 1, 4, (0, 0));
- assert_eq!(player.attack(), 6);
+ assert_eq!(bob.attack(), 6);
}
}
diff --git a/src/main.rs b/src/main.rs
index f7e7f0f..12e92c9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,67 +4,44 @@ extern crate pancurses;
#[macro_use]
extern crate text_io;
+mod state;
mod entities;
mod world;
mod tiling;
-use entities::{Character, Player, Entity};
-use pancurses::{Window, initscr, endwin, Input, noecho};
-use world::{Dungeon, Level, Generatable};
-use tiling::TileType;
+use entities::Player;
+use pancurses::{
+ initscr,
+ endwin,
+ Input,
+ noecho
+};
+use state::State;
+use world::Dungeon;
-fn tile_to_str(tile: &TileType) -> &str {
- match tile {
- TileType::Floor => ".",
- TileType::Wall => "#",
- TileType::Empty => " ",
- TileType::StairsDown => ">",
- TileType::StairsUp => "<",
- TileType::Character => "@",
- _ => "?"
- }
-}
-
-fn draw_block(window: &Window, block: &TileType) {
- window.printw(tile_to_str(block));
-}
-
-fn render_level(window: &Window, level: &Level) {
- let grid = level.to_tilegrid().unwrap();
-
- for (linenum, line) in grid.raw_data().iter().enumerate() {
- for block in line.iter() {
- draw_block(&window, block);
- }
- window.mv(linenum as i32, 0);
- }
-}
fn main() {
let window = initscr();
- let mut level = 0;
- let mut dungeon = Dungeon::new(
- window.get_max_x() as usize,
- window.get_max_y() as usize - 2,
- 5
+ let mut state = State::new(
+ Player::new(
+ "Kshar".to_string(),
+ "Warrior".to_string(),
+ 30,
+ 10,
+ 10,
+ 20,
+ (0, 0)
+ ),
+ Dungeon::new(window.get_max_x() as usize, window.get_max_y() as usize, 5),
);
- dungeon.generate();
- let start_location = dungeon.levels[0].get_start_point();
+ state.init();
- let mut character: Character = Character::new(
- "Kshar".to_string(),
- "Warror".to_string(),
- 30,
- 10,
- 10,
- 20,
- start_location
- );
- character.place(start_location);
+ // Dump the whole dungeon structure in terminal for debugging
+ state.debug();
- render_level(&window, &dungeon.levels[0]);
+ state.render_level(&window);
window.keypad(true);
noecho();
diff --git a/src/state.rs b/src/state.rs
new file mode 100644
index 0000000..20b49d7
--- /dev/null
+++ b/src/state.rs
@@ -0,0 +1,46 @@
+use pancurses::Window;
+use std::env;
+
+use crate::entities::{Character, Entity};
+use crate::world::{Dungeon, Generatable, Level};
+
+pub struct State {
+ pub character: Character,
+ pub dungeon: Dungeon,
+ pub level: usize,
+}
+
+impl State {
+ pub fn new(
+ character: Character,
+ dungeon: Dungeon,
+ ) -> State {
+ State {
+ character: character,
+ dungeon: dungeon,
+ level: 0,
+ }
+ }
+
+ pub fn init(&mut self) {
+ self.dungeon.generate();
+ self.character.place(self.current_level().get_start_point());
+ }
+
+ pub fn debug(&self) {
+ match env::var("DEBUG") {
+ Ok(_) => {
+ self.dungeon.debug_levels();
+ },
+ Err(_) => ()
+ };
+ }
+
+ pub fn render_level(&self, window: &Window) {
+ self.current_level().render(window);
+ }
+
+ fn current_level(&self) -> &Level {
+ &self.dungeon.levels[self.level]
+ }
+} \ No newline at end of file
diff --git a/src/world.rs b/src/world.rs
index abaffa1..bf8b498 100644
--- a/src/world.rs
+++ b/src/world.rs
@@ -1,7 +1,10 @@
use rand::Rng;
+use pancurses::{Window};
use crate::entities::{Entity};
use crate::tiling::{TileGrid, Tileable, TileType};
+pub type Point = (usize, usize);
+
pub enum Direction {
North,
South,
@@ -9,8 +12,6 @@ pub enum Direction {
West
}
-pub type Point = (usize, usize);
-
enum CorridorType {
Horizontal,
Vertical
@@ -53,7 +54,7 @@ impl Room {
start,
width,
height,
- center: (start.0 + width / 2, start.1 + height / 2),
+ center: (start.0 + (width as f32 / 2.0) as usize, start.1 + (height as f32 / 2.0) as usize),
edges: [
RoomEdge::new(start, (start.0 + width, start.1), UP),
RoomEdge::new(start, (start.0, start.1 + height), LEFT),
@@ -186,6 +187,39 @@ impl Dungeon {
levels: vec![]
}
}
+
+ pub fn debug_levels(&self) {
+ for l in &self.levels {
+ Dungeon::debug_level(l);
+ }
+ }
+
+ fn draw_block(window: &Window, block: &TileType) {
+ window.printw(Dungeon::tile_to_str(block));
+ }
+
+ fn debug_level(level: &Level) {
+ let grid = level.to_tilegrid().unwrap();
+
+ for line in grid.raw_data().iter() {
+ for block in line.iter() {
+ print!("{}", Dungeon::tile_to_str(block));
+ }
+ print!("\n");
+ }
+ }
+
+ fn tile_to_str(tile: &TileType) -> &str {
+ match tile {
+ TileType::Floor => ".",
+ TileType::Wall => "#",
+ TileType::Empty => " ",
+ TileType::StairsDown => ">",
+ TileType::StairsUp => "<",
+ TileType::Player => "@",
+ _ => "?"
+ }
+ }
}
impl Generatable for Dungeon {
@@ -223,7 +257,7 @@ impl Level {
}
}
- pub fn to_tilegrid(&self) -> Result<TileGrid, String> {
+ fn to_tilegrid(&self) -> Result<TileGrid, String> {
let mut grid = TileGrid::new(self.xsize, self.ysize);
for room in &self.rooms {
@@ -247,6 +281,17 @@ impl Level {
return (0,0)
}
+ pub fn render(&self, window: &Window) {
+ let grid = self.to_tilegrid().unwrap();
+
+ for (linenum, line) in grid.raw_data().iter().enumerate() {
+ for block in line.iter() {
+ Dungeon::draw_block(&window, &block);
+ }
+ window.mv(linenum as i32, 0);
+ }
+ }
+
pub fn get_entrance(&self) -> Point {
self.entrance
}