From b454904e25e587267ffdbac494d99835eb7d13ad Mon Sep 17 00:00:00 2001 From: Guillaume Pasquet Date: Sat, 6 Jul 2019 23:47:51 +0100 Subject: Refactored GameWorld and introduced TileSet --- src/main.rs | 14 +++++++------- src/world.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7edc654..618327f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,13 +14,13 @@ use pancurses::{Window, initscr, endwin}; use rand::Rng; use std::io; use std::convert::TryFrom; -use world::{World, GameWorld, BlockType}; +use world::{World, GameWorld, TileType}; -fn draw_block(window: &Window, block: &BlockType) { +fn draw_block(window: &Window, block: &TileType) { let repr = match block { - BlockType::Floor => ".", - BlockType::Wall => "0", - BlockType::Nothing => " " + TileType::Floor => ".", + TileType::Wall => "0", + TileType::Empty => " " }; print!("{}", repr); @@ -29,9 +29,9 @@ fn draw_block(window: &Window, block: &BlockType) { } fn render_world(window: &Window, world: &World) { - let grid = world.get_world(); + let grid = world.to_tilegrid(); - for (linenum, line) in grid.iter().enumerate() { + for (linenum, line) in grid.raw_data().iter().enumerate() { for block in line.iter() { draw_block(&window, block); } diff --git a/src/world.rs b/src/world.rs index b596d05..f0fc9c8 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,3 +1,6 @@ +use rand::Rng; + +#[derive(Clone)] pub enum TileType { Empty, Wall, @@ -108,7 +111,7 @@ pub struct TileGrid { grid: Vec> } -impl TileGrid { +impl<'a> TileGrid { pub fn new(size: usize) -> TileGrid { let mut grid = TileGrid { grid: Vec::with_capacity(size) @@ -133,9 +136,13 @@ impl TileGrid { fn set_empty_tile(&mut self, x: usize, y: usize, tile: TileType) { self.set_tile(x, y, match self.grid[y][x] { TileType::Empty => tile, - _ => self.grid[y][x] + _ => self.grid[y][x].clone() }) } + + pub fn raw_data(&'a self) -> &'a Vec> { + &self.grid + } } pub struct World { @@ -152,7 +159,18 @@ pub trait GameWorld { } impl World { + fn overlaps(&self, start: (usize, usize), width: usize, height: usize) -> bool { + for room in &self.world { + if room.start.0 < start.0 + width && + room.start.0 + room.width > start.0 && + room.start.1 < start.1 + height && + room.start.1 + room.height > start.1 { + return true; + } + } + return false; + } } impl GameWorld for World { @@ -164,14 +182,35 @@ impl GameWorld for World { } fn generate(&mut self) { - self.world.push(Room::new((3, 3), 5, 8)); + let mut rng = rand::thread_rng(); + let room_number = rng.gen_range(3, 5); + + for _ in 0..room_number { + let room_width = rng.gen_range(3, 6); + let room_height = rng.gen_range(3, 6); + + // TODO find a way to write a lambda to generate the start point. + let mut start: (usize, usize) = ( + rng.gen_range(0, self.size - room_width), + rng.gen_range(0, self.size - room_height) + ); + + while self.overlaps(start, room_width, room_height) { + start = ( + rng.gen_range(0, self.size - room_width), + rng.gen_range(0, self.size - room_height) + ); + } + + self.world.push(Room::new((3, 3), room_width, room_height)); + } } fn to_tilegrid(&self) -> TileGrid { let mut grid = TileGrid::new(self.size); - for room in self.world { - room.tile(&mut grid); + for room in &self.world { + room.tile(&mut grid).unwrap(); } grid -- cgit v1.2.3