1
0

Prevent playing after Game over, completing part 6

This commit is contained in:
Timothy Warren 2022-01-10 14:22:33 -05:00
parent c3a906e2b3
commit 2d073a3839
3 changed files with 36 additions and 3 deletions

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from components.base_component import BaseComponent
from input_handlers import GameOverEventHandler
from render_order import RenderOrder
if TYPE_CHECKING:
@ -31,6 +32,7 @@ class Fighter(BaseComponent):
def die(self) -> None:
if self.engine.player is self.entity:
death_message = "You died!"
self.engine.event_handler = GameOverEventHandler(self.engine)
else:
death_message = f"{self.entity.name} is dead!"

View File

@ -6,7 +6,7 @@ from tcod.context import Context
from tcod.console import Console
from tcod.map import compute_fov
from input_handlers import EventHandler
from input_handlers import MainGameEventHandler
if TYPE_CHECKING:
from entity import Actor
@ -17,7 +17,7 @@ class Engine:
game_map: GameMap
def __init__(self, player: Actor):
self.event_handler: EventHandler = EventHandler(self)
self.event_handler: EventHandler = MainGameEventHandler(self)
self.player = player
def handle_enemy_turns(self) -> None:

View File

@ -1,6 +1,6 @@
from __future__ import annotations
from typing import Optional, TYPE_CHECKING
from typing import overload, Optional, TYPE_CHECKING
import tcod.event
@ -52,6 +52,15 @@ class EventHandler(tcod.event.EventDispatch[Action]):
def __init__(self, engine: Engine):
self.engine = engine
@overload
def handle_events(self) -> None:
pass
def ev_quit(self, event: tcod.event.Quit) -> Optional[Action]:
raise SystemExit()
class MainGameEventHandler(EventHandler):
def handle_events(self) -> None:
for event in tcod.event.wait():
action = self.dispatch(event)
@ -82,3 +91,25 @@ class EventHandler(tcod.event.EventDispatch[Action]):
# No valid key was pressed
return action
class GameOverEventHandler(EventHandler):
def handle_events(self) -> None:
for event in tcod.event.wait():
action = self.dispatch(event)
if action is None:
continue
action.perform()
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[Action]:
action: Optional[Action] = None
key = event.sym
if key == tcod.event.K_ESCAPE:
action = EscapeAction(self.engine.player)
# No valid key was pressed
return action