2021-04-29 17:15:29 +00:00
|
|
|
package uulm.teamname.marvelous.gamelibrary.gamelogic;
|
|
|
|
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.events.Event;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.requests.Request;
|
|
|
|
|
|
|
|
import java.util.ArrayDeque;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/** Represents manager for a game state. */
|
2021-04-29 17:15:29 +00:00
|
|
|
class GameStateManager {
|
2021-04-30 18:54:34 +00:00
|
|
|
/** The managed {@link GameState} */
|
2021-04-29 17:15:29 +00:00
|
|
|
private final GameState state;
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/** The queue of {@link Event}s to be applied during {@link Request} processing */
|
2021-04-29 17:15:29 +00:00
|
|
|
private final ArrayDeque<Event> queue = new ArrayDeque<Event>();
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new {@link GameStateManager}.
|
|
|
|
* @param state A reference to the state to be managed
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
|
|
|
public GameStateManager(GameState state) {
|
|
|
|
this.state = state;
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Checks a list of {@link Request}s for validity and optionally produces resulting {@link Event}s.
|
|
|
|
* @param requests The requests to check
|
|
|
|
* @param apply True if resulting events should be stored for later application
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
|
|
|
public boolean processRequests(Request[] requests, boolean apply) {
|
|
|
|
GameState snapshot = state.snapshot();
|
|
|
|
|
|
|
|
for(Request request: requests) {
|
|
|
|
if (GameLogic.checkRequest(snapshot, request)) {
|
2021-05-02 12:38:03 +00:00
|
|
|
ArrayList<Event> result = GameLogic.executeRequest(snapshot, request);
|
2021-04-29 17:15:29 +00:00
|
|
|
if(apply) {
|
|
|
|
queue.addAll(result);
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
queue.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-19 17:59:41 +00:00
|
|
|
/**
|
|
|
|
* Handles events that happen after a turn phase.
|
|
|
|
* @return The optionally resulting {@link Event}s
|
|
|
|
*/
|
2021-06-01 13:43:04 +00:00
|
|
|
public ArrayList<Event> checkPostPhase() {
|
|
|
|
return GameLogic.checkTurnEnd(state);
|
2021-05-19 17:59:41 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 19:37:18 +00:00
|
|
|
/**
|
|
|
|
* Initializes the game.
|
|
|
|
* @param selectedCharacters1 The characters selected by player 1
|
|
|
|
* @param selectedCharacters2 The characters selected by player 2
|
|
|
|
* @return The resulting {@link Event}s
|
|
|
|
*/
|
2021-06-01 13:43:04 +00:00
|
|
|
public ArrayList<Event> initGame(ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) {
|
|
|
|
return GameLogic.startGame(state, selectedCharacters1, selectedCharacters2);
|
2021-05-31 19:37:18 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 18:04:10 +00:00
|
|
|
/**
|
|
|
|
* Starts the game.
|
|
|
|
* @return The resulting {@link Event}s
|
|
|
|
*/
|
2021-06-01 13:43:04 +00:00
|
|
|
public ArrayList<Event> startGame() {
|
|
|
|
return GameLogic.handleRoundStart(state);
|
2021-05-19 18:04:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Applies an array of {@link Event}s to the game state.
|
|
|
|
* @param events The events to apply
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
2021-06-01 13:43:04 +00:00
|
|
|
public void applyEvents(ArrayList<Event> events) {
|
2021-04-29 17:15:29 +00:00
|
|
|
for(Event event: events) {
|
|
|
|
GameLogic.applyEvent(state, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Applies the result of the last processRequests call.
|
|
|
|
* @return A list of applied events
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
2021-06-01 13:43:04 +00:00
|
|
|
public ArrayList<Event> apply() {
|
2021-06-01 15:19:48 +00:00
|
|
|
ArrayList<Event> toReturn = new ArrayList<>(queue.size());
|
2021-04-29 17:15:29 +00:00
|
|
|
Event current;
|
|
|
|
while ((current = queue.poll()) != null) {
|
|
|
|
GameLogic.applyEvent(state, current);
|
2021-06-01 13:43:04 +00:00
|
|
|
toReturn.add(current);
|
2021-04-29 17:15:29 +00:00
|
|
|
}
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
}
|