Merge branch 'gamelib' of ssh://gitlab.informatik.uni-ulm.de/sopra/ws20-marvelous-mashup/teams/team25 into gamelib

This commit is contained in:
Yannik Bretschneider 2021-05-18 14:19:23 +02:00
commit 2aefddf979
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,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

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));
}
}
}