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

172 lines
5.9 KiB
Java

package uulm.teamname.marvelous.gamelibrary.gamelogic;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
import uulm.teamname.marvelous.gamelibrary.config.PartyConfig;
import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig;
import uulm.teamname.marvelous.gamelibrary.entities.Character;
import uulm.teamname.marvelous.gamelibrary.entities.Entity;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
import uulm.teamname.marvelous.gamelibrary.entities.StoneType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
/** Represents the state of a game instance. */
public class GameState {
/** The size of the map */
public final IntVector2 mapSize;
public final PartyConfig partyConfig;
public final CharacterConfig characterConfig;
public final ScenarioConfig scenarioConfig;
/** The list of {@link Entity Entities} inside the game */
public final EntityManager entities = new EntityManager();
/** 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;
/** The data for every win condition for each player */
public final WinConditionManager winConditions = new WinConditionManager();
/**
* Constructs a new {@link GameState} with the given config.
*/
public GameState(PartyConfig partyConfig, CharacterConfig characterConfig, ScenarioConfig scenarioConfig) {
this.mapSize = new IntVector2(scenarioConfig.scenario[0].length, scenarioConfig.scenario.length);
this.partyConfig = partyConfig;
this.characterConfig = characterConfig;
this.scenarioConfig = scenarioConfig;
stoneCooldown = new StoneCooldownManager(partyConfig);
}
/**
* Clones the state into a new {@link GameState} object. This is slow.
* @return The cloned game state
*/
public GameState snapshot() {
GameState clone = new GameState(partyConfig, characterConfig, scenarioConfig);
clone.entities.cloneFrom(entities);
clone.unvomitedStones = new HashSet<>(unvomitedStones);
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);
clone.winConditions.cloneFrom(winConditions);
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);
unvomitedStones = new HashSet<>(state.unvomitedStones);
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);
winConditions.cloneFrom(state.winConditions);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for(int y = 0; y < mapSize.getY(); y++) {
for(int x = 0; x < mapSize.getX(); x++) {
ArrayList<Entity> entities = this.entities.findByPosition(new IntVector2(x, y));
if(entities.isEmpty()) {
sb.append(". ");
}else {
switch(entities.get(0).id.type) {
case NPC -> {
switch(entities.get(0).id.id) {
case 0 -> {
sb.append("G");
}
case 1 -> {
sb.append("S");
}
case 2 -> {
sb.append("T");
}
}
sb.append(entities.get(0).id.equals(activeCharacter) ? "'" : " ");
}
case P1, P2 -> {
if(((Character)entities.get(0)).isAlive()) {
sb.append("X");
}else {
sb.append("x");
}
sb.append(entities.get(0).id.equals(activeCharacter) ? "'" : " ");
}
case Rocks -> {
sb.append("M ");
}
case Portals -> {
sb.append("O ");
}
case InfinityStones -> {
sb.append("# ");
}
}
}
}
sb.append("\n");
}
return sb.toString();
}
}