2022-02-04 14:09:48 -05:00
|
|
|
use ::bracket_lib::prelude::*;
|
|
|
|
|
2021-12-16 16:07:33 -05:00
|
|
|
use super::{BuilderMap, MetaMapBuilder};
|
2022-02-03 14:59:35 -05:00
|
|
|
use crate::rng::roll_dice;
|
2021-12-16 16:07:33 -05:00
|
|
|
use crate::{Rect, TileType};
|
|
|
|
|
|
|
|
pub struct RoomDrawer {}
|
|
|
|
|
|
|
|
impl MetaMapBuilder for RoomDrawer {
|
2022-02-03 14:59:35 -05:00
|
|
|
fn build_map(&mut self, build_data: &mut BuilderMap) {
|
|
|
|
self.build(build_data);
|
2021-12-16 16:07:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RoomDrawer {
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn new() -> Box<RoomDrawer> {
|
|
|
|
Box::new(RoomDrawer {})
|
|
|
|
}
|
|
|
|
|
2021-12-16 16:23:38 -05:00
|
|
|
#[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();
|
2022-02-04 14:09:48 -05:00
|
|
|
let center_pt = Point::new(center.0, center.1);
|
2021-12-16 16:23:38 -05:00
|
|
|
|
|
|
|
for y in room.y1..=room.y2 {
|
|
|
|
for x in room.x1..=room.x2 {
|
|
|
|
let idx = build_data.map.xy_idx(x, y);
|
2022-02-04 14:09:48 -05:00
|
|
|
let distance = DistanceAlg::Pythagoras.distance2d(center_pt, Point::new(x, y));
|
2021-12-16 16:23:38 -05:00
|
|
|
|
|
|
|
if idx > 0
|
|
|
|
&& idx < ((build_data.map.width * build_data.map.height) - 1) as usize
|
|
|
|
&& distance <= radius
|
|
|
|
{
|
|
|
|
build_data.map.tiles[idx] = TileType::Floor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 14:59:35 -05:00
|
|
|
fn build(&mut self, build_data: &mut BuilderMap) {
|
2021-12-16 16:07:33 -05:00
|
|
|
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() {
|
2021-12-16 16:23:38 -05:00
|
|
|
// Choose which shape of room to render
|
2022-02-03 14:59:35 -05:00
|
|
|
match roll_dice(1, 4) {
|
2021-12-16 16:23:38 -05:00
|
|
|
1 => self.circle(build_data, room),
|
|
|
|
_ => self.rectangle(build_data, room),
|
|
|
|
};
|
2021-12-16 16:07:33 -05:00
|
|
|
build_data.take_snapshot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|