Skip to content

Commit

Permalink
Merge branch 'master' into refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
strakam authored Oct 3, 2024
2 parents 3aafe7f + 9af3228 commit c3ef39b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 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
2 changes: 1 addition & 1 deletion generals/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def tick(self, fps=None) -> Command:
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 c3ef39b

Please sign in to comment.