feat: implement GameState.snapshot and make entities cloneable

This commit is contained in:
punchready 2021-04-30 21:48:28 +02:00
parent 7f225b850c
commit eccac70656
8 changed files with 82 additions and 8 deletions

View File

@ -58,4 +58,16 @@ public class Character extends Entity {
public boolean isAlive() {
return hp.getValue() > 0;
}
@Override
public Character clone() {
Character clone = new Character(id, position, name, hp.max, mp.max, ap.max, attackRange, rangedDamage, meleeDamage);
for(StoneType stone: inventory) {
clone.inventory.addStone(stone);
}
clone.hp.setValue(hp.getValue());
clone.mp.setValue(mp.getValue());
clone.ap.setValue(ap.getValue());
return clone;
}
}

View File

@ -5,10 +5,10 @@ import uulm.teamname.marvelous.gamelibrary.IntVector2;
/** Represents an abstract entity. */
public abstract class Entity {
/** Whether or not the entity is currently active in the game */
private boolean active = true;
protected boolean active = true;
/** The position of the entity */
private IntVector2 position;
protected IntVector2 position;
/** The {@link EntityID} of the entity */
public final EntityID id;
@ -23,6 +23,12 @@ public abstract class Entity {
this.id = id;
}
/**
* Clones the {@link Entity}.
* @return A new entity cloned from this one
*/
public abstract Entity clone();
public boolean isActive() {
return active;
}

View File

@ -27,6 +27,14 @@ public class EntityID {
return type == other;
}
/**
* Clones this entity id.
* @return The cloned {@link EntityID}
*/
public EntityID clone() {
return new EntityID(this.id, this.type);
}
/**
* Serializes the id for debugging.
* @return A debug string containing all the necessary information about the id

View File

@ -17,4 +17,9 @@ public class InfinityStone extends Entity {
super(id, position);
this.type = type;
}
@Override
public InfinityStone clone() {
return new InfinityStone(id, position, type);
}
}

View File

@ -21,7 +21,7 @@ public class Inventory implements Iterable<StoneType> {
throw new IllegalArgumentException("Attempted to construct an inventory with more than "+size+" initial stones.");
}
for(StoneType stone : content) {
for(StoneType stone: content) {
if(content.contains(stone)) {
throw new IllegalArgumentException("Attempted to construct an inventory with duplicate entries.");
}

View File

@ -29,4 +29,13 @@ public class NPC extends Entity {
super(id, position);
this.inventory = new Inventory();
}
@Override
public NPC clone() {
NPC clone = new NPC(id, position);
for(StoneType stone: inventory) {
clone.inventory.addStone(stone);
}
return clone;
}
}

View File

@ -29,4 +29,11 @@ public class Rock extends Entity {
public void decreaseHp(int damage) {
this.hp -= damage;
}
@Override
public Rock clone() {
Rock clone = new Rock(id, position, maxHP);
clone.hp = hp;
return clone;
}
}

View File

@ -9,6 +9,7 @@ import uulm.teamname.marvelous.gamelibrary.Tuple;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
/** Represents the state of a game instance. */
class GameState {
@ -22,13 +23,13 @@ class GameState {
public int roundNumber = 0;
/** The turn order of every character */
public ArrayList<EntityID> turnOrder;
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;
public EntityID activeCharacter = null;
/** Whether or not the game was won */
public boolean won = false;
@ -48,11 +49,37 @@ class GameState {
}
/**
* Clones the state into a new {@link GameState} object.
* Clones the state into a new {@link GameState} object. This is slow.
* @return The cloned game state
*/
public GameState snapshot() {
//TODO: implement GameState.snapshot
return this;
GameState clone = new GameState(this.mapSize);
for(Iterator<Entity> it = this.entities.getEntities(); it.hasNext(); ) {
clone.entities.addEntity(it.next().clone());
}
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;
for(StoneType type: stoneCooldown.keySet()) {
clone.stoneCooldown.put(type, stoneCooldown.get(type));
}
for(Tuple<ParticipantType, WinCondition> condition: winConditions.keySet()) {
clone.winConditions.put(condition, winConditions.get(condition));
}
return clone;
}
}