refactor: improve stone cooldown storing and implement better cloning
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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<Entity> 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.
|
||||
*/
|
@ -27,7 +27,7 @@ public class Rock extends Entity {
|
||||
}
|
||||
|
||||
public void decreaseHp(int damage) {
|
||||
this.hp -= damage;
|
||||
hp -= damage;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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<StoneType, Integer> 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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user