Pass the whole Map into MapGenerator.generate

This commit is contained in:
Eryn Wells 2023-02-18 22:55:20 -08:00
parent 7428d95126
commit 37ffa423b6
3 changed files with 27 additions and 16 deletions

View file

@ -12,7 +12,7 @@ import numpy as np
import numpy.typing as npt import numpy.typing as npt
import tcod import tcod
from ..engine import Configuration from ..configuration import Configuration
from ..geometry import Point, Rect, Size from ..geometry import Point, Rect, Size
from .generator import MapGenerator from .generator import MapGenerator
from .tile import Empty, Shroud from .tile import Empty, Shroud
@ -28,7 +28,6 @@ class Map:
shape = map_size.numpy_shape shape = map_size.numpy_shape
self.tiles = np.full(shape, fill_value=Empty, order='F') self.tiles = np.full(shape, fill_value=Empty, order='F')
generator.generate(self.tiles)
self.up_stairs = generator.up_stairs self.up_stairs = generator.up_stairs
self.down_stairs = generator.down_stairs self.down_stairs = generator.down_stairs
@ -42,6 +41,8 @@ class Map:
self.__walkable_points = None self.__walkable_points = None
generator.generate(self)
@property @property
def bounds(self) -> Rect: def bounds(self) -> Rect:
'''The bounds of the map''' '''The bounds of the map'''

View file

@ -20,7 +20,8 @@ class MapGenerator:
'''The location of any routes to a lower floor of the dungeon.''' '''The location of any routes to a lower floor of the dungeon.'''
raise NotImplementedError() raise NotImplementedError()
def generate(self, tiles: np.ndarray): # pylint: disable=redefined-builtin
def generate(self, map: 'Map'):
'''Generate a map and place it in `tiles`''' '''Generate a map and place it in `tiles`'''
raise NotImplementedError() raise NotImplementedError()
@ -42,9 +43,10 @@ class RoomsAndCorridorsGenerator(MapGenerator):
def down_stairs(self) -> List[Point]: def down_stairs(self) -> List[Point]:
return self.room_generator.down_stairs return self.room_generator.down_stairs
def generate(self, tiles: np.ndarray): # pylint: disable=redefined-builtin
def generate(self, map: 'Map'):
self.room_generator.generate() self.room_generator.generate()
self.room_generator.apply(tiles) self.room_generator.apply(map)
self.corridor_generator.generate(self.room_generator.rooms) self.corridor_generator.generate(self.room_generator.rooms)
self.corridor_generator.apply(tiles) self.corridor_generator.apply(map.tiles)

View file

@ -1,15 +1,17 @@
# Eryn Wells <eryn@erynwells.me>
import random import random
from copy import copy from copy import copy
from dataclasses import dataclass from dataclasses import dataclass
from typing import List, Optional from typing import List, Optional, TYPE_CHECKING
import numpy as np
import tcod import tcod
from ... import log from ... import log
from ...geometry import Point, Rect, Size from ...geometry import Point, Rect, Size
from ..room import Room, RectangularRoom
from ..tile import Empty, Floor, StairsUp, StairsDown, Wall if TYPE_CHECKING:
from .. import Map
class RoomGenerator: class RoomGenerator:
@ -29,7 +31,9 @@ class RoomGenerator:
def __init__(self, *, size: Size, config: Optional[Configuration] = None): def __init__(self, *, size: Size, config: Optional[Configuration] = None):
self.size = size self.size = size
self.configuration = config if config else copy(RoomGenerator.DefaultConfiguration) self.configuration = config if config else copy(RoomGenerator.DefaultConfiguration)
self.rooms: List[Room] = [] self.rooms: List[Room] = []
self.up_stairs: List[Point] = [] self.up_stairs: List[Point] = []
self.down_stairs: List[Point] = [] self.down_stairs: List[Point] = []
@ -54,20 +58,23 @@ class RoomGenerator:
''' '''
raise NotImplementedError() raise NotImplementedError()
def apply(self, tiles: np.ndarray): # pylint: disable=redefined-builtin
def apply(self, map: 'Map'):
'''Apply the generated rooms to a tile array''' '''Apply the generated rooms to a tile array'''
self._apply(tiles) self._apply(map)
self._apply_stairs(tiles) self._apply_stairs(map.tiles)
def _apply(self, tiles: np.ndarray): def _apply(self, map: 'Map'):
''' '''
Apply the generated list of rooms to an array of tiles. Subclasses must implement this. Apply the generated list of rooms to an array of tiles. Subclasses must implement this.
Arguments Arguments
--------- ---------
tiles: np.ndarray map: Map
The array of tiles to update. The game map to apply the generated room to
''' '''
tiles = map.tiles
for room in self.rooms: for room in self.rooms:
for pt in room.floor_points: for pt in room.floor_points:
tiles[pt.x, pt.y] = Floor tiles[pt.x, pt.y] = Floor
@ -76,6 +83,7 @@ class RoomGenerator:
for pt in room.wall_points: for pt in room.wall_points:
if tiles[pt.x, pt.y] != Empty: if tiles[pt.x, pt.y] != Empty:
continue continue
tiles[pt.x, pt.y] = Wall tiles[pt.x, pt.y] = Wall
def _generate_stairs(self): def _generate_stairs(self):