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.EntityType; import uulm.teamname.marvelous.gamelibrary.entities.Rock; import java.util.ArrayList; 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 entities = new HashSet<>(); /** A set of all currently used {@link Rock} entity ids */ private final HashSet usedRockSlots = new HashSet<>(); /** * Takes over all the entities from a different {@link EntityManager}. * @param other The entity list to take the data from */ void cloneFrom(EntityManager other) { entities.clear(); for(Entity entity: other.entities) { entities.add(entity.clone()); } usedRockSlots.clear(); usedRockSlots.addAll(other.usedRockSlots); } /** * Finds an unused {@link Rock} entity id * @return The first free id */ int findFreeRockSlot() { int i = 0; while(usedRockSlots.contains(i)) { i++; } return i; } /** * Clears the list of entities. */ void clear() { entities.clear(); usedRockSlots.clear(); } /** * Adds an entity to the list. * @param entity The {@link Entity} to add */ void addEntity(Entity entity) { if(entity.id.isSameType(EntityType.Rocks)) { usedRockSlots.add(entity.id.id); } entities.add(entity); } /** * Adds multiple entities to the list. * @param entities The entities to add */ void addEntities(Entity... entities) { for(Entity e: entities) { this.addEntity(e); } } /** * Removes an entity from the list. * @param entity The {@link Entity} to remove */ boolean removeEntity(Entity entity) { if(entity.id.isSameType(EntityType.Rocks)) { usedRockSlots.remove(entity.id.id); } return entities.remove(entity); } /** * Removes an entity from the list. * @param entityid The {@link EntityID} of the {@link Entity} to remove */ boolean removeEntity(EntityID entityid) { Entity entity = findEntity(entityid); if(entity != null) { return entities.remove(entity); }else { return false; } } /** * Finds an entity with an {@link EntityID}. * @param id The id to search for * @return The found {@link Entity} or null if none found */ public Entity findEntity(EntityID id) { for(Entity entity: entities) { if(entity.id.equals(id)) { return entity; } } return null; } /** * Finds all entities with a position. * @param pos The position to check on * @return The found {@link Entity}s matching the position */ public ArrayList findByPosition(IntVector2 pos) { ArrayList found = new ArrayList<>(); for(Entity entity: entities) { if(entity.getPosition().equals(pos)) { found.add(entity); } } return found; } /** * Checks if any entity on a position blocks vision. * @param pos The position to check on * @return Whether or not anything on the position blocks vision */ public boolean blocksVision(IntVector2 pos) { for(Entity entity: entities) { if(entity.getPosition().equals(pos) && entity.blocksVision()) { return true; } } return false; } /** * Checks if any entity on a position blocks movement. * @param pos The position to check on * @return Whether or not anything on the position blocks movement */ public boolean blocksMovement(IntVector2 pos) { for(Entity entity: entities) { if(entity.getPosition().equals(pos) && entity.blocksMovement()) { return true; } } return false; } /** * Iterates over all entities inside the list. * @return An iterator over every {@link Entity} */ public Iterator getEntities() { return entities.iterator(); } /** * Exports all entities as an array. * @return An array containing every {@link Entity} */ Entity[] export() { return (Entity[])entities.toArray(); } }