From 939996c71e03b850674ae50e2525427264d67164 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 16 Dec 2021 13:48:36 -0500 Subject: [PATCH] Expand Room sorter with more options --- src/map_builders.rs | 4 +-- src/map_builders/room_sorter.rs | 48 +++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/map_builders.rs b/src/map_builders.rs index c910380..6b63ee7 100644 --- a/src/map_builders.rs +++ b/src/map_builders.rs @@ -38,7 +38,7 @@ use room_based_stairs::RoomBasedStairs; use room_based_starting_position::RoomBasedStartingPosition; use room_corner_rounding::RoomCornerRounder; use room_exploder::RoomExploder; -use room_sorter::RoomSorter; +use room_sorter::{RoomSort, RoomSorter}; use rooms_corridors_bsp::BspCorridors; use rooms_corridors_dogleg::DoglegCorridors; use simple_map::SimpleMapBuilder; @@ -216,7 +216,7 @@ pub fn random_builder(new_depth: i32, rng: &mut RandomNumberGenerator) -> Builde builder .start_with(BspDungeonBuilder::new()) - .with(RoomSorter::new()) + .with(RoomSorter::new(RoomSort::Central)) .with(BspCorridors::new()) .with(AreaStartingPosition::new(XStart::Center, YStart::Center)) .with(CullUnreachable::new()) diff --git a/src/map_builders/room_sorter.rs b/src/map_builders/room_sorter.rs index 316f34e..3437290 100644 --- a/src/map_builders/room_sorter.rs +++ b/src/map_builders/room_sorter.rs @@ -1,8 +1,19 @@ use rltk::RandomNumberGenerator; use super::{BuilderMap, MetaMapBuilder}; +use crate::Rect; -pub struct RoomSorter {} +pub enum RoomSort { + LeftMost, + RightMost, + TopMost, + BottomMost, + Central, +} + +pub struct RoomSorter { + sort_by: RoomSort, +} impl MetaMapBuilder for RoomSorter { #[allow(dead_code)] @@ -13,15 +24,36 @@ impl MetaMapBuilder for RoomSorter { impl RoomSorter { #[allow(dead_code)] - pub fn new() -> Box { - Box::new(RoomSorter {}) + pub fn new(sort_by: RoomSort) -> Box { + Box::new(RoomSorter { sort_by }) } fn sorter(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - build_data - .rooms - .as_mut() - .unwrap() - .sort_by(|a, b| a.x1.cmp(&b.x1)); + let rooms = build_data.rooms.as_mut().unwrap(); + + match self.sort_by { + RoomSort::LeftMost => rooms.sort_by(|a, b| a.x1.cmp(&b.x1)), + RoomSort::RightMost => rooms.sort_by(|a, b| b.x2.cmp(&a.x2)), + RoomSort::TopMost => rooms.sort_by(|a, b| a.y1.cmp(&b.y1)), + RoomSort::BottomMost => rooms.sort_by(|a, b| b.y2.cmp(&a.y2)), + RoomSort::Central => { + let map_center = + rltk::Point::new(build_data.map.width / 2, build_data.map.height / 2); + let center_sort = |a: &Rect, b: &Rect| { + let a_center = a.center(); + let a_center_pt = rltk::Point::new(a_center.0, a_center.1); + let b_center = b.center(); + let b_center_pt = rltk::Point::new(b_center.0, b_center.1); + let distance_a = + rltk::DistanceAlg::Pythagoras.distance2d(a_center_pt, map_center); + let distance_b = + rltk::DistanceAlg::Pythagoras.distance2d(b_center_pt, map_center); + + distance_a.partial_cmp(&distance_b).unwrap() + }; + + rooms.sort_by(center_sort); + } + } } }