RoomGenerator constructor
RoomGenerator(
- int width,
- int height,
- RoomConstraint widthBounds,
- RoomConstraint heightBounds,
- double maxAspect,
Initialize a room generator to generate random rooms within a space of the
given width
and height
, with other tuning parameters:
- Room size:
widthBounds
andheightBounds
- Room shape:
maxAspect
Note that the RoomGenerator will ensure that the resulting rooms are
placed at odd positions and have odd sizes (within the given constraints)
so that the rooms will be aligned with corridors generated by the
MazeGenerator
.
Implementation
RoomGenerator(int width, int height, RoomConstraint widthBounds,
RoomConstraint heightBounds, double maxAspect)
: _roomBounds = Rect.sides(1, width - 1, height - 1, 1),
_maxAspect = maxAspect {
// safety checks
if (widthBounds.max < widthBounds.min ||
heightBounds.max < heightBounds.min) {
throw StateError('Room constraints have swapped min and max values: '
'width $widthBounds height $heightBounds');
}
var minAspect = math.max(widthBounds.min, heightBounds.min) /
math.min(widthBounds.min, heightBounds.min);
if (minAspect > _maxAspect) {
throw StateError(
'Room constraints always produce an aspect ratio greater than '
'configured max: width $widthBounds height $heightBounds');
}
// fill widths
for (var w = widthBounds.min; w <= widthBounds.max; w++) {
if (w % 2 == 1) _widths.add(w);
}
// fill heights
for (var h = heightBounds.min; h <= heightBounds.max; h++) {
if (h % 2 == 1) _heights.add(h);
}
}