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() { public boolean isAlive() {
return hp.getValue() > 0; 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. */ /** Represents an abstract entity. */
public abstract class Entity { public abstract class Entity {
/** Whether or not the entity is currently active in the game */ /** Whether or not the entity is currently active in the game */
private boolean active = true; protected boolean active = true;
/** The position of the entity */ /** The position of the entity */
private IntVector2 position; protected IntVector2 position;
/** The {@link EntityID} of the entity */ /** The {@link EntityID} of the entity */
public final EntityID id; public final EntityID id;
@ -23,6 +23,12 @@ public abstract class Entity {
this.id = id; this.id = id;
} }
/**
* Clones the {@link Entity}.
* @return A new entity cloned from this one
*/
public abstract Entity clone();
public boolean isActive() { public boolean isActive() {
return active; return active;
} }

View File

@ -27,6 +27,14 @@ public class EntityID {
return type == other; 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. * Serializes the id for debugging.
* @return A debug string containing all the necessary information about the id * @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); super(id, position);
this.type = type; this.type = type;
} }
@Override
public InfinityStone clone() {
return new InfinityStone(id, position, type);
}
} }

View File

@ -29,4 +29,13 @@ public class NPC extends Entity {
super(id, position); super(id, position);
this.inventory = new Inventory(); 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) { public void decreaseHp(int damage) {
this.hp -= 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.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
/** Represents the state of a game instance. */ /** Represents the state of a game instance. */
class GameState { class GameState {
@ -22,13 +23,13 @@ class GameState {
public int roundNumber = 0; public int roundNumber = 0;
/** The turn order of every character */ /** The turn order of every character */
public ArrayList<EntityID> turnOrder; public ArrayList<EntityID> turnOrder = new ArrayList<>();
/** The total amount of turns that occurred */ /** The total amount of turns that occurred */
public int turnNumber = 0; public int turnNumber = 0;
/** The {@link EntityID} of the active character */ /** The {@link EntityID} of the active character */
public EntityID activeCharacter; public EntityID activeCharacter = null;
/** Whether or not the game was won */ /** Whether or not the game was won */
public boolean won = false; 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 * @return The cloned game state
*/ */
public GameState snapshot() { public GameState snapshot() {
//TODO: implement GameState.snapshot GameState clone = new GameState(this.mapSize);
return this;
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;
} }
} }