Refactor bsp_interior map builder to use Rust builder pattern
This commit is contained in:
parent
ec6e44477b
commit
3f692846f8
@ -3,100 +3,61 @@ use rltk::RandomNumberGenerator;
|
|||||||
use super::MapBuilder;
|
use super::MapBuilder;
|
||||||
use crate::components::Position;
|
use crate::components::Position;
|
||||||
use crate::{spawner, Map, Rect, TileType, SHOW_MAPGEN_VISUALIZER};
|
use crate::{spawner, Map, Rect, TileType, SHOW_MAPGEN_VISUALIZER};
|
||||||
|
use crate::map_builders::{BuilderMap, InitialMapBuilder};
|
||||||
|
use crate::map_builders::common::draw_corridor;
|
||||||
|
|
||||||
const MIN_ROOM_SIZE: i32 = 8;
|
const MIN_ROOM_SIZE: i32 = 8;
|
||||||
|
|
||||||
pub struct BspInteriorBuilder {
|
pub struct BspInteriorBuilder {
|
||||||
map: Map,
|
|
||||||
starting_position: Position,
|
|
||||||
depth: i32,
|
|
||||||
rooms: Vec<Rect>,
|
|
||||||
history: Vec<Map>,
|
|
||||||
rects: Vec<Rect>,
|
rects: Vec<Rect>,
|
||||||
spawn_list: Vec<(usize, String)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MapBuilder for BspInteriorBuilder {
|
impl InitialMapBuilder for BspInteriorBuilder {
|
||||||
fn get_map(&self) -> Map {
|
#[allow(dead_code)]
|
||||||
self.map.clone()
|
fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
||||||
}
|
self.build(rng, build_data);
|
||||||
|
|
||||||
fn get_starting_position(&self) -> Position {
|
|
||||||
self.starting_position
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_snapshot_history(&self) -> Vec<Map> {
|
|
||||||
self.history.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_map(&mut self) {
|
|
||||||
self.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn take_snapshot(&mut self) {
|
|
||||||
if SHOW_MAPGEN_VISUALIZER {
|
|
||||||
let mut snapshot = self.map.clone();
|
|
||||||
for v in snapshot.revealed_tiles.iter_mut() {
|
|
||||||
*v = true;
|
|
||||||
}
|
|
||||||
self.history.push(snapshot);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_spawn_list(&self) -> &Vec<(usize, String)> {
|
|
||||||
&self.spawn_list
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BspInteriorBuilder {
|
impl BspInteriorBuilder {
|
||||||
pub fn new(new_depth: i32) -> BspInteriorBuilder {
|
pub fn new() -> Box<BspInteriorBuilder> {
|
||||||
BspInteriorBuilder {
|
Box::new(BspInteriorBuilder {
|
||||||
map: Map::new(new_depth),
|
|
||||||
starting_position: Position::default(),
|
|
||||||
depth: new_depth,
|
|
||||||
rooms: Vec::new(),
|
|
||||||
history: Vec::new(),
|
|
||||||
rects: Vec::new(),
|
rects: Vec::new(),
|
||||||
spawn_list: Vec::new(),
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build(&mut self) {
|
fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
||||||
let mut rng = RandomNumberGenerator::new();
|
let mut rooms: Vec<Rect> = Vec::new();
|
||||||
|
|
||||||
self.rects.clear();
|
self.rects.clear();
|
||||||
|
|
||||||
// Start with a single map-sized rectangle
|
// Start with a single map-sized rectangle
|
||||||
self.rects
|
self.rects
|
||||||
.push(Rect::new(1, 1, self.map.width - 2, self.map.height - 2));
|
.push(Rect::new(1, 1, build_data.map.width - 2, build_data.map.height - 2));
|
||||||
|
|
||||||
let first_room = self.rects[0];
|
let first_room = self.rects[0];
|
||||||
self.add_subrects(first_room, &mut rng); // Divide the first room
|
self.add_subrects(first_room, rng); // Divide the first room
|
||||||
|
|
||||||
let rooms = self.rects.clone();
|
let rooms_copy = self.rects.clone();
|
||||||
for r in rooms.iter() {
|
for r in rooms_copy.iter() {
|
||||||
let room = *r;
|
let room = *r;
|
||||||
|
|
||||||
self.rooms.push(room);
|
rooms.push(room);
|
||||||
for y in room.y1..room.y2 {
|
for y in room.y1..room.y2 {
|
||||||
for x in room.x1..room.x2 {
|
for x in room.x1..room.x2 {
|
||||||
let idx = self.map.xy_idx(x, y);
|
let idx = build_data.map.xy_idx(x, y);
|
||||||
if idx > 0 && idx < ((self.map.width * self.map.height) - 1) as usize {
|
if idx > 0 && idx < ((build_data.map.width * build_data.map.height) - 1) as usize {
|
||||||
self.map.tiles[idx] = TileType::Floor;
|
build_data.map.tiles[idx] = TileType::Floor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.take_snapshot();
|
build_data.take_snapshot();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now we sort the rooms
|
|
||||||
self.rooms.sort_by(|a, b| a.x1.cmp(&b.x1));
|
|
||||||
|
|
||||||
// Now we want corridors
|
// Now we want corridors
|
||||||
for i in 0..self.rooms.len() - 1 {
|
for i in 0..rooms.len() - 1 {
|
||||||
let room = self.rooms[i];
|
let room = rooms[i];
|
||||||
let next_room = self.rooms[i + 1];
|
let next_room = rooms[i + 1];
|
||||||
let start_x = room.x1 + (rng.roll_dice(1, i32::abs(room.x1 - room.x2)) - 1);
|
let start_x = room.x1 + (rng.roll_dice(1, i32::abs(room.x1 - room.x2)) - 1);
|
||||||
let start_y = room.y1 + (rng.roll_dice(1, i32::abs(room.y1 - room.y2) - 1));
|
let start_y = room.y1 + (rng.roll_dice(1, i32::abs(room.y1 - room.y2) - 1));
|
||||||
let end_x =
|
let end_x =
|
||||||
@ -104,21 +65,11 @@ impl BspInteriorBuilder {
|
|||||||
let end_y =
|
let end_y =
|
||||||
next_room.y1 + (rng.roll_dice(1, i32::abs(next_room.y1 - next_room.y2)) - 1);
|
next_room.y1 + (rng.roll_dice(1, i32::abs(next_room.y1 - next_room.y2)) - 1);
|
||||||
|
|
||||||
self.draw_corridor(start_x, start_y, end_x, end_y);
|
draw_corridor(&mut build_data.map, start_x, start_y, end_x, end_y);
|
||||||
self.take_snapshot();
|
build_data.take_snapshot();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't forget the stairs
|
build_data.rooms = Some(rooms);
|
||||||
let stairs = self.rooms[self.rooms.len() - 1].center();
|
|
||||||
let stairs_idx = self.map.xy_idx(stairs.0, stairs.1);
|
|
||||||
self.map.tiles[stairs_idx] = TileType::DownStairs;
|
|
||||||
|
|
||||||
self.starting_position = Position::from(self.rooms[0].center());
|
|
||||||
|
|
||||||
// Spawn some entities
|
|
||||||
for room in self.rooms.iter().skip(1) {
|
|
||||||
spawner::spawn_room(&self.map, &mut rng, room, self.depth, &mut self.spawn_list);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_subrects(&mut self, rect: Rect, rng: &mut RandomNumberGenerator) {
|
fn add_subrects(&mut self, rect: Rect, rng: &mut RandomNumberGenerator) {
|
||||||
@ -163,24 +114,4 @@ impl BspInteriorBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_corridor(&mut self, x1: i32, y1: i32, x2: i32, y2: i32) {
|
|
||||||
let mut x = x1;
|
|
||||||
let mut y = y1;
|
|
||||||
|
|
||||||
while x != x2 || y != y2 {
|
|
||||||
if x < x2 {
|
|
||||||
x += 1;
|
|
||||||
} else if x > x2 {
|
|
||||||
x -= 1;
|
|
||||||
} else if y < y2 {
|
|
||||||
y += 1;
|
|
||||||
} else if y > y2 {
|
|
||||||
y -= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
let idx = self.map.xy_idx(x, y);
|
|
||||||
self.map.tiles[idx] = TileType::Floor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user