feat: expose methods for game state event handling
This commit is contained in:
		| @ -1,7 +1,6 @@ | |||||||
| package uulm.teamname.marvelous.gamelibrary.entities; | package uulm.teamname.marvelous.gamelibrary.entities; | ||||||
|  |  | ||||||
| import java.util.HashMap; | import java.util.HashMap; | ||||||
| import java.util.Map; |  | ||||||
|  |  | ||||||
| /** Specifies the type of an {@link InfinityStone}. */ | /** Specifies the type of an {@link InfinityStone}. */ | ||||||
| public enum StoneType { | public enum StoneType { | ||||||
|  | |||||||
| @ -2,6 +2,7 @@ package uulm.teamname.marvelous.gamelibrary.gamelogic; | |||||||
|  |  | ||||||
| import uulm.teamname.marvelous.gamelibrary.IntVector2; | import uulm.teamname.marvelous.gamelibrary.IntVector2; | ||||||
| import uulm.teamname.marvelous.gamelibrary.events.Event; | import uulm.teamname.marvelous.gamelibrary.events.Event; | ||||||
|  | import uulm.teamname.marvelous.gamelibrary.events.EventType; | ||||||
| import uulm.teamname.marvelous.gamelibrary.requests.Request; | import uulm.teamname.marvelous.gamelibrary.requests.Request; | ||||||
|  |  | ||||||
| import java.util.Observer; | import java.util.Observer; | ||||||
| @ -66,6 +67,22 @@ public class GameInstance { | |||||||
|         return manager.processRequests(requests, false); |         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. |      * Applies an array of {@link Event}s to the game state. | ||||||
|      * @param events The events to apply. |      * @param events The events to apply. | ||||||
|  | |||||||
| @ -174,20 +174,29 @@ class GameLogic { | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             case Req -> { |             case Req -> { | ||||||
|                 result.add(new EventBuilder(EventType.GamestateEvent) |                 result.add(buildGameStateEvent(state)); | ||||||
|                         .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; |         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}. |      * Checks a {@link Request} for validity for a {@link GameState}. | ||||||
|      * @param state The game state to check on |      * @param state The game state to check on | ||||||
|  | |||||||
| @ -74,4 +74,33 @@ class GameState { | |||||||
|  |  | ||||||
|         return clone; |         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)); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user