1
0

Begin setup of map generation

This commit is contained in:
Timothy Warren 2022-01-06 13:27:15 -05:00
parent 07dd70b25f
commit 56267fe8a7
5 changed files with 59 additions and 23 deletions

View File

@ -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"]
console.tiles_rgb[0: self.width, 0: self.height] = self.tiles["dark"]

16
main.py
View File

@ -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:

36
procgen.py Normal file
View File

@ -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

View File

@ -1,2 +1,2 @@
tcod>=11.13
tcod>=11.14
numpy>=1.18

View File

@ -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))
)
)