feat: expose methods for game state event handling

This commit is contained in:
punchready 2021-05-18 14:14:23 +02:00
parent 2d33f150a8
commit 9fa5c554ef
4 changed files with 63 additions and 9 deletions

View File

@ -1,7 +1,6 @@
package uulm.teamname.marvelous.gamelibrary.entities;
import java.util.HashMap;
import java.util.Map;
/** Specifies the type of an {@link InfinityStone}. */
public enum StoneType {

View File

@ -2,6 +2,7 @@ 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.Observer;
@ -66,6 +67,22 @@ public class GameInstance {
return manager.processRequests(requests, false);
}
/**
* 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.

View File

@ -174,18 +174,27 @@ class GameLogic {
}
}
case Req -> {
result.add(new EventBuilder(EventType.GamestateEvent)
result.add(buildGameStateEvent(state));
}
}
return result;
}
/**
* Builds a {@link EventType#GamestateEvent} for the given {@link GameState}.
* @param state The game state to use
* @return The resulting event
*/
public static Event buildGameStateEvent(GameState state) {
return new EventBuilder(EventType.GamestateEvent)
.withEntities(state.entities.export())
.withTurnOrder((EntityID[])state.turnOrder.toArray())
.withMapSize(state.mapSize)
.withActiveCharacter(state.activeCharacter)
.withStoneCooldowns(state.stoneCooldown.export())
.withWinCondition(state.won)
.buildGameStateEvent());
}
}
return result;
.buildGameStateEvent();
}
/**

View File

@ -74,4 +74,33 @@ class GameState {
return clone;
}
/**
* Restores the state rom the given {@link GameState}. This is slow.
* @param state The state to restore from
*/
public void restore(GameState state) {
mapSize.set(state.mapSize);
entities.cloneFrom(state.entities);
roundNumber = state.roundNumber;
turnOrder = new ArrayList<>();
for(EntityID id: state.turnOrder) {
turnOrder.add(id.clone());
}
turnNumber = state.turnNumber;
activeCharacter = state.activeCharacter != null ? state.activeCharacter.clone() : null;
won = state.won;
stoneCooldown.cloneFrom(state.stoneCooldown);
for(Tuple<ParticipantType, WinCondition> condition: state.winConditions.keySet()) {
winConditions.put(condition, state.winConditions.get(condition));
}
}
}