diff options
author | Luis Ferro <luis.ferro@eggplant.io> | 2019-11-12 15:57:03 +0100 |
---|---|---|
committer | Luis Ferro <luis.ferro@eggplant.io> | 2019-11-12 15:57:03 +0100 |
commit | 44146c74c913a72f79f94280a5daa40f33e6c05b (patch) | |
tree | de900d2fe144ceffa6c6065cc5824a3960005224 | |
parent | fe183a3453a828c077db85cd72d3ce39410f2196 (diff) | |
parent | e8e931794052455d93517d35c75bb98b7829e70d (diff) |
May not work
q!
-rw-r--r-- | src/entities.rs | 15 | ||||
-rw-r--r-- | src/main.rs | 93 | ||||
-rw-r--r-- | src/state.rs | 62 | ||||
-rw-r--r-- | src/tiling.rs | 21 | ||||
-rw-r--r-- | src/world.rs | 37 |
5 files changed, 153 insertions, 75 deletions
diff --git a/src/entities.rs b/src/entities.rs index 57ecb4c..2d3078c 100644 --- a/src/entities.rs +++ b/src/entities.rs @@ -1,6 +1,6 @@ use std::cmp;
-use crate::world::{Point, Direction};
+use crate::world::{Point};
use crate::tiling::TileType;
pub trait Entity {
@@ -30,7 +30,6 @@ pub trait Enemy { attack: i32,
dodge: i32,
luck: i32,
- level: i32,
location: Point
) -> Self;
@@ -75,9 +74,9 @@ impl Enemy for Character { attack: i32,
dodge: i32,
luck: i32,
- level: i32,
location: Point
) -> Character {
+ Character {
name: class.clone(),
class: class.clone(),
max_health: health,
@@ -85,9 +84,11 @@ impl Enemy for Character { attack,
dodge,
luck,
+ level: 0,
xp: 0,
location: location,
tile_type: TileType::Character
+ }
}
fn set_tile_type(&mut self, tile_type: TileType) {
@@ -143,8 +144,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
)
}
}
@@ -154,8 +155,8 @@ mod tests { use super::*;
fn test_attack() {
- let player: Character = Player::new("".to_string(), "Rogue".to_string(), 1, 4, 1, 4, 0, (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 9f5e45b..11cac4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,90 +4,55 @@ 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, // allow 2 lines for game stats - 5 + let mut state = State::new( + Player::new( + "Kshar".to_string(), + "Warrior".to_string(), + 30, + 10, + 10, + 20, + 1, + (0, 0) + ), + Dungeon::new(window.get_max_x() as usize, window.get_max_y() as usize, 5), ); - dungeon.generate(); + state.init(); - let start_location = dungeon.levels[0].get_start_point(); - - let mut character: Character = Character::new( - "Kshar".to_string(), - "Warror".to_string(), - 30, - 10, - 10, - 20, - 0, - start_location - ); - - render_level(&window, &dungeon.levels[0]); + // Dump the whole dungeon structure in terminal for debugging + state.debug(); window.keypad(true); noecho(); - + loop { // update actors - + + state.render_level(&window); // update character - window.mv(window.get_max_y() - 2, 0); - window.clrtoeol(); + state.show_character(&window); - window.refresh(); - - window.addstr(character.stats() + "\n"); - window.addstr(character.info() + "\n"); - - window.mv(character.location.1 as i32,character.location.0 as i32); - window.refresh(); - draw_block(&window, &world::TileType::Character); - window.refresh(); - // get input and execute it match window.getch() { diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..6dad58e --- /dev/null +++ b/src/state.rs @@ -0,0 +1,62 @@ +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); + } + + pub fn show_character(&self, window: &Window) { + self.character.render(window); + + // window.mv(window.get_max_y() - 2, 0); + // window.clrtoeol(); + + // window.refresh(); + + // window.addstr(self.character.info() + "\n"); + + // window.mv(self.character.location.1 as i32,self.character.location.0 as i32); + // window.refresh(); + // draw_block(&window, self.character.tile_type); + // window.refresh(); + } + + fn current_level(&self) -> &Level { + &self.dungeon.levels[self.level] + } +}
\ No newline at end of file diff --git a/src/tiling.rs b/src/tiling.rs index 139af24..3e33de1 100644 --- a/src/tiling.rs +++ b/src/tiling.rs @@ -1,3 +1,8 @@ +extern crate pancurses; + +use pancurses::{Window}; + + pub struct TileGrid { grid: Vec<Vec<TileType>> } @@ -36,6 +41,22 @@ impl<'a> TileGrid { } } +fn tile_to_str(tile: &TileType) -> &str { + match tile { + TileType::Floor => ".", + TileType::Wall => "#", + TileType::Empty => " ", + TileType::StairsDown => ">", + TileType::StairsUp => "<", + TileType::Player => "@", + _ => "?" + } +} + +fn draw_block(window: &Window, block: &TileType) { + window.printw(tile_to_str(block)); +} + pub trait Tileable { fn tile(&self, grid: &mut TileGrid) -> Result<(), String>; } diff --git a/src/world.rs b/src/world.rs index abaffa1..42e4d7c 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,23 @@ impl Dungeon { levels: vec![]
}
}
+
+ pub fn debug_levels(&self) {
+ for l in &self.levels {
+ Dungeon::debug_level(l);
+ }
+ }
+
+ 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");
+ }
+ }
}
impl Generatable for Dungeon {
@@ -223,7 +241,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 +265,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
}
|