From 9f95c253433e84bff929e5f001a919cb84110003 Mon Sep 17 00:00:00 2001 From: Guillaume Pasquet Date: Tue, 12 Nov 2019 10:35:27 +0100 Subject: up --- src/main.rs | 24 ++++++++-------- src/world.rs | 91 +++++++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 71 insertions(+), 44 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7da68ed..776e224 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,10 +11,7 @@ mod world; use character::Player; use computer::Enemy; use pancurses::{Window, initscr, endwin}; -use rand::Rng; -use std::io; -use std::convert::TryFrom; -use world::{World, GameWorld, TileType}; +use world::{Dungeon, Level, Generable, TileType}; fn tile_to_str(tile: &TileType) -> &str { match tile { @@ -31,8 +28,8 @@ fn draw_block(window: &Window, block: &TileType) { window.printw(tile_to_str(block)); } -fn render_world(window: &Window, world: &World) { - let grid = world.to_tilegrid().unwrap(); +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() { @@ -42,8 +39,8 @@ fn render_world(window: &Window, world: &World) { } } -fn debug_world(world: &World) { - let grid = world.to_tilegrid().unwrap(); +fn debug_level(level: &Level) { + let grid = level.to_tilegrid().unwrap(); for line in grid.raw_data().iter() { for block in line.iter() { @@ -54,14 +51,17 @@ fn debug_world(world: &World) { } fn main() { - let mut world = World::new(80, 24); - world.generate(); + let mut level = 0; + let mut dungeon = Dungeon::new(80, 24, 5); + dungeon.generate(); - debug_world(&world); + for l in dungeon.levels { + debug_level(&l); + } // let window = initscr(); - // render_world(&window, &world); + // render_dungeon(&window, &world); // window.refresh(); diff --git a/src/world.rs b/src/world.rs index 69f3897..f2f3470 100644 --- a/src/world.rs +++ b/src/world.rs @@ -181,19 +181,22 @@ impl<'a> TileGrid { } } -pub struct World { +pub struct Level { xsize: usize, ysize: usize, rooms: Vec, corridors: Vec } -pub trait GameWorld { - fn new(xsize: usize, ysize: usize) -> Self; +pub struct Dungeon { + xsize: usize, + ysize: usize, + depth: usize, + pub levels: Vec +} +pub trait Generable { fn generate(&mut self); - - fn to_tilegrid(&self) -> Result; } fn hor_dist(point1: Point, point2: Point) -> f32 { @@ -213,7 +216,54 @@ fn distance(point1: Point, point2: Point) -> f32 { ).sqrt() } -impl World { +impl Dungeon { + pub fn new(xsize: usize, ysize: usize, depth: usize) -> Dungeon { + Dungeon { + xsize, + ysize, + depth, + levels: vec![] + } + } +} + +impl Generable for Dungeon { + fn generate(&mut self) { + for _ in 0..self.depth { + let mut level = Level::new(self.xsize, self.ysize, None); + level.generate(); + self.levels.push(level); + } + } +} + +impl Level { + /// Creates a new level of horizontal size `xsize` and vertical size `ysize`. + /// If start is Some then a room will be created at that point to link + /// with an upper room. + pub fn new(xsize: usize, ysize: usize, start: Option) -> Level { + Level { + xsize, + ysize, + rooms: Vec::new(), + corridors: Vec::new() + } + } + + pub fn to_tilegrid(&self) -> Result { + let mut grid = TileGrid::new(self.xsize, self.ysize); + + for room in &self.rooms { + room.tile(&mut grid)?; + } + + for corridor in &self.corridors { + corridor.tile(&mut grid)?; + } + + Ok(grid) + } + fn overlaps(&self, start: Point, width: usize, height: usize, padding: usize) -> bool { for room in &self.rooms { if room.start.0 < start.0 + width + padding && @@ -262,16 +312,7 @@ impl World { } } -impl GameWorld for World { - fn new(xsize: usize, ysize: usize) -> World { - World { - xsize, - ysize, - rooms: Vec::new(), - corridors: Vec::new() - } - } - +impl Generable for Level { fn generate(&mut self) { let mut rng = rand::thread_rng(); let room_number = rng.gen_range(3, 5); @@ -317,20 +358,6 @@ impl GameWorld for World { )); } } - - fn to_tilegrid(&self) -> Result { - let mut grid = TileGrid::new(self.xsize, self.ysize); - - for room in &self.rooms { - room.tile(&mut grid)?; - } - - for corridor in &self.corridors { - corridor.tile(&mut grid)?; - } - - Ok(grid) - } } #[cfg(test)] @@ -339,7 +366,7 @@ mod tests { #[test] fn test_generates_world() { - let mut world = World::new(128, 128); - world.generate(); + let mut level = Level::new(128, 128, None); + level.generate(); } } -- cgit v1.2.3