diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Character.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Character.java index a5b18e5..2328a90 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Character.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Character.java @@ -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; + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Entity.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Entity.java index eeee9dd..1be2176 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Entity.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Entity.java @@ -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; } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityID.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityID.java index 8916b31..87e1b49 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityID.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityID.java @@ -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 diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/InfinityStone.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/InfinityStone.java index 45e5997..3b8a8a4 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/InfinityStone.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/InfinityStone.java @@ -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); + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java index abf84ba..6c13d0e 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java @@ -21,7 +21,7 @@ public class Inventory implements Iterable { 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."); } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/NPC.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/NPC.java index 3ede8b1..06b72c2 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/NPC.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/NPC.java @@ -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; + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java index bf1fde7..26beb72 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java @@ -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; + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java index c1c02fc..ced7f0f 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java @@ -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 turnOrder; + public ArrayList 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 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 condition: winConditions.keySet()) { + clone.winConditions.put(condition, winConditions.get(condition)); + } + + return clone; } }