Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event handler improvements #75

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading