2021-12-01 10:47:41 -05:00
|
|
|
use super::{apply_horizontal_tunnel, apply_room_to_map, apply_vertical_tunnel, MapBuilder};
|
2021-12-01 11:02:39 -05:00
|
|
|
use crate::{Map, Position, Rect, TileType};
|
2021-12-01 10:47:41 -05:00
|
|
|
use rltk::RandomNumberGenerator;
|
|
|
|
|
|
|
|
pub struct SimpleMapBuilder {}
|
|
|
|
|
|
|
|
impl MapBuilder for SimpleMapBuilder {
|
2021-12-01 11:02:39 -05:00
|
|
|
fn build(new_depth: i32) -> (Map, Position) {
|
2021-12-01 10:47:41 -05:00
|
|
|
let mut map = Map::new(new_depth);
|
2021-12-01 11:02:39 -05:00
|
|
|
let playerpos = SimpleMapBuilder::rooms_and_corridors(&mut map);
|
2021-12-01 10:47:41 -05:00
|
|
|
|
2021-12-01 11:02:39 -05:00
|
|
|
(map, playerpos)
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SimpleMapBuilder {
|
2021-12-01 11:02:39 -05:00
|
|
|
fn rooms_and_corridors(map: &mut Map) -> Position {
|
2021-12-01 10:47:41 -05:00
|
|
|
const MAX_ROOMS: i32 = 30;
|
|
|
|
const MIN_SIZE: i32 = 6;
|
|
|
|
const MAX_SIZE: i32 = 10;
|
|
|
|
|
|
|
|
let mut rng = RandomNumberGenerator::new();
|
|
|
|
|
|
|
|
for _ in 0..MAX_ROOMS {
|
|
|
|
let w = rng.range(MIN_SIZE, MAX_SIZE);
|
|
|
|
let h = rng.range(MIN_SIZE, MAX_SIZE);
|
|
|
|
let x = rng.roll_dice(1, map.width - w - 1) - 1;
|
|
|
|
let y = rng.roll_dice(1, map.height - h - 1) - 1;
|
|
|
|
|
|
|
|
let new_room = Rect::new(x, y, w, h);
|
|
|
|
let mut ok = true;
|
|
|
|
|
|
|
|
for other_room in map.rooms.iter() {
|
|
|
|
if new_room.intersect(other_room) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
apply_room_to_map(map, &new_room);
|
|
|
|
|
|
|
|
if !map.rooms.is_empty() {
|
|
|
|
let (new_x, new_y) = new_room.center();
|
|
|
|
let (prev_x, prev_y) = map.rooms[map.rooms.len() - 1].center();
|
|
|
|
|
|
|
|
if rng.range(0, 2) == 1 {
|
|
|
|
apply_horizontal_tunnel(map, prev_x, new_x, prev_y);
|
|
|
|
apply_vertical_tunnel(map, prev_y, new_y, new_x);
|
|
|
|
} else {
|
|
|
|
apply_vertical_tunnel(map, prev_y, new_y, prev_x);
|
|
|
|
apply_horizontal_tunnel(map, prev_x, new_x, new_y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
map.rooms.push(new_room);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let stairs_position = map.rooms[map.rooms.len() - 1].center();
|
|
|
|
let stairs_idx = map.xy_idx(stairs_position.0, stairs_position.1);
|
|
|
|
map.tiles[stairs_idx] = TileType::DownStairs;
|
2021-12-01 11:02:39 -05:00
|
|
|
|
|
|
|
let start_pos = map.rooms[0].center();
|
|
|
|
|
|
|
|
Position {
|
|
|
|
x: start_pos.0,
|
|
|
|
y: start_pos.1,
|
|
|
|
}
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
}
|