gen_room_bounds -> associated function
This commit is contained in:
parent
515fa65e61
commit
e8030b9e07
54
src/rooms.rs
54
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,35 +109,32 @@ impl RoomBounds {
|
||||||
..*other
|
..*other
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// The possible sizes of a room, on both the x and y axes.
|
/// Generates bounds for a set of at most `n_rooms` nonoverlapping
|
||||||
const ROOM_SIZE_LIMITS: Range<usize> = 4..8;
|
/// rooms within a region of size `region_size`.
|
||||||
|
fn generate(
|
||||||
|
n_rooms: usize,
|
||||||
|
region_size: (usize, usize),
|
||||||
|
rng: &mut impl Rng,
|
||||||
|
) -> Vec<Self> {
|
||||||
|
let mut v: Vec<Self> = Vec::new();
|
||||||
|
|
||||||
/// Generates bounds for a set of at most `n_rooms` nonoverlapping
|
for _ in 0..n_rooms {
|
||||||
/// rooms within a region of size `region_size`.
|
let size = (
|
||||||
fn gen_room_bounds(
|
rng.gen_range(ROOM_SIZE_LIMITS),
|
||||||
n_rooms: usize,
|
rng.gen_range(ROOM_SIZE_LIMITS),
|
||||||
region_size: (usize, usize),
|
);
|
||||||
rng: &mut impl Rng,
|
let ul_corner = (
|
||||||
) -> Vec<RoomBounds> {
|
rng.gen_range(0..region_size.0 - size.0),
|
||||||
let mut v: Vec<RoomBounds> = Vec::new();
|
rng.gen_range(0..region_size.1 - size.1),
|
||||||
|
);
|
||||||
|
|
||||||
for _ in 0..n_rooms {
|
let new_room = Self { ul_corner, size };
|
||||||
let size = (
|
if v.iter().all(|room| !room.near(&new_room, 2)) {
|
||||||
rng.gen_range(ROOM_SIZE_LIMITS),
|
v.push(new_room)
|
||||||
rng.gen_range(ROOM_SIZE_LIMITS),
|
}
|
||||||
);
|
|
||||||
let ul_corner = (
|
|
||||||
rng.gen_range(0..region_size.0 - size.0),
|
|
||||||
rng.gen_range(0..region_size.1 - size.1),
|
|
||||||
);
|
|
||||||
|
|
||||||
let new_room = RoomBounds { ul_corner, size };
|
|
||||||
if v.iter().all(|room| !room.near(&new_room, 2)) {
|
|
||||||
v.push(new_room)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
v
|
v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue