Add two new room generators

- OneBigRoomGenerator
- RandomRectRoomGenerator
This commit is contained in:
Eryn Wells 2023-02-10 21:12:42 -08:00
parent 9d00f3b638
commit 85b059dbd4

View file

@ -97,15 +97,53 @@ class RoomGenerator:
tiles[pt.x, pt.y] = StairsDown
class BSPRoomGenerator(RoomGenerator):
'''Generate a rooms-and-corridors style map with BSP.'''
class OneBigRoomGenerator(RoomGenerator):
'''Generates one big room in the center of the map.'''
@dataclass
class Configuration:
'''Configuration parameters for BSPRoomGenerator.'''
def _generate(self) -> bool:
if self.rooms:
return True
minimum_room_size: Size
maximum_room_size: Size
origin = Point(self.size.width // 4, self.size.height // 4)
size = Size(self.size.width // 2, self.size.height // 2)
room = RectangularRoom(Rect(origin, size))
self.rooms.append(room)
return True
class RandomRectRoomGenerator(RoomGenerator):
'''Generate rooms by repeatedly attempting to place rects of random size across the map.'''
NUMBER_OF_ATTEMPTS_PER_ROOM = 5
def _generate(self) -> bool:
number_of_attempts = 0
minimum_room_size = self.configuration.minimum_room_size
maximum_room_size = self.configuration.maximum_room_size
width_range = (minimum_room_size.width, maximum_room_size.width)
height_range = (minimum_room_size.height, maximum_room_size.height)
while len(self.rooms) < self.configuration.number_of_rooms:
size = Size(random.randint(*width_range), random.randint(*height_range))
origin = Point(random.randint(0, self.size.width - size.width),
random.randint(0, self.size.height - size.height))
candidate_room_rect = Rect(origin, size)
overlaps_any_existing_room = any(candidate_room_rect.intersects(room.bounds) for room in self.rooms)
if not overlaps_any_existing_room:
self.rooms.append(RectangularRoom(candidate_room_rect))
number_of_attempts = 0
continue
number_of_attempts += 1
if number_of_attempts > RandomRectRoomGenerator.NUMBER_OF_ATTEMPTS_PER_ROOM:
break
return True
DefaultConfiguration = Configuration(
minimum_room_size=Size(7, 7),