From 9405b89fb39f539c0ad86ca2f37fd3add1f8da54 Mon Sep 17 00:00:00 2001 From: punchready Date: Mon, 3 May 2021 20:19:17 +0200 Subject: [PATCH] fix: resolve event emitter not emitting because of missing call to setChanged --- .../gamelibrary/gamelogic/EventEmitter.java | 11 +++++++++- .../gamelibrary/gamelogic/EventObserver.java | 22 +++++++++++++++++++ .../gamelibrary/gamelogic/GameInstance.java | 4 ++-- .../gamelibrary/gamelogic/GameLogic.java | 1 - .../gamelibrary/gamelogic/GameStateView.java | 2 +- 5 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EventObserver.java diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EventEmitter.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EventEmitter.java index fdf8872..0a2efb9 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EventEmitter.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EventEmitter.java @@ -1,8 +1,17 @@ package uulm.teamname.marvelous.gamelibrary.gamelogic; +import uulm.teamname.marvelous.gamelibrary.events.Event; + import java.util.Observable; /** Represents an event emitter for game events fired by a game instance. */ class EventEmitter extends Observable { - + /** + * Emits an array of {@link Event}s. This method is necessary because {@link Observable#setChanged} is protected. + * @param events The events to emit + */ + public void update(Event... events) { + setChanged(); + notifyObservers(events); + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EventObserver.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EventObserver.java new file mode 100644 index 0000000..369c972 --- /dev/null +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EventObserver.java @@ -0,0 +1,22 @@ +package uulm.teamname.marvelous.gamelibrary.gamelogic; + +import uulm.teamname.marvelous.gamelibrary.events.Event; + +import java.util.Observable; +import java.util.Observer; + +/** Represents an event observer for game events emitted by an {@link EventEmitter}. */ +public class EventObserver implements Observer { + @Override + public void update(Observable o, Object arg) { + handle((Event)arg); + } + + /** + * Gets called with incoming {@link Event}s. Override this method to handle events. + * @param event The event that got emitted + */ + protected void handle(Event event) { + + } +} diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java index a27845f..13c0d04 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java @@ -83,10 +83,10 @@ public class GameInstance { } /** - * Emits an array of {@link Event}s. + * Instructs the emitter to emit an array of {@link Event}s. * @param events The events to emit */ private void emit(Event... events) { - emitter.notifyObservers(events); + emitter.update(events); } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java index d2f41e5..21d303e 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -28,7 +28,6 @@ class GameLogic { public static ArrayList executeRequest(GameState state, Request request) { ArrayList result = new ArrayList<>(); - //TODO: refactor for EventBuilder in GameLogic.executeRequest switch(request.type) { case MeleeAttackRequest, RangedAttackRequest -> { CharacterRequest data = (CharacterRequest)request; diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateView.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateView.java index f152e1e..5ecd370 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateView.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateView.java @@ -14,7 +14,7 @@ public class GameStateView { /** * Constructs a new {@link GameStateView}. - * @param state A reference to the state to be viewable + * @param state A reference to the {@link GameState} to be viewable */ public GameStateView(GameState state) { this.state = state;