1
0
Fork 0
python-roguelike/game_map.py

47 lines
1.7 KiB
Python
Raw Normal View History

from __future__ import annotations
from typing import Iterable, TYPE_CHECKING
2022-01-06 13:27:15 -05:00
import numpy as np # type: ignore
2022-01-06 11:41:34 -05:00
from tcod.console import Console
import tile_types
if TYPE_CHECKING:
from entity import Entity
2022-01-06 13:27:15 -05:00
2022-01-06 11:41:34 -05:00
class GameMap:
def __init__(self, width: int, height: int, entities: Iterable[Entity] = ()):
2022-01-06 13:27:15 -05:00
self.width, self.height = width, height
self.entities = entities
2022-01-06 13:27:15 -05:00
self.tiles = np.full((width, height), fill_value=tile_types.wall, order="F")
2022-01-06 11:41:34 -05:00
2022-01-07 14:18:47 -05:00
self.visible = np.full((width, height), fill_value=False, order="F") # Tiles the player can currently see
self.explored = np.full((width, height), fill_value=False, order="F") # Tiles the player has seen before
2022-01-06 11:41:34 -05:00
def in_bounds(self, x: int, y: int) -> bool:
2022-01-06 13:27:15 -05:00
"""Return True if x and y are inside the bounds of the map."""
2022-01-06 11:41:34 -05:00
return 0 <= x < self.width and 0 <= y < self.height
def render(self, console: Console):
2022-01-07 14:18:47 -05:00
"""
Renders the map.
If a tile is in the "visible" array, then draw it with the "light" colors.
If it isn't, but it's in the "explored" array, then draw it with the "dark" colors.
Otherwise, the default is "SHROUD".
:param console:
:return:
"""
console.tiles_rgb[0: self.width, 0: self.height] = np.select(
condlist=[self.visible, self.explored],
choicelist=[self.tiles["light"], self.tiles["dark"]],
default=tile_types.SHROUD
)
for entity in self.entities:
# Only print entities that are in the FOV
if self.visible[entity.x, entity.y]:
console.print(x=entity.x, y=entity.y, string=entity.char, fg=entity.color)