Break up room and corridor generation into generate and apply phases
- Generate creates rooms and corridors, and apply applies them to a tile grid. - Add up and down stairs generation to the Room Generators. - Clean up Room.wall_points and Room.floor_points to make it easier to write a generic apply() method on RoomGenerator
This commit is contained in:
parent
d4c4b5d879
commit
c59dc1b907
4 changed files with 122 additions and 72 deletions
|
@ -46,37 +46,49 @@ class ElbowCorridorGenerator(CorridorGenerator):
|
|||
self.corridors: List[List[Point]] = []
|
||||
|
||||
def generate(self, rooms: List[Room]) -> bool:
|
||||
if len(rooms) < 2:
|
||||
return True
|
||||
|
||||
for (left_room, right_room) in pairwise(rooms):
|
||||
left_room_bounds = left_room.bounds
|
||||
right_room_bounds = right_room.bounds
|
||||
|
||||
log.MAP.debug(' left: %s, %s', left_room, left_room_bounds)
|
||||
log.MAP.debug('right: %s, %s', right_room, right_room_bounds)
|
||||
|
||||
start_point = left_room_bounds.midpoint
|
||||
end_point = right_room_bounds.midpoint
|
||||
|
||||
# Randomly choose whether to move horizontally then vertically or vice versa
|
||||
if random.random() < 0.5:
|
||||
corner = Point(end_point.x, start_point.y)
|
||||
else:
|
||||
corner = Point(start_point.x, end_point.y)
|
||||
|
||||
log.MAP.debug('Digging a tunnel between %s and %s with corner %s', start_point, end_point, corner)
|
||||
log.MAP.debug('|-> start: %s', left_room_bounds)
|
||||
log.MAP.debug('`-> end: %s', right_room_bounds)
|
||||
|
||||
corridor: List[Point] = []
|
||||
|
||||
for x, y in tcod.los.bresenham(tuple(start_point), tuple(corner)).tolist():
|
||||
corridor.append(Point(x, y))
|
||||
for x, y in tcod.los.bresenham(tuple(corner), tuple(end_point)).tolist():
|
||||
corridor.append(Point(x, y))
|
||||
corridor = self._generate_corridor_between(left_room, right_room)
|
||||
self.corridors.append(corridor)
|
||||
|
||||
for i in range(len(rooms) - 2):
|
||||
corridor = self._generate_corridor_between(rooms[i], rooms[i + 2])
|
||||
self.corridors.append(corridor)
|
||||
|
||||
return True
|
||||
|
||||
def _generate_corridor_between(self, left_room, right_room):
|
||||
left_room_bounds = left_room.bounds
|
||||
right_room_bounds = right_room.bounds
|
||||
|
||||
log.MAP.debug(' left: %s, %s', left_room, left_room_bounds)
|
||||
log.MAP.debug('right: %s, %s', right_room, right_room_bounds)
|
||||
|
||||
start_point = left_room_bounds.midpoint
|
||||
end_point = right_room_bounds.midpoint
|
||||
|
||||
# Randomly choose whether to move horizontally then vertically or vice versa
|
||||
if random.random() < 0.5:
|
||||
corner = Point(end_point.x, start_point.y)
|
||||
else:
|
||||
corner = Point(start_point.x, end_point.y)
|
||||
|
||||
log.MAP.debug('Digging a tunnel between %s and %s with corner %s', start_point, end_point, corner)
|
||||
log.MAP.debug('|-> start: %s', left_room_bounds)
|
||||
log.MAP.debug('`-> end: %s', right_room_bounds)
|
||||
|
||||
corridor: List[Point] = []
|
||||
|
||||
for x, y in tcod.los.bresenham(tuple(start_point), tuple(corner)).tolist():
|
||||
corridor.append(Point(x, y))
|
||||
|
||||
for x, y in tcod.los.bresenham(tuple(corner), tuple(end_point)).tolist():
|
||||
corridor.append(Point(x, y))
|
||||
|
||||
return corridor
|
||||
|
||||
def apply(self, tiles):
|
||||
for corridor in self.corridors:
|
||||
for pt in corridor:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue