Gamelib/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java

112 lines
3.3 KiB
Java
Raw Normal View History

package uulm.teamname.marvelous.gamelibrary.gamelogic;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.*;
import uulm.teamname.marvelous.gamelibrary.Tuple;
import java.util.ArrayList;
2021-05-27 15:08:08 +00:00
import java.util.Arrays;
import java.util.HashMap;
2021-05-27 15:08:08 +00:00
import java.util.HashSet;
/** Represents the state of a game instance. */
class GameState {
/** The size of the map */
public final IntVector2 mapSize;
/** The list of {@link Entity}s inside the game */
public final EntityManager entities = new EntityManager();
2021-05-27 15:08:08 +00:00
/** The set of stones that are yet to be placed on the map */
public HashSet<StoneType> unvomitedStones = new HashSet<>(Arrays.asList(StoneType.values()));
/** The total amount of full turn cycles that occurred */
public int roundNumber = 0;
/** The turn order of every character */
public ArrayList<EntityID> turnOrder = new ArrayList<>();
/** The total amount of turns that occurred */
public int turnNumber = 0;
/** The {@link EntityID} of the active character */
public EntityID activeCharacter = null;
/** Whether or not the game was won */
public boolean won = false;
/** The global cooldown of every infinity stone */
public final StoneCooldownManager stoneCooldown = new StoneCooldownManager();
/** The store of the {@link WinCondition} data for every win condition for each player */
public final HashMap<Tuple<EntityType, WinCondition>, Integer> winConditions = new HashMap<>();
/**
* Constructs a new {@link GameState}.
* @param mapSize The size of the map
*/
public GameState(IntVector2 mapSize) {
this.mapSize = mapSize;
}
/**
* Clones the state into a new {@link GameState} object. This is slow.
* @return The cloned game state
*/
public GameState snapshot() {
GameState clone = new GameState(this.mapSize);
clone.entities.cloneFrom(entities);
clone.roundNumber = roundNumber;
clone.turnOrder = new ArrayList<>();
for(EntityID id: turnOrder) {
clone.turnOrder.add(id.clone());
}
clone.turnNumber = turnNumber;
clone.activeCharacter = activeCharacter != null ? activeCharacter.clone() : null;
clone.won = won;
clone.stoneCooldown.cloneFrom(stoneCooldown);
for(Tuple<EntityType, WinCondition> condition: winConditions.keySet()) {
clone.winConditions.put(condition, winConditions.get(condition));
}
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<EntityType, WinCondition> condition: state.winConditions.keySet()) {
winConditions.put(condition, state.winConditions.get(condition));
}
}
}