-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.py
57 lines (43 loc) · 1.92 KB
/
engine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from typing import Set, Iterable, Any
from tcod.context import Context
from tcod.console import Console
from tcod.map import compute_fov
from actions import EscapeAction, MovementAction
from entity import Entity
from game_map import GameMap
from input_handlers import EventHandler
class Engine:
def __init__(self, entities: Set[Entity], event_handler: EventHandler, game_map: GameMap, player: Entity):
self.entities = entities
self.event_handler = event_handler
self.game_map = game_map
self.player = player
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
if isinstance(action, MovementAction):
if self.game_map.tiles["walkable"][self.player.x + action.dx, self.player.y + action.dy]:
self.player.move(dx=action.dx, dy=action.dy)
elif isinstance(action, EscapeAction):
raise SystemExit()
self.update_fov() # Update the FOV before the players next action.
def update_fov(self) -> None:
"""Recompute the visible area based on the players 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
def render(self, console: Console, context: Context) -> None:
self.game_map.render(console)
for entity in self.entities:
# Only print entities that are in the FOV
if self.game_map.visible[entity.x, entity.y]:
console.print(entity.x, entity.y, entity.char, fg=entity.color)
context.present(console)
console.clear()