From f281a8be7a40ca051ad07ca514c12fb3f433f17b Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 16 Dec 2021 16:23:38 -0500 Subject: [PATCH] Add circular room builder, and finish section 4.16 --- src/map_builders.rs | 2 ++ src/map_builders/bsp_dungeon.rs | 6 ++--- src/map_builders/common.rs | 2 +- src/map_builders/room_draw.rs | 48 +++++++++++++++++++++++++++------ 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/map_builders.rs b/src/map_builders.rs index 822be8b..649fda9 100644 --- a/src/map_builders.rs +++ b/src/map_builders.rs @@ -197,6 +197,8 @@ fn random_room_builder(rng: &mut RandomNumberGenerator, builder: &mut BuilderCha }; builder.with(RoomSorter::new(room_sort)); + builder.with(RoomDrawer::new()); + // Pick a corridor type match rng.roll_dice(1, 2) { 1 => builder.with(DoglegCorridors::new()), diff --git a/src/map_builders/bsp_dungeon.rs b/src/map_builders/bsp_dungeon.rs index d26802f..bed53e5 100644 --- a/src/map_builders/bsp_dungeon.rs +++ b/src/map_builders/bsp_dungeon.rs @@ -1,7 +1,7 @@ use rltk::RandomNumberGenerator; use crate::map_builders::{BuilderMap, InitialMapBuilder}; -use crate::{Map, Rect, TileType}; +use crate::{Rect, TileType}; pub struct BspDungeonBuilder { rects: Vec, @@ -40,7 +40,7 @@ impl BspDungeonBuilder { let rect = self.get_random_rect(rng); let candidate = self.get_random_sub_rect(rect, rng); - if self.is_possible(candidate, &build_data, &rooms) { + if self.is_possible(candidate, build_data, &rooms) { rooms.push(candidate); self.add_subrects(rect); } @@ -104,7 +104,7 @@ impl BspDungeonBuilder { result } - fn is_possible(&self, rect: Rect, build_data: &BuilderMap, rooms: &Vec) -> bool { + fn is_possible(&self, rect: Rect, build_data: &BuilderMap, rooms: &[Rect]) -> bool { let mut expanded = rect; expanded.x1 -= 2; expanded.x2 += 2; diff --git a/src/map_builders/common.rs b/src/map_builders/common.rs index 5f98287..f7f93c4 100644 --- a/src/map_builders/common.rs +++ b/src/map_builders/common.rs @@ -1,6 +1,6 @@ use std::cmp::{max, min}; -use crate::{Map, Rect, TileType}; +use crate::{Map, TileType}; #[derive(PartialEq, Copy, Clone)] #[allow(dead_code)] diff --git a/src/map_builders/room_draw.rs b/src/map_builders/room_draw.rs index f6f3c30..c5498a0 100644 --- a/src/map_builders/room_draw.rs +++ b/src/map_builders/room_draw.rs @@ -1,6 +1,7 @@ +use rltk::RandomNumberGenerator; + use super::{BuilderMap, MetaMapBuilder}; use crate::{Rect, TileType}; -use rltk::RandomNumberGenerator; pub struct RoomDrawer {} @@ -16,7 +17,39 @@ impl RoomDrawer { Box::new(RoomDrawer {}) } - fn build(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + #[allow(dead_code)] + fn rectangle(&mut self, build_data: &mut BuilderMap, room: &Rect) { + for y in room.y1 + 1..=room.y2 { + for x in room.x1 + 1..=room.x2 { + let idx = build_data.map.xy_idx(x, y); + build_data.map.tiles[idx] = TileType::Floor; + } + } + } + + #[allow(dead_code)] + fn circle(&mut self, build_data: &mut BuilderMap, room: &Rect) { + let radius = i32::min(room.x2 - room.x1, room.y2 - room.y1) as f32 / 2.0; + let center = room.center(); + let center_pt = rltk::Point::new(center.0, center.1); + + for y in room.y1..=room.y2 { + for x in room.x1..=room.x2 { + let idx = build_data.map.xy_idx(x, y); + let distance = + rltk::DistanceAlg::Pythagoras.distance2d(center_pt, rltk::Point::new(x, y)); + + if idx > 0 + && idx < ((build_data.map.width * build_data.map.height) - 1) as usize + && distance <= radius + { + build_data.map.tiles[idx] = TileType::Floor; + } + } + } + } + + fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { let rooms: Vec; if let Some(rooms_builder) = &build_data.rooms { rooms = rooms_builder.clone(); @@ -25,12 +58,11 @@ impl RoomDrawer { } for room in rooms.iter() { - for y in room.y1 + 1..=room.y2 { - for x in room.x1 + 1..=room.x2 { - let idx = build_data.map.xy_idx(x, y); - build_data.map.tiles[idx] = TileType::Floor; - } - } + // Choose which shape of room to render + match rng.roll_dice(1, 4) { + 1 => self.circle(build_data, room), + _ => self.rectangle(build_data, room), + }; build_data.take_snapshot(); } }