diff --git a/core/src/com/me/common/ecs/Engine.java b/core/src/com/me/common/ecs/Engine.java index 6f3f5e8..ab0a4c5 100644 --- a/core/src/com/me/common/ecs/Engine.java +++ b/core/src/com/me/common/ecs/Engine.java @@ -1,6 +1,7 @@ package com.me.common.ecs; import com.badlogic.gdx.utils.Array; +import com.badlogic.gdx.utils.ObjectSet; import com.me.common.ecs.event.Event; import com.me.common.ecs.event.Listener; @@ -89,6 +90,23 @@ public class Engine { listenerRegistry.callEvent(event); } + /** + * Return the list of entities that are a part of the given group. + * @param group the group to look up + * @return a list of entities + */ + public Array getEntitiesInGroup(String group) { + return entityManager.getGroupEntities(group); + } + + void addEntityGroup(Entity entity, String group) { + entityManager.addEntityToGroup(entity, group); + } + + void removeEntityGroup(Entity entity, String group) { + entityManager.removeEntityFromGroup(entity, group); + } + void refreshEntity(Entity entity) { entityManager.queueRefresh(entity); } diff --git a/core/src/com/me/common/ecs/Entity.java b/core/src/com/me/common/ecs/Entity.java index 316c00b..b66a7d1 100644 --- a/core/src/com/me/common/ecs/Entity.java +++ b/core/src/com/me/common/ecs/Entity.java @@ -1,5 +1,7 @@ package com.me.common.ecs; +import com.badlogic.gdx.utils.ObjectSet; + public final class Entity { private static int nextId = 0; @@ -15,11 +17,14 @@ public final class Entity { boolean active; boolean removed; + ObjectSet groups; + private long uniqueId; protected Entity(Engine engine) { this.engine = engine; this.id = nextId++; + this.groups = new ObjectSet<>(); } public int getId() { @@ -89,6 +94,22 @@ public final class Entity { return engine.getEntityComponent(this, clazz); } + public ObjectSet getGroups() { + return groups; + } + + public void addGroup(String group) { + engine.addEntityGroup(this, group); + } + + public void removeGroup(String group) { + engine.removeEntityGroup(this, group); + } + + public boolean inGroup(String group) { + return groups.contains(group); + } + // Internal getComponent method accessed only by ComponentMapper Component getComponent(ComponentType type) { return engine.getEntityComponent(this, type); diff --git a/core/src/com/me/common/ecs/EntityManager.java b/core/src/com/me/common/ecs/EntityManager.java index bf45d3a..5112389 100644 --- a/core/src/com/me/common/ecs/EntityManager.java +++ b/core/src/com/me/common/ecs/EntityManager.java @@ -1,6 +1,7 @@ package com.me.common.ecs; import com.badlogic.gdx.utils.Array; +import com.badlogic.gdx.utils.ObjectMap; final class EntityManager { @@ -10,12 +11,16 @@ final class EntityManager { private final Array removedEntities; private final Array toRefresh; + private final ObjectMap> groupEntities; + EntityManager(Engine engine) { this.engine = engine; this.entities = new Array<>(false, 16); this.removedEntities = new Array<>(false, 16); this.toRefresh = new Array<>(false, 16); + + this.groupEntities = new ObjectMap<>(); } Array getEntities() { @@ -47,6 +52,27 @@ final class EntityManager { toRefresh.clear(); } + Array getGroupEntities(String group) { + Array entities = groupEntities.get(group); + if (entities == null) { + entities = new Array<>(); + groupEntities.put(group, entities); + } + return entities; + } + + void addEntityToGroup(Entity entity, String group) { + if (!entity.groups.contains(group)) { + getGroupEntities(group).add(entity); + entity.groups.add(group); + } + } + + void removeEntityFromGroup(Entity entity, String group) { + getGroupEntities(group).removeValue(entity, true); + entity.groups.remove(group); + } + void queueRefresh(Entity entity) { toRefresh.add(entity); } @@ -64,7 +90,14 @@ final class EntityManager { engine.removeAllEntityComponents(entity.id); entities.removeValue(entity, true); removedEntities.add(entity); + + // remove the entity from any groups it is in + for (String group : entity.groups) { + getGroupEntities(group).removeValue(entity, true); + } + entity.groups.clear(); } } + }