From fe249f39eab1ab99a152f90aaffbb4c9a0fdd7f6 Mon Sep 17 00:00:00 2001 From: punchready Date: Sat, 1 May 2021 19:47:19 +0200 Subject: [PATCH] refactor: improve stone cooldown storing and implement better cloning --- .../gamelibrary/entities/EntityID.java | 2 +- .../{EntityList.java => EntityManager.java} | 15 +++++- .../marvelous/gamelibrary/entities/Rock.java | 2 +- .../entities/StoneCooldownManager.java | 52 +++++++++++++++++++ .../gamelibrary/gamelogic/GameState.java | 18 ++----- .../gamelibrary/gamelogic/GameStateView.java | 4 +- 6 files changed, 74 insertions(+), 19 deletions(-) rename src/main/java/uulm/teamname/marvelous/gamelibrary/entities/{EntityList.java => EntityManager.java} (73%) create mode 100644 src/main/java/uulm/teamname/marvelous/gamelibrary/entities/StoneCooldownManager.java 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 87e1b49..c720e6f 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityID.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityID.java @@ -32,7 +32,7 @@ public class EntityID { * @return The cloned {@link EntityID} */ public EntityID clone() { - return new EntityID(this.id, this.type); + return new EntityID(id, type); } /** diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityList.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityManager.java similarity index 73% rename from src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityList.java rename to src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityManager.java index e98ad01..2fea6c8 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityList.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/EntityManager.java @@ -4,11 +4,22 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; -/** Represents a list of {@link Entity}s. */ -public class EntityList { +/** Represents a managed list of {@link Entity}s. */ +public class EntityManager { /** The internal collection of {@link Entity}s */ private final HashSet entities = new HashSet<>(); + /** + * Takes over all the entities from a different {@link EntityManager}. + * @param other The entity list to take the data from + */ + public void cloneFrom(EntityManager other) { + entities.clear(); + for(Entity entity: other.entities) { + entities.add(entity.clone()); + } + } + /** * Clears the list of entities. */ 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 26beb72..1b2c3b7 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java @@ -27,7 +27,7 @@ public class Rock extends Entity { } public void decreaseHp(int damage) { - this.hp -= damage; + hp -= damage; } @Override diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/StoneCooldownManager.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/StoneCooldownManager.java new file mode 100644 index 0000000..bf6974f --- /dev/null +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/StoneCooldownManager.java @@ -0,0 +1,52 @@ +package uulm.teamname.marvelous.gamelibrary.entities; + +import java.util.HashMap; + +/** Represents a manager for infinity stone cooldowns. */ +public class StoneCooldownManager { + /** The cooldown round numbers for every stone */ + private final HashMap cooldowns = new HashMap<>(); + + /** + * Takes over all the cooldowns from a different {@link StoneCooldownManager}. + * @param other The cooldown manager to take the data from + */ + public void cloneFrom(StoneCooldownManager other) { + cooldowns.clear(); + cooldowns.putAll(other.cooldowns); + } + + /** + * Decreases all cooldowns by one according to a round having passed. + */ + public void update() { + cooldowns.replaceAll((s, v) -> Math.max(0, cooldowns.get(s) - 1)); + } + + /** + * Checks if a stone is on cooldown. + * @param stone The {@link StoneType} to check for + * @return Whether or not the stone is on cooldown + */ + public boolean onCooldown(StoneType stone) { + return cooldowns.containsKey(stone); + } + + /** + * Retrieves the current cooldown for a stone. + * @param stone The {@link StoneType} to check for + * @return The stone's cooldown in rounds + */ + public int getCooldown(StoneType stone) { + return cooldowns.get(stone); + } + + /** + * Marks a stone as on cooldown. + * @param stone The {@link StoneType} to check for + * @param duration The number of rounds the stone should be on cooldown + */ + public void setCooldown(StoneType stone, int duration) { + cooldowns.put(stone, Math.max(0, duration)); + } +} 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 ced7f0f..b7e3a53 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java @@ -1,15 +1,11 @@ package uulm.teamname.marvelous.gamelibrary.gamelogic; import uulm.teamname.marvelous.gamelibrary.IntVector2; -import uulm.teamname.marvelous.gamelibrary.entities.Entity; -import uulm.teamname.marvelous.gamelibrary.entities.EntityID; -import uulm.teamname.marvelous.gamelibrary.entities.EntityList; -import uulm.teamname.marvelous.gamelibrary.entities.StoneType; +import uulm.teamname.marvelous.gamelibrary.entities.*; 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 { @@ -17,7 +13,7 @@ class GameState { public final IntVector2 mapSize; /** The list of {@link Entity}s inside the game */ - public final EntityList entities = new EntityList(); + public final EntityManager entities = new EntityManager(); /** The total amount of full turn cycles that occurred */ public int roundNumber = 0; @@ -35,7 +31,7 @@ class GameState { public boolean won = false; /** The global cooldown of every infinity stone */ - public final HashMap stoneCooldown = new HashMap<>(); + public final StoneCooldownManager stoneCooldown = new StoneCooldownManager(); /** The store of the {@link WinCondition} data for every win condition for each player */ public final HashMap, Integer> winConditions = new HashMap<>(); @@ -55,9 +51,7 @@ class GameState { public GameState snapshot() { GameState clone = new GameState(this.mapSize); - for(Iterator it = this.entities.getEntities(); it.hasNext(); ) { - clone.entities.addEntity(it.next().clone()); - } + clone.entities.cloneFrom(entities); clone.roundNumber = roundNumber; @@ -72,9 +66,7 @@ class GameState { clone.won = won; - for(StoneType type: stoneCooldown.keySet()) { - clone.stoneCooldown.put(type, stoneCooldown.get(type)); - } + clone.stoneCooldown.cloneFrom(stoneCooldown); for(Tuple condition: winConditions.keySet()) { clone.winConditions.put(condition, winConditions.get(condition)); diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateView.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateView.java index 6c2d26a..124ca5a 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateView.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateView.java @@ -45,7 +45,7 @@ public class GameStateView { return state.won; } - public float getStoneCooldown(StoneType stone) { - return state.stoneCooldown.get(stone); + public int getStoneCooldown(StoneType stone) { + return state.stoneCooldown.getCooldown(stone); } }