Create basic room drawer

This commit is contained in:
Timothy Warren 2021-12-16 16:07:33 -05:00
parent 2ef1f3e1c8
commit d7b617474f
5 changed files with 51 additions and 23 deletions

View File

@ -13,6 +13,7 @@ mod room_based_spawner;
mod room_based_stairs;
mod room_based_starting_position;
mod room_corner_rounding;
mod room_draw;
mod room_exploder;
mod room_sorter;
mod rooms_corridors_bsp;
@ -37,6 +38,7 @@ use room_based_spawner::RoomBasedSpawner;
use room_based_stairs::RoomBasedStairs;
use room_based_starting_position::RoomBasedStartingPosition;
use room_corner_rounding::RoomCornerRounder;
use room_draw::RoomDrawer;
use room_exploder::RoomExploder;
use room_sorter::{RoomSort, RoomSorter};
use rooms_corridors_bsp::BspCorridors;

View File

@ -1,6 +1,5 @@
use rltk::RandomNumberGenerator;
use crate::map_builders::common::apply_room_to_map;
use crate::map_builders::{BuilderMap, InitialMapBuilder};
use crate::{Map, Rect, TileType};
@ -41,11 +40,9 @@ 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.map) {
apply_room_to_map(&mut build_data.map, &candidate);
if self.is_possible(candidate, &build_data, &rooms) {
rooms.push(candidate);
self.add_subrects(rect);
build_data.take_snapshot();
}
n_rooms += 1;
@ -107,7 +104,7 @@ impl BspDungeonBuilder {
result
}
fn is_possible(&self, rect: Rect, map: &Map) -> bool {
fn is_possible(&self, rect: Rect, build_data: &BuilderMap, rooms: &Vec<Rect>) -> bool {
let mut expanded = rect;
expanded.x1 -= 2;
expanded.x2 += 2;
@ -116,12 +113,18 @@ impl BspDungeonBuilder {
let mut can_build = true;
for y in expanded.y1..=expanded.y2 {
for x in expanded.x1..=expanded.x2 {
if x > map.width - 2 {
for r in rooms.iter() {
if r.intersect(&rect) {
can_build = false;
}
if y > map.height - 2 {
}
for y in expanded.y1..=expanded.y2 {
for x in expanded.x1..=expanded.x2 {
if x > build_data.map.width - 2 {
can_build = false;
}
if y > build_data.map.height - 2 {
can_build = false;
}
if x < 1 {
@ -132,8 +135,8 @@ impl BspDungeonBuilder {
}
if can_build {
let idx = map.xy_idx(x, y);
if map.tiles[idx] != TileType::Wall {
let idx = build_data.map.xy_idx(x, y);
if build_data.map.tiles[idx] != TileType::Wall {
can_build = false;
}
}

View File

@ -11,15 +11,6 @@ pub enum Symmetry {
Both,
}
pub fn apply_room_to_map(map: &mut Map, room: &Rect) {
for y in room.y1 + 1..=room.y2 {
for x in room.x1 + 1..=room.x2 {
let idx = map.xy_idx(x, y);
map.tiles[idx] = TileType::Floor;
}
}
}
pub fn apply_horizontal_tunnel(map: &mut Map, x1: i32, x2: i32, y: i32) {
for x in min(x1, x2)..=max(x1, x2) {
let idx = map.xy_idx(x, y);

View File

@ -0,0 +1,37 @@
use super::{BuilderMap, MetaMapBuilder};
use crate::{Rect, TileType};
use rltk::RandomNumberGenerator;
pub struct RoomDrawer {}
impl MetaMapBuilder for RoomDrawer {
fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
self.build(rng, build_data);
}
}
impl RoomDrawer {
#[allow(dead_code)]
pub fn new() -> Box<RoomDrawer> {
Box::new(RoomDrawer {})
}
fn build(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
let rooms: Vec<Rect>;
if let Some(rooms_builder) = &build_data.rooms {
rooms = rooms_builder.clone();
} else {
panic!("Room Rounding requires a builder with room structures");
}
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;
}
}
build_data.take_snapshot();
}
}
}

View File

@ -1,6 +1,5 @@
use rltk::RandomNumberGenerator;
use crate::map_builders::common::apply_room_to_map;
use crate::map_builders::{BuilderMap, InitialMapBuilder};
use crate::Rect;
@ -41,11 +40,7 @@ impl SimpleMapBuilder {
}
if ok {
apply_room_to_map(&mut build_data.map, &new_room);
build_data.take_snapshot();
rooms.push(new_room);
build_data.take_snapshot();
}
}