From ec4b06c7218590d7faca418a441bd0c5281612f9 Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Sun, 19 Dec 2021 11:40:26 -0600 Subject: [PATCH] Prevent rooms from generating right next to each other --- src/rooms.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/rooms.rs b/src/rooms.rs index f42db91..b1b805c 100644 --- a/src/rooms.rs +++ b/src/rooms.rs @@ -62,8 +62,9 @@ impl RoomBounds { (y_min..y_max).flat_map(move |y| (x_min..x_max).map(move |x| (x, y))) } - /// Returns whether the two rooms are overlapping. - pub fn overlapping(&self, other: &Self) -> bool { + /// Returns whether the two rooms are overlapping, i.e., there + /// exists at least one tile that is contained in both rooms. + pub fn intersects(&self, other: &Self) -> bool { fn range_overlapping(a: Range, b: Range) -> bool { if a.start > b.start { range_overlapping(b, a) @@ -80,6 +81,19 @@ impl RoomBounds { other.ul_corner.1..other.ul_corner.1 + other.size.1, ) } + + /// Returns whether the two rooms are within distance `dist` of + /// one another or intersecting. + pub fn near(&self, other: &Self, dist: usize) -> bool { + RoomBounds { + size: (self.size.0 + dist, self.size.1 + dist), + ..*self + } + .intersects(&RoomBounds { + size: (other.size.0 + dist, other.size.1 + dist), + ..*other + }) + } } /// The possible sizes of a room, on both the x and y axes. @@ -105,7 +119,7 @@ fn gen_room_bounds( ); let new_room = RoomBounds { ul_corner, size }; - if v.iter().all(|room| !room.overlapping(&new_room)) { + if v.iter().all(|room| !room.near(&new_room, 2)) { v.push(new_room) } }