Gamelib/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EntityManager.java

64 lines
1.6 KiB
Java
Raw Normal View History

2021-05-01 19:40:36 +00:00
package uulm.teamname.marvelous.gamelibrary.gamelogic;
import uulm.teamname.marvelous.gamelibrary.entities.Entity;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
/** 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.
*/
public void clear() {
entities.clear();
}
/**
* Adds an entity to the list.
* @param entity The {@link Entity} to add
*/
public void addEntity(Entity entity) {
entities.add(entity);
}
/**
* Adds multiple entities to the list.
* @param entities The entities to add
*/
public void addEntities(Entity... entities) {
this.entities.addAll(Arrays.asList(entities));
}
/**
* Removes an entity from the list.
* @param entity The {@link Entity} to remove
*/
public boolean removeEntity(Entity entity) {
return entities.remove(entity);
}
/**
* Iterates over all entities inside the list.
* @return An iterator over every {@link Entity}
*/
public Iterator<Entity> getEntities() {
return entities.iterator();
}
}