Add circular room builder, and finish section 4.16

This commit is contained in:
Timothy Warren 2021-12-16 16:23:38 -05:00
parent d7b617474f
commit f281a8be7a
4 changed files with 46 additions and 12 deletions

View File

@ -197,6 +197,8 @@ fn random_room_builder(rng: &mut RandomNumberGenerator, builder: &mut BuilderCha
}; };
builder.with(RoomSorter::new(room_sort)); builder.with(RoomSorter::new(room_sort));
builder.with(RoomDrawer::new());
// Pick a corridor type // Pick a corridor type
match rng.roll_dice(1, 2) { match rng.roll_dice(1, 2) {
1 => builder.with(DoglegCorridors::new()), 1 => builder.with(DoglegCorridors::new()),

View File

@ -1,7 +1,7 @@
use rltk::RandomNumberGenerator; use rltk::RandomNumberGenerator;
use crate::map_builders::{BuilderMap, InitialMapBuilder}; use crate::map_builders::{BuilderMap, InitialMapBuilder};
use crate::{Map, Rect, TileType}; use crate::{Rect, TileType};
pub struct BspDungeonBuilder { pub struct BspDungeonBuilder {
rects: Vec<Rect>, rects: Vec<Rect>,
@ -40,7 +40,7 @@ impl BspDungeonBuilder {
let rect = self.get_random_rect(rng); let rect = self.get_random_rect(rng);
let candidate = self.get_random_sub_rect(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); rooms.push(candidate);
self.add_subrects(rect); self.add_subrects(rect);
} }
@ -104,7 +104,7 @@ impl BspDungeonBuilder {
result result
} }
fn is_possible(&self, rect: Rect, build_data: &BuilderMap, rooms: &Vec<Rect>) -> bool { fn is_possible(&self, rect: Rect, build_data: &BuilderMap, rooms: &[Rect]) -> bool {
let mut expanded = rect; let mut expanded = rect;
expanded.x1 -= 2; expanded.x1 -= 2;
expanded.x2 += 2; expanded.x2 += 2;

View File

@ -1,6 +1,6 @@
use std::cmp::{max, min}; use std::cmp::{max, min};
use crate::{Map, Rect, TileType}; use crate::{Map, TileType};
#[derive(PartialEq, Copy, Clone)] #[derive(PartialEq, Copy, Clone)]
#[allow(dead_code)] #[allow(dead_code)]

View File

@ -1,6 +1,7 @@
use rltk::RandomNumberGenerator;
use super::{BuilderMap, MetaMapBuilder}; use super::{BuilderMap, MetaMapBuilder};
use crate::{Rect, TileType}; use crate::{Rect, TileType};
use rltk::RandomNumberGenerator;
pub struct RoomDrawer {} pub struct RoomDrawer {}
@ -16,7 +17,39 @@ impl RoomDrawer {
Box::new(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<Rect>; let rooms: Vec<Rect>;
if let Some(rooms_builder) = &build_data.rooms { if let Some(rooms_builder) = &build_data.rooms {
rooms = rooms_builder.clone(); rooms = rooms_builder.clone();
@ -25,12 +58,11 @@ impl RoomDrawer {
} }
for room in rooms.iter() { for room in rooms.iter() {
for y in room.y1 + 1..=room.y2 { // Choose which shape of room to render
for x in room.x1 + 1..=room.x2 { match rng.roll_dice(1, 4) {
let idx = build_data.map.xy_idx(x, y); 1 => self.circle(build_data, room),
build_data.map.tiles[idx] = TileType::Floor; _ => self.rectangle(build_data, room),
} };
}
build_data.take_snapshot(); build_data.take_snapshot();
} }
} }