2022-01-02 17:19:43 +00:00
|
|
|
from __future__ import annotations
|
2022-01-02 21:43:44 +00:00
|
|
|
from typing import Iterable, TYPE_CHECKING, Optional, Iterator
|
2022-01-02 17:19:43 +00:00
|
|
|
|
2022-01-02 15:23:25 +00:00
|
|
|
import numpy as np # type: ignore
|
|
|
|
from tcod.console import Console
|
|
|
|
|
2022-01-02 21:43:44 +00:00
|
|
|
from entity import Actor
|
2022-01-02 15:23:25 +00:00
|
|
|
import tile_types
|
|
|
|
|
2022-01-02 17:19:43 +00:00
|
|
|
if TYPE_CHECKING:
|
2022-01-02 17:50:33 +00:00
|
|
|
from engine import Engine
|
2022-01-02 17:19:43 +00:00
|
|
|
from entity import Entity
|
|
|
|
|
2022-01-02 15:23:25 +00:00
|
|
|
|
|
|
|
class GameMap:
|
2022-01-02 21:43:44 +00:00
|
|
|
def __init__(
|
|
|
|
self, engine: Engine, width: int, height: int, entities: Iterable[Entity] = ()
|
2022-01-02 17:50:33 +00:00
|
|
|
):
|
|
|
|
self.engine = engine
|
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
|
|
|
|
2022-01-02 17:50:33 +00: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-02 15:23:25 +00:00
|
|
|
|
2022-01-02 21:43:44 +00:00
|
|
|
@property
|
|
|
|
def actors(self) -> Iterator[Actor]:
|
|
|
|
yield from (
|
|
|
|
entity
|
|
|
|
for entity in self.entities
|
|
|
|
if isinstance(entity, Actor) and entity.is_alive
|
|
|
|
)
|
|
|
|
|
2022-01-02 17:50:33 +00:00
|
|
|
def get_blocking_entity_at_location(
|
2022-01-02 21:43:44 +00:00
|
|
|
self, location_x: int, location_y: int
|
2022-01-02 17:50:33 +00:00
|
|
|
) -> Optional[Entity]:
|
2022-01-02 17:19:43 +00:00
|
|
|
for entity in self.entities:
|
2022-01-02 17:50:33 +00:00
|
|
|
if (
|
2022-01-02 21:43:44 +00:00
|
|
|
entity.blocks_movement
|
|
|
|
and entity.x == location_x
|
|
|
|
and entity.y == location_y
|
2022-01-02 17:50:33 +00:00
|
|
|
):
|
2022-01-02 17:19:43 +00:00
|
|
|
return entity
|
|
|
|
return None
|
|
|
|
|
2022-01-02 21:43:44 +00:00
|
|
|
def get_actor_at_location(self, x: int, y: int) -> Optional[Actor]:
|
|
|
|
for actor in self.actors:
|
|
|
|
if actor.x == x and actor.y == y:
|
|
|
|
return actor
|
|
|
|
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
|
|
|
|
2022-01-02 21:43:44 +00:00
|
|
|
entities_sorted_for_rendering = sorted(
|
|
|
|
self.entities, key=lambda x: x.render_order.value
|
|
|
|
)
|
|
|
|
|
|
|
|
for entity in entities_sorted_for_rendering:
|
2022-01-02 17:19:43 +00:00
|
|
|
if self.visible[entity.x, entity.y]:
|
2022-01-02 21:43:44 +00:00
|
|
|
console.print(
|
|
|
|
x=entity.x, y=entity.y, string=entity.char, fg=entity.color
|
|
|
|
)
|