diff --git a/game_map.py b/game_map.py index ad742ac..5587217 100644 --- a/game_map.py +++ b/game_map.py @@ -1,18 +1,17 @@ -import numpy as np # type: ignore +import numpy as np # type: ignore from tcod.console import Console import tile_types + class GameMap: def __init__(self, width: int, height: int): - self.width, self.height = width, height; - self.tiles = np.full((width, height), fill_value=tile_types.floor, order="F") - - self.tiles[30:33, 22] = tile_types.wall + self.width, self.height = width, height + self.tiles = np.full((width, height), fill_value=tile_types.wall, order="F") def in_bounds(self, x: int, y: int) -> bool: - """Return True if x and y are inside of the bounds of the map.""" + """Return True if x and y are inside the bounds of the map.""" return 0 <= x < self.width and 0 <= y < self.height def render(self, console: Console): - console.tiles_rgb[0: self.width, 0: self.height] = self.tiles["dark"] \ No newline at end of file + console.tiles_rgb[0: self.width, 0: self.height] = self.tiles["dark"] diff --git a/main.py b/main.py index 2d298d0..c3c3d0c 100755 --- a/main.py +++ b/main.py @@ -3,8 +3,8 @@ import tcod from engine import Engine from entity import Entity -from game_map import GameMap from input_handlers import EventHandler +from procgen import generate_dungeon def main() -> None: @@ -23,20 +23,20 @@ def main() -> None: event_handler = EventHandler() - player = Entity(int(screen_width / 2), int(screen_height/ 2), "@", (255, 255, 255)) + player = Entity(int(screen_width / 2), int(screen_height / 2), "@", (255, 255, 255)) npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), "@", (255, 255, 0)) entities = {npc, player} - game_map = GameMap(map_width, map_height) + game_map = generate_dungeon(map_width, map_height) engine = Engine(entities, event_handler, game_map, player) with tcod.context.new_terminal( - screen_width, - screen_height, - tileset=tileset, - title="Yet Another Roguelike Tutorial", - vsync=True, + screen_width, + screen_height, + tileset=tileset, + title="Yet Another Roguelike Tutorial", + vsync=True, ) as context: root_console = tcod.Console(screen_width, screen_height, order="F") while True: diff --git a/procgen.py b/procgen.py new file mode 100644 index 0000000..6e1d3d8 --- /dev/null +++ b/procgen.py @@ -0,0 +1,36 @@ +from typing import Tuple + +from game_map import GameMap +import tile_types + + +class RectangularRoom: + def __init__(self, x: int, y: int, width: int, height: int): + self.x1 = x + self.y1 = y + self.x2 = x + width + self.y2 = y + height + + @property + def center(self) -> Tuple[int, int]: + center_x = int((self.x1 + self.x2) / 2) + center_y = int((self.y1 + self.y1) / 2) + + return center_x, center_y + + @property + def inner(self) -> Tuple[slice, slice]: + """Return the inner area of this room as a 2D array index.""" + return slice(self.x1 + 1, self.x2), slice(self.y1 + 1, self.y2) + + +def generate_dungeon(map_width, map_height) -> GameMap: + dungeon = GameMap(map_width, map_height) + + room_1 = RectangularRoom(x=20, y=15, width=10, height=15) + room_2 = RectangularRoom(x=35, y=15, width=10, height=15) + + dungeon.tiles[room_1.inner] = tile_types.floor + dungeon.tiles[room_2.inner] = tile_types.floor + + return dungeon diff --git a/requirements.txt b/requirements.txt index dd23368..04ce4bf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -tcod>=11.13 +tcod>=11.14 numpy>=1.18 \ No newline at end of file diff --git a/tile_types.py b/tile_types.py index c44d1b9..37bf9ec 100644 --- a/tile_types.py +++ b/tile_types.py @@ -15,16 +15,17 @@ graphic_dt = np.dtype( tile_dt = np.dtype( [ ("walkable", np.bool), # True if this tile can be walked over. - ("transparent", np.bool), # True if this tile doesn't block FOV. - ("dark", graphic_dt), # Graphics for when this tile is not in FOV. + ("transparent", np.bool), # True if this tile doesn't block FOV. + ("dark", graphic_dt), # Graphics for when this tile is not in FOV. ] ) + def new_tile( - *, # Enforce the use of keywords, so that parameter order doesn't matter - walkable: int, - transparent: int, - dark: Tuple[int, Tuple[int, int, int], Tuple[int, int, int]] + *, # Enforce the use of keywords, so that parameter order doesn't matter + walkable: int, + transparent: int, + dark: Tuple[int, Tuple[int, int, int], Tuple[int, int, int]] ) -> np.ndarray: """Helper function for defining individual tile types""" return np.array((walkable, transparent, dark), dtype=tile_dt) @@ -40,4 +41,4 @@ wall = new_tile( walkable=False, transparent=False, dark=(ord(" "), (255, 255, 255), (0, 0, 100)) -) \ No newline at end of file +)