Skip to content

Commit

Permalink
Merge pull request #75 from Puckoland/eventHandlerImprovements
Browse files Browse the repository at this point in the history
Event handler improvements
  • Loading branch information
strakam authored Oct 3, 2024
2 parents 822092f + f1991fb commit 9af3228
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions generals/agents/agent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import abstractmethod
from abc import ABC, abstractmethod


class Agent:
class Agent(ABC):
"""
Base class for all agents.
"""
Expand Down
27 changes: 22 additions & 5 deletions generals/gui/event_handler.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -45,7 +45,7 @@ def __init__(self):
super().__init__()


class EventHandler:
class EventHandler(ABC):
def __init__(self, properties: Properties):
"""
Initialize the event handler.
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions generals/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9af3228

Please sign in to comment.