gen_room_bounds -> associated function
This commit is contained in:
parent
515fa65e61
commit
e8030b9e07
18
src/rooms.rs
18
src/rooms.rs
|
@ -24,7 +24,7 @@ use crate::game::{DungeonTile, LEVEL_SIZE};
|
||||||
/// passages.
|
/// passages.
|
||||||
pub fn generate(n_rooms: usize, size: (usize, usize), rng: &mut impl Rng) -> Grid<DungeonTile> {
|
pub fn generate(n_rooms: usize, size: (usize, usize), rng: &mut impl Rng) -> Grid<DungeonTile> {
|
||||||
let mut grid = Grid::init(size.1, size.0, DungeonTile::Wall);
|
let mut grid = Grid::init(size.1, size.0, DungeonTile::Wall);
|
||||||
let rooms = gen_room_bounds(n_rooms, size, rng);
|
let rooms = RoomBounds::generate(n_rooms, size, rng);
|
||||||
|
|
||||||
for room in rooms {
|
for room in rooms {
|
||||||
for (x, y) in room.tiles() {
|
for (x, y) in room.tiles() {
|
||||||
|
@ -59,6 +59,9 @@ pub fn generate_level(
|
||||||
data
|
data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The possible sizes of a room, on both the x and y axes.
|
||||||
|
const ROOM_SIZE_LIMITS: Range<usize> = 4..8;
|
||||||
|
|
||||||
/// The bounding box of a room.
|
/// The bounding box of a room.
|
||||||
struct RoomBounds {
|
struct RoomBounds {
|
||||||
ul_corner: (usize, usize),
|
ul_corner: (usize, usize),
|
||||||
|
@ -106,19 +109,15 @@ impl RoomBounds {
|
||||||
..*other
|
..*other
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// The possible sizes of a room, on both the x and y axes.
|
|
||||||
const ROOM_SIZE_LIMITS: Range<usize> = 4..8;
|
|
||||||
|
|
||||||
/// Generates bounds for a set of at most `n_rooms` nonoverlapping
|
/// Generates bounds for a set of at most `n_rooms` nonoverlapping
|
||||||
/// rooms within a region of size `region_size`.
|
/// rooms within a region of size `region_size`.
|
||||||
fn gen_room_bounds(
|
fn generate(
|
||||||
n_rooms: usize,
|
n_rooms: usize,
|
||||||
region_size: (usize, usize),
|
region_size: (usize, usize),
|
||||||
rng: &mut impl Rng,
|
rng: &mut impl Rng,
|
||||||
) -> Vec<RoomBounds> {
|
) -> Vec<Self> {
|
||||||
let mut v: Vec<RoomBounds> = Vec::new();
|
let mut v: Vec<Self> = Vec::new();
|
||||||
|
|
||||||
for _ in 0..n_rooms {
|
for _ in 0..n_rooms {
|
||||||
let size = (
|
let size = (
|
||||||
|
@ -130,7 +129,7 @@ fn gen_room_bounds(
|
||||||
rng.gen_range(0..region_size.1 - size.1),
|
rng.gen_range(0..region_size.1 - size.1),
|
||||||
);
|
);
|
||||||
|
|
||||||
let new_room = RoomBounds { ul_corner, size };
|
let new_room = Self { ul_corner, size };
|
||||||
if v.iter().all(|room| !room.near(&new_room, 2)) {
|
if v.iter().all(|room| !room.near(&new_room, 2)) {
|
||||||
v.push(new_room)
|
v.push(new_room)
|
||||||
}
|
}
|
||||||
|
@ -138,3 +137,4 @@ fn gen_room_bounds(
|
||||||
|
|
||||||
v
|
v
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue