2022-01-07 14:35:47 -05:00
|
|
|
from typing import Iterable, Any
|
2022-01-06 11:28:03 -05:00
|
|
|
|
|
|
|
from tcod.context import Context
|
|
|
|
from tcod.console import Console
|
2022-01-07 14:18:47 -05:00
|
|
|
from tcod.map import compute_fov
|
2022-01-06 11:28:03 -05:00
|
|
|
|
|
|
|
from entity import Entity
|
2022-01-06 11:56:08 -05:00
|
|
|
from game_map import GameMap
|
2022-01-06 11:28:03 -05:00
|
|
|
from input_handlers import EventHandler
|
|
|
|
|
|
|
|
|
|
|
|
class Engine:
|
2022-01-06 11:56:08 -05:00
|
|
|
def __init__(
|
2022-01-07 14:18:47 -05:00
|
|
|
self,
|
|
|
|
event_handler: EventHandler,
|
|
|
|
game_map: GameMap,
|
|
|
|
player: Entity
|
2022-01-06 11:56:08 -05:00
|
|
|
):
|
2022-01-06 11:28:03 -05:00
|
|
|
self.event_handler = event_handler
|
2022-01-06 11:56:08 -05:00
|
|
|
self.game_map = game_map
|
2022-01-06 11:28:03 -05:00
|
|
|
self.player = player
|
2022-01-07 14:18:47 -05:00
|
|
|
self.update_fov()
|
2022-01-06 11:28:03 -05:00
|
|
|
|
|
|
|
def handle_events(self, events: Iterable[Any]) -> None:
|
|
|
|
for event in events:
|
|
|
|
action = self.event_handler.dispatch(event)
|
|
|
|
|
|
|
|
if action is None:
|
|
|
|
continue
|
|
|
|
|
2022-01-06 11:56:08 -05:00
|
|
|
action.perform(self, self.player)
|
2022-01-06 11:28:03 -05:00
|
|
|
|
2022-01-07 14:18:47 -05:00
|
|
|
# Update the FOV before the player's next action.
|
|
|
|
self.update_fov()
|
|
|
|
|
|
|
|
def update_fov(self) -> None:
|
|
|
|
"""Recompute the visible area based on the player's point of view."""
|
|
|
|
self.game_map.visible[:] = compute_fov(
|
|
|
|
self.game_map.tiles["transparent"],
|
|
|
|
(self.player.x, self.player.y),
|
|
|
|
radius=8,
|
|
|
|
)
|
|
|
|
|
|
|
|
# If a tile is "visible" it should be added to "explored"
|
|
|
|
self.game_map.explored |= self.game_map.visible
|
|
|
|
|
2022-01-06 11:28:03 -05:00
|
|
|
def render(self, console: Console, context: Context) -> None:
|
2022-01-06 11:56:08 -05:00
|
|
|
self.game_map.render(console)
|
|
|
|
|
2022-01-06 11:28:03 -05:00
|
|
|
# Actually output to screen
|
|
|
|
context.present(console)
|
|
|
|
console.clear()
|