breaking: remove observable pattern, make methods return events directly
This commit is contained in:
@ -8,7 +8,6 @@ import uulm.teamname.marvelous.gamelibrary.json.config.ScenarioConfig;
|
||||
import uulm.teamname.marvelous.gamelibrary.requests.Request;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Observer;
|
||||
|
||||
/** Represents a game instance. */
|
||||
public class GameInstance {
|
||||
@ -21,9 +20,6 @@ public class GameInstance {
|
||||
/** The {@link GameStateManager} managing the {@link GameState} of the instance */
|
||||
private final GameStateManager manager;
|
||||
|
||||
/** The {@link EventEmitter} for {@link Event}s resulting from {@link Request}s */
|
||||
private final EventEmitter emitter = new EventEmitter();
|
||||
|
||||
/** Constructs a new {@link GameInstance}. */
|
||||
public GameInstance(PartyConfig partyConfig, CharacterConfig characterConfig, ScenarioConfig scenarioConfig) {
|
||||
_state = new GameState(partyConfig, characterConfig, scenarioConfig);
|
||||
@ -34,20 +30,16 @@ public class GameInstance {
|
||||
/**
|
||||
* Checks an array of {@link Request}s for validity and automatically applies them if valid.
|
||||
* @param requests The requests to check
|
||||
* @return Whether or not the given set of requests was valid
|
||||
* @return The list of resulting {@link Event}s or `null` if the check failed
|
||||
*/
|
||||
public boolean checkRequestsAndApply(Request... requests) {
|
||||
public ArrayList<Event> checkRequestsAndApply(Request... requests) {
|
||||
if(manager.processRequests(requests, true)) {
|
||||
emit(manager.apply());
|
||||
|
||||
Event[] result = manager.checkPostPhase();
|
||||
if(result.length > 0) {
|
||||
emit(result);
|
||||
}
|
||||
|
||||
return true;
|
||||
ArrayList<Event> result = manager.apply();
|
||||
result.addAll(manager.checkPostPhase());
|
||||
return result;
|
||||
}
|
||||
return false;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,7 +47,7 @@ public class GameInstance {
|
||||
* @param requests The requests to check
|
||||
* @return Whether or not the given set of requests is valid
|
||||
*/
|
||||
public boolean checkRequestsSilent(Request... requests) {
|
||||
public boolean checkRequests(Request... requests) {
|
||||
return manager.processRequests(requests, false);
|
||||
}
|
||||
|
||||
@ -63,10 +55,14 @@ public class GameInstance {
|
||||
* Initializes and starts the game. Selected characters are given as a list of indices from the {@link CharacterConfig#characters} array.
|
||||
* @param selectedCharacters1 The characters selected by player 1
|
||||
* @param selectedCharacters2 The characters selected by player 2
|
||||
* @return The list of resulting {@link Event}s
|
||||
*/
|
||||
public void startGame(ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) {
|
||||
emit(manager.initGame(selectedCharacters1, selectedCharacters2));
|
||||
emit(manager.startGame());
|
||||
public ArrayList<Event> startGame(ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) {
|
||||
ArrayList<Event> result = manager.initGame(selectedCharacters1, selectedCharacters2);
|
||||
manager.applyEvents(result);
|
||||
result.addAll(manager.startGame());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -89,24 +85,7 @@ public class GameInstance {
|
||||
* Applies an array of {@link Event}s to the game state.
|
||||
* @param events The events to apply.
|
||||
*/
|
||||
public void applyEvents(Event... events) {
|
||||
public void applyEvents(ArrayList<Event> events) {
|
||||
manager.applyEvents(events);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an {@link Observer} for events.
|
||||
* @param observer The observer to add
|
||||
*/
|
||||
public void addObserver(Observer observer) {
|
||||
emitter.addObserver(observer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instructs the emitter to emit an array of {@link Event}s.
|
||||
* @param events The events to emit
|
||||
*/
|
||||
private void emit(Event... events) {
|
||||
manager.applyEvents(events);
|
||||
emitter.update(events);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user