From d8cabf4549c33638a79150df0e2b317dacdb5dd5 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sat, 1 Feb 2020 20:31:15 +0400 Subject: [PATCH] Support Entity tagging --- core/src/com/me/common/ecs/Engine.java | 20 ++++++++++++++-- core/src/com/me/common/ecs/Entity.java | 17 ++++++++++++++ core/src/com/me/common/ecs/EntityManager.java | 23 +++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/core/src/com/me/common/ecs/Engine.java b/core/src/com/me/common/ecs/Engine.java index ab0a4c5..b52bb13 100644 --- a/core/src/com/me/common/ecs/Engine.java +++ b/core/src/com/me/common/ecs/Engine.java @@ -91,14 +91,22 @@ public class Engine { } /** - * 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 + * @return the list of entities that are a part of the given group. */ public Array getEntitiesInGroup(String group) { return entityManager.getGroupEntities(group); } + /** + * @param tag the tag to look up + * @return the entity with the given tag, or null + */ + public Entity getEntityByTag(String tag) { + return entityManager.getEntityByTag(tag); + } + + void addEntityGroup(Entity entity, String group) { entityManager.addEntityToGroup(entity, group); } @@ -107,6 +115,14 @@ public class Engine { entityManager.removeEntityFromGroup(entity, group); } + void tagEntity(Entity entity, String tag) { + entityManager.tagEntity(entity, tag); + } + + void removeEntityTag(Entity entity) { + entityManager.removeEntityTag(entity); + } + 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 b66a7d1..ee1b46a 100644 --- a/core/src/com/me/common/ecs/Entity.java +++ b/core/src/com/me/common/ecs/Entity.java @@ -18,6 +18,7 @@ public final class Entity { boolean removed; ObjectSet groups; + String tag; private long uniqueId; @@ -110,6 +111,22 @@ public final class Entity { return groups.contains(group); } + public String getTag() { + return tag; + } + + public void setTag(String tag) { + engine.tagEntity(this, tag); + } + + public void removeTag() { + engine.removeEntityTag(this); + } + + public boolean hasTag(String tag) { + return tag.equals(this.tag); + } + // 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 5112389..93e1c44 100644 --- a/core/src/com/me/common/ecs/EntityManager.java +++ b/core/src/com/me/common/ecs/EntityManager.java @@ -12,6 +12,7 @@ final class EntityManager { private final Array toRefresh; private final ObjectMap> groupEntities; + private final ObjectMap taggedEntities; EntityManager(Engine engine) { this.engine = engine; @@ -21,6 +22,7 @@ final class EntityManager { this.toRefresh = new Array<>(false, 16); this.groupEntities = new ObjectMap<>(); + this.taggedEntities = new ObjectMap<>(); } Array getEntities() { @@ -73,6 +75,25 @@ final class EntityManager { entity.groups.remove(group); } + Entity getEntityByTag(String tag) { + return taggedEntities.get(tag); + } + + void tagEntity(Entity entity, String tag) { + Entity previouslyTagged = taggedEntities.put(tag, entity); + if (previouslyTagged != null) { + previouslyTagged.tag = null; + } + entity.tag = tag; + } + + void removeEntityTag(Entity entity) { + if (entity.tag != null) { + taggedEntities.remove(entity.tag); + entity.tag = null; + } + } + void queueRefresh(Entity entity) { toRefresh.add(entity); } @@ -96,6 +117,8 @@ final class EntityManager { getGroupEntities(group).removeValue(entity, true); } entity.groups.clear(); + + removeEntityTag(entity); } }