Support Entity tagging

This commit is contained in:
Matt Low 2020-02-01 20:31:15 +04:00
parent f8f7868398
commit d8cabf4549
3 changed files with 58 additions and 2 deletions

View File

@ -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 * @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<Entity> getEntitiesInGroup(String group) { public Array<Entity> getEntitiesInGroup(String group) {
return entityManager.getGroupEntities(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) { void addEntityGroup(Entity entity, String group) {
entityManager.addEntityToGroup(entity, group); entityManager.addEntityToGroup(entity, group);
} }
@ -107,6 +115,14 @@ public class Engine {
entityManager.removeEntityFromGroup(entity, group); 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) { void refreshEntity(Entity entity) {
entityManager.queueRefresh(entity); entityManager.queueRefresh(entity);
} }

View File

@ -18,6 +18,7 @@ public final class Entity {
boolean removed; boolean removed;
ObjectSet<String> groups; ObjectSet<String> groups;
String tag;
private long uniqueId; private long uniqueId;
@ -110,6 +111,22 @@ public final class Entity {
return groups.contains(group); 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 // Internal getComponent method accessed only by ComponentMapper
Component getComponent(ComponentType type) { Component getComponent(ComponentType type) {
return engine.getEntityComponent(this, type); return engine.getEntityComponent(this, type);

View File

@ -12,6 +12,7 @@ final class EntityManager {
private final Array<Entity> toRefresh; private final Array<Entity> toRefresh;
private final ObjectMap<String, Array<Entity>> groupEntities; private final ObjectMap<String, Array<Entity>> groupEntities;
private final ObjectMap<String, Entity> taggedEntities;
EntityManager(Engine engine) { EntityManager(Engine engine) {
this.engine = engine; this.engine = engine;
@ -21,6 +22,7 @@ final class EntityManager {
this.toRefresh = new Array<>(false, 16); this.toRefresh = new Array<>(false, 16);
this.groupEntities = new ObjectMap<>(); this.groupEntities = new ObjectMap<>();
this.taggedEntities = new ObjectMap<>();
} }
Array<Entity> getEntities() { Array<Entity> getEntities() {
@ -73,6 +75,25 @@ final class EntityManager {
entity.groups.remove(group); 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) { void queueRefresh(Entity entity) {
toRefresh.add(entity); toRefresh.add(entity);
} }
@ -96,6 +117,8 @@ final class EntityManager {
getGroupEntities(group).removeValue(entity, true); getGroupEntities(group).removeValue(entity, true);
} }
entity.groups.clear(); entity.groups.clear();
removeEntityTag(entity);
} }
} }