Add corridors to the bsp interior map builder, completing section 4.4

This commit is contained in:
Timothy Warren 2021-12-03 14:15:17 -05:00
parent c38a8f1629
commit e8c03b9536
2 changed files with 46 additions and 3 deletions

View File

@ -1,5 +1,4 @@
use super::MapBuilder; use super::MapBuilder;
use crate::map_builders::common::apply_room_to_map;
use crate::spawner; use crate::spawner;
use crate::{components::Position, Map, Rect, TileType, SHOW_MAPGEN_VISUALIZER}; use crate::{components::Position, Map, Rect, TileType, SHOW_MAPGEN_VISUALIZER};
use rltk::RandomNumberGenerator; use rltk::RandomNumberGenerator;
@ -22,7 +21,7 @@ impl MapBuilder for BspInteriorBuilder {
} }
fn get_starting_position(&self) -> Position { fn get_starting_position(&self) -> Position {
self.starting_position.clone() self.starting_position
} }
fn get_snapshot_history(&self) -> Vec<Map> { fn get_snapshot_history(&self) -> Vec<Map> {
@ -91,6 +90,29 @@ impl BspInteriorBuilder {
self.take_snapshot(); self.take_snapshot();
} }
// Now we sort the rooms
self.rooms.sort_by(|a, b| a.x1.cmp(&b.x1));
// Now we want corridors
for i in 0..self.rooms.len() - 1 {
let room = self.rooms[i];
let next_room = self.rooms[i + 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 end_x =
next_room.x1 + (rng.roll_dice(1, i32::abs(next_room.x1 - next_room.x2)) - 1);
let end_y =
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);
self.take_snapshot();
}
// Don't forget the stairs
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;
let start = self.rooms[0].center(); let start = self.rooms[0].center();
self.starting_position = start.into(); self.starting_position = start.into();
} }
@ -137,4 +159,24 @@ 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;
}
}
} }

View File

@ -21,8 +21,9 @@ pub trait MapBuilder {
pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> { pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
let mut rng = rltk::RandomNumberGenerator::new(); let mut rng = rltk::RandomNumberGenerator::new();
match rng.roll_dice(1, 2) { match rng.roll_dice(1, 3) {
1 => Box::new(BspDungeonBuilder::new(new_depth)), 1 => Box::new(BspDungeonBuilder::new(new_depth)),
2 => Box::new(BspInteriorBuilder::new(new_depth)),
_ => Box::new(SimpleMapBuilder::new(new_depth)), _ => Box::new(SimpleMapBuilder::new(new_depth)),
} }
} }