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:41:29 -05:00
|
|
|
use crate::spawner;
|
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;
|
2021-12-01 11:41:29 -05:00
|
|
|
use specs::prelude::*;
|
2021-12-01 10:47:41 -05:00
|
|
|
|
2021-12-01 12:03:49 -05:00
|
|
|
pub struct SimpleMapBuilder {
|
|
|
|
map: Map,
|
|
|
|
starting_position: Position,
|
2021-12-01 12:08:14 -05:00
|
|
|
depth: i32,
|
|
|
|
rooms: Vec<Rect>
|
2021-12-01 12:03:49 -05:00
|
|
|
}
|
2021-12-01 10:47:41 -05:00
|
|
|
|
|
|
|
impl MapBuilder for SimpleMapBuilder {
|
2021-12-01 12:03:49 -05:00
|
|
|
fn build_map(&mut self) {
|
|
|
|
self.rooms_and_corridors();
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
2021-12-01 11:41:29 -05:00
|
|
|
|
2021-12-01 12:03:49 -05:00
|
|
|
fn spawn_entities(&mut self, ecs: &mut World) {
|
2021-12-01 12:08:14 -05:00
|
|
|
for room in self.rooms.iter().skip(1) {
|
2021-12-01 12:03:49 -05:00
|
|
|
spawner::spawn_room(ecs, room, self.depth);
|
2021-12-01 11:41:29 -05:00
|
|
|
}
|
|
|
|
}
|
2021-12-01 12:03:49 -05:00
|
|
|
|
|
|
|
fn get_map(&self) -> Map {
|
|
|
|
self.map.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_starting_position(&self) -> Position {
|
|
|
|
self.starting_position.clone()
|
|
|
|
}
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SimpleMapBuilder {
|
2021-12-01 12:03:49 -05:00
|
|
|
pub fn new(new_depth: i32) -> SimpleMapBuilder {
|
|
|
|
SimpleMapBuilder{
|
|
|
|
map: Map::new(new_depth),
|
|
|
|
starting_position: Position { x: 0, y: 0 },
|
2021-12-01 12:08:14 -05:00
|
|
|
depth: new_depth,
|
|
|
|
rooms: Vec::new(),
|
2021-12-01 12:03:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rooms_and_corridors(&mut self) {
|
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);
|
2021-12-01 12:03:49 -05:00
|
|
|
let x = rng.roll_dice(1, self.map.width - w - 1) - 1;
|
|
|
|
let y = rng.roll_dice(1, self.map.height - h - 1) - 1;
|
2021-12-01 10:47:41 -05:00
|
|
|
|
|
|
|
let new_room = Rect::new(x, y, w, h);
|
|
|
|
let mut ok = true;
|
|
|
|
|
2021-12-01 12:08:14 -05:00
|
|
|
for other_room in self.rooms.iter() {
|
2021-12-01 10:47:41 -05:00
|
|
|
if new_room.intersect(other_room) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
2021-12-01 12:03:49 -05:00
|
|
|
apply_room_to_map(&mut self.map, &new_room);
|
2021-12-01 10:47:41 -05:00
|
|
|
|
2021-12-01 12:08:14 -05:00
|
|
|
if !self.rooms.is_empty() {
|
2021-12-01 10:47:41 -05:00
|
|
|
let (new_x, new_y) = new_room.center();
|
2021-12-01 12:08:14 -05:00
|
|
|
let (prev_x, prev_y) = self.rooms[self.rooms.len() - 1].center();
|
2021-12-01 10:47:41 -05:00
|
|
|
|
|
|
|
if rng.range(0, 2) == 1 {
|
2021-12-01 12:03:49 -05:00
|
|
|
apply_horizontal_tunnel(&mut self.map, prev_x, new_x, prev_y);
|
|
|
|
apply_vertical_tunnel(&mut self.map, prev_y, new_y, new_x);
|
2021-12-01 10:47:41 -05:00
|
|
|
} else {
|
2021-12-01 12:03:49 -05:00
|
|
|
apply_vertical_tunnel(&mut self.map, prev_y, new_y, prev_x);
|
|
|
|
apply_horizontal_tunnel(&mut self.map, prev_x, new_x, new_y);
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 12:08:14 -05:00
|
|
|
self.rooms.push(new_room);
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 12:08:14 -05:00
|
|
|
let stairs_position = self.rooms[self.rooms.len() - 1].center();
|
2021-12-01 12:03:49 -05:00
|
|
|
let stairs_idx = self.map.xy_idx(stairs_position.0, stairs_position.1);
|
|
|
|
self.map.tiles[stairs_idx] = TileType::DownStairs;
|
2021-12-01 11:02:39 -05:00
|
|
|
|
2021-12-01 12:08:14 -05:00
|
|
|
let start_pos = self.rooms[0].center();
|
2021-12-01 12:03:49 -05:00
|
|
|
self.starting_position = Position {
|
2021-12-01 11:02:39 -05:00
|
|
|
x: start_pos.0,
|
|
|
|
y: start_pos.1,
|
2021-12-01 12:03:49 -05:00
|
|
|
};
|
2021-12-01 10:47:41 -05:00
|
|
|
}
|
|
|
|
}
|