107 lines
3.2 KiB
Java
107 lines
3.2 KiB
Java
package uulm.teamname.marvelous.gamelibrary.gamelogic;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
|
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.Observer;
|
|
|
|
/** 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;
|
|
|
|
/** 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(IntVector2 mapSize) {
|
|
_state = new GameState(mapSize);
|
|
this.state = new GameStateView(_state);
|
|
manager = new GameStateManager(_state);
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public boolean checkRequestsAndApply(Request... requests) {
|
|
if(manager.processRequests(requests, true)) {
|
|
emit(manager.apply());
|
|
|
|
Event[] result = manager.checkPostPhase();
|
|
if(result.length > 0) {
|
|
emit(result);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Checks an array of {@link Request}s for validity without applying it.
|
|
* @param requests The requests to check
|
|
* @return Whether or not the given set of requests is valid
|
|
*/
|
|
public boolean checkRequestsSilent(Request... requests) {
|
|
return manager.processRequests(requests, false);
|
|
}
|
|
|
|
/**
|
|
* Initializes and starts the game.
|
|
*/
|
|
public void startGame() {
|
|
emit(manager.startGame());
|
|
}
|
|
|
|
/**
|
|
* Produces a {@link EventType#GamestateEvent} for the current {@link GameState}.
|
|
* @return The resulting event
|
|
*/
|
|
public Event getGameStateEvent() {
|
|
return GameLogic.buildGameStateEvent(_state);
|
|
}
|
|
|
|
/**
|
|
* Restores the current {@link GameState} based on the given one.
|
|
* @param state The state to restore from
|
|
*/
|
|
public void setGameState(GameState state) {
|
|
_state.restore(state);
|
|
}
|
|
|
|
/**
|
|
* Applies an array of {@link Event}s to the game state.
|
|
* @param events The events to apply.
|
|
*/
|
|
public void applyEvents(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) {
|
|
emitter.update(events);
|
|
}
|
|
}
|