2022-01-06 11:28:03 -05:00
|
|
|
from typing import Set, Iterable, Any
|
|
|
|
|
|
|
|
from tcod.context import Context
|
|
|
|
from tcod.console import Console
|
|
|
|
|
|
|
|
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__(
|
|
|
|
self,
|
|
|
|
entities: Set[Entity],
|
|
|
|
event_handler: EventHandler,
|
|
|
|
game_map: GameMap,
|
|
|
|
player: Entity
|
|
|
|
):
|
2022-01-06 11:28:03 -05:00
|
|
|
self.entities = entities
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
for entity in self.entities:
|
|
|
|
console.print(entity.x, entity.y, entity.char, fg=entity.color)
|
|
|
|
|
|
|
|
# Actually output to screen
|
|
|
|
context.present(console)
|
|
|
|
console.clear()
|