Stepwise/source/engine.py
2025-03-21 12:06:08 +11:00

57 lines
1.5 KiB
Python

from typing import Any, Iterable
from tcod.context import Context
from tcod.console import Console
from tcod.map import compute_fov
from entity import Entity
from floor_map import FloorMap #TODO: replace with "DungeonMap"
class Engine:
def __init__(self, floor_map: FloorMap):
from event_handler import EventHandler #here to prevent circular imports
self.event_handler = EventHandler(self)
self.player = Entity(0, 0, "@", (255, 255, 255))
self.floor_map = floor_map
self.entities: Iterable[Entity] = []
#spawn the player, add to render list
self.player.x, self.player.y = self.floor_map.spawn
self.entities.append(self.player)
#kick off the render
self.update_fov()
def handle_events(self, events: Iterable[Any]) -> None:
for event in events:
action = self.event_handler.dispatch(event)
if action is None:
continue
action.apply(self)
self.update_fov() #update before the next action
def update_fov(self):
self.floor_map.visible[:] = compute_fov(
self.floor_map.tiles["transparent"],
(self.player.x, self.player.y),
radius = 8,
)
#add the visible tiles to the explored list
self.floor_map.explored |= self.floor_map.visible
def render(self, context: Context, console: Console) -> None:
self.floor_map.render(console)
for entity in self.entities:
if self.floor_map.visible[entity.x, entity.y]:
console.print(entity.x, entity.y, entity.char, fg=entity.color)
#send to the screen
context.present(console)
console.clear()