2022-01-02 17:19:43 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Iterable, TYPE_CHECKING, Optional
|
|
|
|
|
2022-01-02 15:23:25 +00:00
|
|
|
import numpy as np # type: ignore
|
|
|
|
from tcod.console import Console
|
|
|
|
|
|
|
|
import tile_types
|
|
|
|
|
2022-01-02 17:19:43 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from entity import Entity
|
|
|
|
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
class GameMap:
|
2022-01-02 17:19:43 +00:00
|
|
|
def __init__(self, width: int, height: int, entities: Iterable[Entity] = ()):
|
2022-01-02 15:23:25 +00:00
|
|
|
self.width, self.height = width, height
|
2022-01-02 17:19:43 +00:00
|
|
|
self.entities = set(entities)
|
|
|
|
self.tiles = np.full((width, height), fill_value=tile_types.wall, order="F")
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
self.visible = np.full((width, height), fill_value=False, order="F")
|
|
|
|
self.explored = np.full((width, height), fill_value=False, order="F")
|
|
|
|
|
2022-01-02 17:19:43 +00:00
|
|
|
def get_blocking_entity_at_location(self, location_x: int, location_y: int) -> Optiona9[Entity]:
|
|
|
|
for entity in self.entities:
|
|
|
|
if entity.blocks_movement and entity.x == location_x and entity.y == location_y:
|
|
|
|
return entity
|
|
|
|
return None
|
|
|
|
|
2022-01-02 15:23:25 +00:00
|
|
|
def in_bounds(self, x: int, y: int) -> bool:
|
|
|
|
return 0 <= x < self.width and 0 <= y < self.height
|
|
|
|
|
|
|
|
def render(self, console: Console) -> None:
|
2022-01-02 17:19:43 +00:00
|
|
|
console.tiles_rgb[0 : self.width, 0 : self.height] = np.select(
|
2022-01-02 15:23:25 +00:00
|
|
|
condlist=[self.visible, self.explored],
|
|
|
|
choicelist=[self.tiles["light"], self.tiles["dark"]],
|
2022-01-02 17:19:43 +00:00
|
|
|
default=tile_types.SHROUD,
|
2022-01-02 15:23:25 +00:00
|
|
|
)
|
2022-01-02 17:19:43 +00:00
|
|
|
|
|
|
|
for entity in self.entities:
|
|
|
|
if self.visible[entity.x, entity.y]:
|
|
|
|
console.print(entity.x, entity.y, entity.char, fg=entity.color)
|