Gamelib/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java

92 lines
2.8 KiB
Java
Raw Normal View History

package uulm.teamname.marvelous.gamelibrary.gamelogic;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
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 a checksum with the current one.
* @param input The checksum to compare to.
* @return Whether or not the checksum matches.
*/
public boolean checkChecksum(long input) {
return ChecksumCalculator.checkChecksum(_state, input);
}
/** Calculates the current checksum of the game state.
* @return The calculated checksum.
*/
public long calculateChecksum() {
return ChecksumCalculator.calculateChecksum(_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());
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);
}
/** 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);
}
/** Emits an array of {@link Event}s.
* @param events The events to emit.
*/
private void emit(Event... events) {
emitter.notifyObservers(events);
}
}