fix: resolve event emitter not emitting because of missing call to setChanged

This commit is contained in:
punchready 2021-05-03 20:19:17 +02:00
parent a5ccba0326
commit 9405b89fb3
5 changed files with 35 additions and 5 deletions

View File

@ -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);
}
}

View File

@ -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) {
}
}

View File

@ -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);
}
}

View File

@ -28,7 +28,6 @@ class GameLogic {
public static ArrayList<Event> executeRequest(GameState state, Request request) {
ArrayList<Event> result = new ArrayList<>();
//TODO: refactor for EventBuilder in GameLogic.executeRequest
switch(request.type) {
case MeleeAttackRequest, RangedAttackRequest -> {
CharacterRequest data = (CharacterRequest)request;

View File

@ -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;