From caec9ec0841bf4e487921da1b894fbdeea584dc5 Mon Sep 17 00:00:00 2001 From: Guillaume Pasquet Date: Sun, 7 Jul 2019 23:37:22 +0100 Subject: Better rendering. --- src/main.rs | 3 ++- src/world.rs | 25 +++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 618327f..ae32474 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,8 @@ use world::{World, GameWorld, TileType}; fn draw_block(window: &Window, block: &TileType) { let repr = match block { TileType::Floor => ".", - TileType::Wall => "0", + TileType::Wall => "█", + TileType::Corridor => "#", TileType::Empty => " " }; diff --git a/src/world.rs b/src/world.rs index f0fc9c8..0ebf519 100644 --- a/src/world.rs +++ b/src/world.rs @@ -4,6 +4,7 @@ use rand::Rng; pub enum TileType { Empty, Wall, + Corridor, Floor, } @@ -38,8 +39,8 @@ impl Tileable for Room { let endx = self.start.0 + self.width; let endy = self.start.1 + self.height; - // Draw the walls - for x in self.start.0..endx { + // Set the walls + for x in self.start.0..(endx + 1) { grid.set_empty_tile(x, self.start.1, TileType::Wall); grid.set_empty_tile(x, endy, TileType::Wall); } @@ -50,8 +51,8 @@ impl Tileable for Room { } // Fill the room - for x in (self.start.0 + 1)..(endx) { - for y in (self.start.1 + 1)..(endy) { + for x in (self.start.0 + 1)..endx { + for y in (self.start.1 + 1)..endy { grid.set_tile(x, y, TileType::Floor); } } @@ -159,12 +160,12 @@ pub trait GameWorld { } impl World { - fn overlaps(&self, start: (usize, usize), width: usize, height: usize) -> bool { + fn overlaps(&self, start: (usize, usize), width: usize, height: usize, padding: 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 { + if room.start.0 < start.0 + width + padding && + room.start.0 + room.width + padding > start.0 && + room.start.1 < start.1 + height + padding && + room.start.1 + room.height + padding > start.1 { return true; } } @@ -180,7 +181,7 @@ impl GameWorld for World { world: Vec::new() } } - + fn generate(&mut self) { let mut rng = rand::thread_rng(); let room_number = rng.gen_range(3, 5); @@ -195,14 +196,14 @@ impl GameWorld for World { rng.gen_range(0, self.size - room_height) ); - while self.overlaps(start, room_width, room_height) { + while self.overlaps(start, room_width, room_height, 2) { 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)); + self.world.push(Room::new(start, room_width, room_height)); } } -- cgit v1.2.3