Add straight line corridor builder

This commit is contained in:
Timothy Warren 2021-12-17 10:57:03 -05:00
parent ac8b70af2b
commit 5cbd914a98
2 changed files with 67 additions and 0 deletions

View File

@ -18,6 +18,7 @@ mod room_exploder;
mod room_sorter;
mod rooms_corridors_bsp;
mod rooms_corridors_dogleg;
mod rooms_corridors_lines;
mod rooms_corridors_nearest;
mod simple_map;
mod voronoi;
@ -44,6 +45,7 @@ use room_exploder::RoomExploder;
use room_sorter::{RoomSort, RoomSorter};
use rooms_corridors_bsp::BspCorridors;
use rooms_corridors_dogleg::DoglegCorridors;
use rooms_corridors_lines::StraightLineCorridors;
use rooms_corridors_nearest::NearestCorridors;
use simple_map::SimpleMapBuilder;
use specs::prelude::*;

View File

@ -0,0 +1,65 @@
use super::{BuilderMap, MetaMapBuilder};
use crate::{Rect, TileType};
use rltk::RandomNumberGenerator;
use std::collections::HashSet;
pub struct StraightLineCorridors {}
impl MetaMapBuilder for StraightLineCorridors {
#[allow(dead_code)]
fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
self.corridors(rng, build_data);
}
}
impl StraightLineCorridors {
#[allow(dead_code)]
pub fn new() -> Box<StraightLineCorridors> {
Box::new(StraightLineCorridors {})
}
fn corridors(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
let rooms: Vec<Rect>;
if let Some(rooms_builder) = &build_data.rooms {
rooms = rooms_builder.clone();
} else {
panic!("Straight Line Corridors require a builder with room structures.");
}
let mut connected: HashSet<usize> = HashSet::new();
for (i, room) in rooms.iter().enumerate() {
let mut room_distance: Vec<(usize, f32)> = Vec::new();
let room_center = room.center();
let room_center_pt = rltk::Point::new(room_center.0, room_center.1);
for (j, other_room) in rooms.iter().enumerate() {
if i != j && !connected.contains(&j) {
let other_center = other_room.center();
let other_center_pt = rltk::Point::new(other_center.0, other_center.1);
let distance =
rltk::DistanceAlg::Pythagoras.distance2d(room_center_pt, other_center_pt);
room_distance.push((j, distance));
}
}
if !room_distance.is_empty() {
room_distance.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
let dest_center = rooms[room_distance[0].0].center();
let line = rltk::line2d(
rltk::LineAlg::Bresenham,
room_center_pt,
rltk::Point::new(dest_center.0, dest_center.1),
);
for cell in line.iter() {
let idx = build_data.map.xy_idx(cell.x, cell.y);
build_data.map.tiles[idx] = TileType::Floor;
}
connected.insert(i);
build_data.take_snapshot();
}
}
}
}