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 import entity_types 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.floor_map = floor_map #spawn the player object self.player = entity_types.player.clone(self.floor_map.procgen_cache["spawn_x"], self.floor_map.procgen_cache["spawn_y"]) self.floor_map.entities.add(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.player) self.handle_entities() self.update_fov() #update before the next action def handle_entities(self) -> None: for entity in self.floor_map.entities - {self.player}: pass #run entity AI 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) console.print(self.player.x, self.player.y, self.player.char, fg=self.player.color) #send to the screen context.present(console) console.clear()