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;
|
2021-05-18 12:14:23 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.events.EventType;
|
2021-04-29 17:15:29 +00:00
|
|
|
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 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-05-18 12:14:23 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|