2021-04-29 17:15:29 +00:00
|
|
|
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;
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/** Represents a game instance. */
|
2021-04-29 17:15:29 +00:00
|
|
|
public class GameInstance {
|
2021-04-30 18:54:34 +00:00
|
|
|
/** The private {@link GameState} of the instance */
|
2021-04-29 17:15:29 +00:00
|
|
|
private final GameState _state;
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/** The public view for the underlying {@link GameState} of the instance */
|
2021-04-29 17:15:29 +00:00
|
|
|
public final GameStateView state;
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/** The {@link GameStateManager} managing the {@link GameState} of the instance */
|
2021-04-29 17:15:29 +00:00
|
|
|
private final GameStateManager manager;
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/** The {@link EventEmitter} for {@link Event}s resulting from {@link Request}s */
|
2021-04-29 17:15:29 +00:00
|
|
|
private final EventEmitter emitter = new EventEmitter();
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/** Constructs a new {@link GameInstance}. */
|
2021-04-29 17:15:29 +00:00
|
|
|
public GameInstance(IntVector2 mapSize) {
|
|
|
|
_state = new GameState(mapSize);
|
|
|
|
this.state = new GameStateView(_state);
|
|
|
|
manager = new GameStateManager(_state);
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Checks a checksum with the current one.
|
|
|
|
* @param input The checksum to compare to
|
|
|
|
* @return Whether or not the checksum matches
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
|
|
|
public boolean checkChecksum(long input) {
|
|
|
|
return ChecksumCalculator.checkChecksum(_state, input);
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Calculates the current checksum of the game state.
|
|
|
|
* @return The calculated checksum
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
|
|
|
public long calculateChecksum() {
|
|
|
|
return ChecksumCalculator.calculateChecksum(_state);
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
|
|
|
public boolean checkRequestsAndApply(Request... requests) {
|
|
|
|
if(manager.processRequests(requests, true)) {
|
|
|
|
emit(manager.apply());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
|
|
|
public boolean checkRequestsSilent(Request... requests) {
|
|
|
|
return manager.processRequests(requests, false);
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Applies an array of {@link Event}s to the game state.
|
2021-04-29 17:15:29 +00:00
|
|
|
* @param events The events to apply.
|
|
|
|
*/
|
|
|
|
public void applyEvents(Event... events) {
|
|
|
|
manager.applyEvents(events);
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Adds an {@link Observer} for events.
|
|
|
|
* @param observer The observer to add
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
|
|
|
public void addObserver(Observer observer) {
|
|
|
|
emitter.addObserver(observer);
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
2021-05-03 18:19:17 +00:00
|
|
|
* Instructs the emitter to emit an array of {@link Event}s.
|
2021-04-30 18:54:34 +00:00
|
|
|
* @param events The events to emit
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
|
|
|
private void emit(Event... events) {
|
2021-05-03 18:19:17 +00:00
|
|
|
emitter.update(events);
|
2021-04-29 17:15:29 +00:00
|
|
|
}
|
|
|
|
}
|