From 9fa5c554ef23bd818f221739fc26873a2ad71ce9 Mon Sep 17 00:00:00 2001 From: punchready Date: Tue, 18 May 2021 14:14:23 +0200 Subject: [PATCH] feat: expose methods for game state event handling --- .../gamelibrary/entities/StoneType.java | 1 - .../gamelibrary/gamelogic/GameInstance.java | 17 +++++++++++ .../gamelibrary/gamelogic/GameLogic.java | 25 +++++++++++----- .../gamelibrary/gamelogic/GameState.java | 29 +++++++++++++++++++ 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/StoneType.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/StoneType.java index f3c042d..2a30b89 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/StoneType.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/StoneType.java @@ -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 { diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java index 13c0d04..a1d7ee6 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java @@ -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. diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java index c88a035..22946e3 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -174,20 +174,29 @@ class GameLogic { } } case Req -> { - result.add(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()); + 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(); + } + /** * Checks a {@link Request} for validity for a {@link GameState}. * @param state The game state to check on diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java index b7e3a53..533ba93 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java @@ -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 condition: state.winConditions.keySet()) { + winConditions.put(condition, state.winConditions.get(condition)); + } + } }