2021-05-01 19:40:36 +00:00
|
|
|
package uulm.teamname.marvelous.gamelibrary.gamelogic;
|
|
|
|
|
2021-05-01 22:03:03 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
2021-05-01 19:40:36 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.entities.Entity;
|
2021-05-01 22:03:03 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
|
2021-05-11 01:50:24 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.entities.Rock;
|
2021-04-30 18:54:34 +00:00
|
|
|
|
2021-05-01 22:03:03 +00:00
|
|
|
import java.util.ArrayList;
|
2021-04-30 18:54:34 +00:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
2021-05-01 17:47:19 +00:00
|
|
|
/** Represents a managed list of {@link Entity}s. */
|
|
|
|
public class EntityManager {
|
2021-04-30 18:54:34 +00:00
|
|
|
/** The internal collection of {@link Entity}s */
|
|
|
|
private final HashSet<Entity> entities = new HashSet<>();
|
|
|
|
|
2021-05-11 01:50:24 +00:00
|
|
|
/** A set of all currently used {@link Rock} entity ids */
|
|
|
|
private final HashSet<Integer> usedRockSlots = new HashSet<>();
|
|
|
|
|
2021-05-01 17:47:19 +00:00
|
|
|
/**
|
|
|
|
* Takes over all the entities from a different {@link EntityManager}.
|
|
|
|
* @param other The entity list to take the data from
|
|
|
|
*/
|
2021-05-19 16:24:07 +00:00
|
|
|
void cloneFrom(EntityManager other) {
|
2021-05-01 17:47:19 +00:00
|
|
|
entities.clear();
|
|
|
|
for(Entity entity: other.entities) {
|
|
|
|
entities.add(entity.clone());
|
|
|
|
}
|
2021-05-11 01:50:24 +00:00
|
|
|
|
|
|
|
usedRockSlots.clear();
|
|
|
|
usedRockSlots.addAll(other.usedRockSlots);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds an unused {@link Rock} entity id
|
|
|
|
* @return The first free id
|
|
|
|
*/
|
2021-05-19 16:24:07 +00:00
|
|
|
int findFreeRockSlot() {
|
2021-05-11 01:50:24 +00:00
|
|
|
int i = 0;
|
|
|
|
while(usedRockSlots.contains(i)) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
2021-05-01 17:47:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Clears the list of entities.
|
|
|
|
*/
|
2021-05-19 16:24:07 +00:00
|
|
|
void clear() {
|
2021-04-30 18:54:34 +00:00
|
|
|
entities.clear();
|
2021-05-11 01:50:24 +00:00
|
|
|
usedRockSlots.clear();
|
2021-04-30 18:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an entity to the list.
|
|
|
|
* @param entity The {@link Entity} to add
|
|
|
|
*/
|
2021-05-19 16:24:07 +00:00
|
|
|
void addEntity(Entity entity) {
|
2021-05-11 01:50:24 +00:00
|
|
|
if(entity.id.isSameType(EntityType.Rocks)) {
|
|
|
|
usedRockSlots.add(entity.id.id);
|
|
|
|
}
|
2021-04-30 18:54:34 +00:00
|
|
|
entities.add(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds multiple entities to the list.
|
|
|
|
* @param entities The entities to add
|
|
|
|
*/
|
2021-05-19 16:24:07 +00:00
|
|
|
void addEntities(Entity... entities) {
|
2021-05-11 01:50:24 +00:00
|
|
|
for(Entity e: entities) {
|
|
|
|
this.addEntity(e);
|
|
|
|
}
|
2021-04-30 18:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes an entity from the list.
|
|
|
|
* @param entity The {@link Entity} to remove
|
|
|
|
*/
|
2021-05-19 16:24:07 +00:00
|
|
|
boolean removeEntity(Entity entity) {
|
2021-05-11 01:50:24 +00:00
|
|
|
if(entity.id.isSameType(EntityType.Rocks)) {
|
|
|
|
usedRockSlots.remove(entity.id.id);
|
|
|
|
}
|
2021-04-30 18:54:34 +00:00
|
|
|
return entities.remove(entity);
|
|
|
|
}
|
|
|
|
|
2021-05-18 11:55:36 +00:00
|
|
|
/**
|
|
|
|
* Removes an entity from the list.
|
|
|
|
* @param entityid The {@link EntityID} of the {@link Entity} to remove
|
|
|
|
*/
|
2021-05-19 16:24:07 +00:00
|
|
|
boolean removeEntity(EntityID entityid) {
|
2021-05-18 11:55:36 +00:00
|
|
|
Entity entity = findEntity(entityid);
|
|
|
|
if(entity != null) {
|
|
|
|
return entities.remove(entity);
|
|
|
|
}else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 22:03:03 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2021-05-31 22:32:07 +00:00
|
|
|
if(entity.id.equals(id)) {
|
2021-05-01 22:03:03 +00:00
|
|
|
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<Entity> findByPosition(IntVector2 pos) {
|
|
|
|
ArrayList<Entity> found = new ArrayList<>();
|
|
|
|
for(Entity entity: entities) {
|
2021-05-31 22:32:07 +00:00
|
|
|
if(entity.getPosition().equals(pos)) {
|
2021-05-01 22:03:03 +00:00
|
|
|
found.add(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2021-05-02 13:34:42 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2021-05-31 22:32:07 +00:00
|
|
|
if(entity.getPosition().equals(pos) && entity.blocksVision()) {
|
2021-05-02 13:34:42 +00:00
|
|
|
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) {
|
2021-05-31 22:32:07 +00:00
|
|
|
if(entity.getPosition().equals(pos) && entity.blocksMovement()) {
|
2021-05-02 13:34:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Iterates over all entities inside the list.
|
|
|
|
* @return An iterator over every {@link Entity}
|
|
|
|
*/
|
|
|
|
public Iterator<Entity> getEntities() {
|
|
|
|
return entities.iterator();
|
|
|
|
}
|
2021-05-05 16:59:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Exports all entities as an array.
|
|
|
|
* @return An array containing every {@link Entity}
|
|
|
|
*/
|
2021-05-19 16:24:07 +00:00
|
|
|
Entity[] export() {
|
2021-06-01 13:43:04 +00:00
|
|
|
return entities.toArray(new Entity[0]);
|
2021-05-05 16:59:07 +00:00
|
|
|
}
|
2021-04-30 18:54:34 +00:00
|
|
|
}
|