diff --git a/generals/agents/agent.py b/generals/agents/agent.py index 84453ad..06842d3 100644 --- a/generals/agents/agent.py +++ b/generals/agents/agent.py @@ -1,7 +1,7 @@ -from abc import abstractmethod +from abc import ABC, abstractmethod -class Agent: +class Agent(ABC): """ Base class for all agents. """ diff --git a/generals/gui/event_handler.py b/generals/gui/event_handler.py index 4c6d1f1..0fb29e2 100644 --- a/generals/gui/event_handler.py +++ b/generals/gui/event_handler.py @@ -1,7 +1,7 @@ import pygame from enum import Enum from pygame.event import Event -from abc import abstractmethod +from abc import ABC, abstractmethod from .properties import Properties from generals.core import config as c @@ -45,7 +45,7 @@ def __init__(self): super().__init__() -class EventHandler: +class EventHandler(ABC): def __init__(self, properties: Properties): """ Initialize the event handler. @@ -56,6 +56,11 @@ def __init__(self, properties: Properties): self.properties = properties self.mode = properties.mode + @property + @abstractmethod + def command(self) -> Command: + raise NotImplementedError + def handle_events(self) -> Command: """ Handle pygame GUI events @@ -105,7 +110,11 @@ def handle_mouse_event(self): class ReplayEventHandler(EventHandler): def __init__(self, properties: Properties): super().__init__(properties) - self.command = ReplayCommand() + self._command = ReplayCommand() + + @property + def command(self) -> ReplayCommand: + return self._command def handle_key_event(self, event: Event) -> ReplayCommand: match event.key: @@ -135,7 +144,11 @@ def handle_mouse_event(self) -> None: class GameEventHandler(EventHandler): def __init__(self, properties: Properties): super().__init__(properties) - self.command = GameCommand() + self._command = GameCommand() + + @property + def command(self) -> GameCommand: + return self._command def handle_key_event(self, event: Event) -> GameCommand: raise NotImplementedError @@ -147,7 +160,11 @@ def handle_mouse_event(self) -> None: class TrainEventHandler(EventHandler): def __init__(self, properties: Properties): super().__init__(properties) - self.command = TrainCommand() + self._command = TrainCommand() + + @property + def command(self) -> TrainCommand: + return self._command def handle_key_event(self, event: Event) -> TrainCommand: if event.key == Keybindings.Q.value: diff --git a/generals/gui/gui.py b/generals/gui/gui.py index 531fda8..40e3c92 100644 --- a/generals/gui/gui.py +++ b/generals/gui/gui.py @@ -3,7 +3,7 @@ from generals.core.game import Game from .properties import Properties -from .event_handler import TrainEventHandler, GameEventHandler, ReplayEventHandler +from .event_handler import TrainEventHandler, GameEventHandler, ReplayEventHandler, ReplayCommand from .rendering import Renderer @@ -37,7 +37,7 @@ def tick(self, fps=None): command = handler.handle_events() if command.quit: quit() - if self.properties.mode == "replay": + if isinstance(command, ReplayCommand): self.properties.update_speed(command.speed_change) if command.frame_change != 0 or command.restart: self.properties.paused = True