131 lines
4.4 KiB
Java
131 lines
4.4 KiB
Java
package uulm.teamname.marvelous.gamelibrary.gamelogic;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.ArrayTools;
|
|
import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
|
|
import uulm.teamname.marvelous.gamelibrary.config.PartyConfig;
|
|
import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig;
|
|
import uulm.teamname.marvelous.gamelibrary.events.Event;
|
|
import uulm.teamname.marvelous.gamelibrary.events.EventType;
|
|
import uulm.teamname.marvelous.gamelibrary.requests.Request;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
|
|
/** Represents a game instance. */
|
|
public class GameInstance {
|
|
/** The private {@link GameState} of the instance */
|
|
private final GameState _state;
|
|
|
|
/** The public view for the underlying {@link GameState} of the instance */
|
|
public final GameStateView state;
|
|
|
|
/** The {@link GameStateManager} managing the {@link GameState} of the instance */
|
|
private final GameStateManager manager;
|
|
|
|
/** Constructs a new {@link GameInstance}. */
|
|
public GameInstance(PartyConfig partyConfig, CharacterConfig characterConfig, ScenarioConfig scenarioConfig) {
|
|
_state = new GameState(partyConfig, characterConfig, scenarioConfig);
|
|
state = new GameStateView(_state);
|
|
manager = new GameStateManager(_state);
|
|
}
|
|
|
|
/**
|
|
* See {@link GameInstance#checkRequestsAndApply(ArrayList)}.
|
|
*/
|
|
public Optional<List<Event>> checkRequestsAndApply(Request... requests) {
|
|
return checkRequestsAndApply(ArrayTools.toArrayList(requests));
|
|
}
|
|
|
|
/**
|
|
* Checks an array of {@link Request Requests} for validity and automatically applies them if valid.
|
|
* @param requests The requests to check
|
|
* @return The list of resulting {@link Event Events} or {@link Optional#empty()} if the check failed
|
|
*/
|
|
public Optional<List<Event>> checkRequestsAndApply(ArrayList<Request> requests) {
|
|
if(manager.processRequests(requests, true)) {
|
|
ArrayList<Event> result = manager.apply();
|
|
|
|
result.addAll(manager.checkPostPhase());
|
|
|
|
return Optional.of(result);
|
|
}
|
|
|
|
return Optional.empty();
|
|
}
|
|
|
|
/**
|
|
* See {@link GameInstance#checkRequests(ArrayList)}.
|
|
*/
|
|
public boolean checkRequests(Request... requests) {
|
|
return checkRequests(ArrayTools.toArrayList(requests));
|
|
}
|
|
|
|
/**
|
|
* Checks an array of {@link Request Requests} for validity without applying it.
|
|
* @param requests The requests to check
|
|
* @return Whether or not the given set of requests is valid
|
|
*/
|
|
public boolean checkRequests(ArrayList<Request> requests) {
|
|
return manager.processRequests(requests, false);
|
|
}
|
|
|
|
/**
|
|
* See {@link GameInstance#applyEvents(List)}.
|
|
*/
|
|
public void applyEvents(Event... events) {
|
|
manager.applyEvents(events);
|
|
}
|
|
|
|
/**
|
|
* Applies an array of {@link Event Events} to the game state.
|
|
* @param events The events to apply.
|
|
*/
|
|
public void applyEvents(List<Event> events) {
|
|
manager.applyEvents(events);
|
|
}
|
|
|
|
/**
|
|
* 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 Events}
|
|
*/
|
|
public List<Event> startGame(List<Integer> selectedCharacters1, List<Integer> selectedCharacters2) {
|
|
return manager.startGame(selectedCharacters1, selectedCharacters2);
|
|
}
|
|
|
|
/**
|
|
* Forcefully ends the current turn.
|
|
* @return The list of resulting {@link Event Events}
|
|
*/
|
|
public ArrayList<Event> endTurn() {
|
|
return manager.endTurn();
|
|
}
|
|
|
|
/**
|
|
* Produces a {@link EventType#GamestateEvent} for the current {@link GameState}.
|
|
* @return The resulting event
|
|
*/
|
|
public Event getGameStateEvent() {
|
|
return manager.getGameStateEvent();
|
|
}
|
|
|
|
/**
|
|
* Restores the current {@link GameState} based on the given {@link EventType#GamestateEvent}.
|
|
* @param gameStateEvent The event to restore from
|
|
*/
|
|
public void applyGameStateEvent(Event gameStateEvent) {
|
|
manager.applyEvent(gameStateEvent);
|
|
}
|
|
|
|
public GameState getGameStateUnsafe() {
|
|
return _state;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return _state.toString();
|
|
}
|
|
}
|