Support Entity tagging
This commit is contained in:
parent
f8f7868398
commit
d8cabf4549
@ -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<Entity> 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);
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ public final class Entity {
|
||||
boolean removed;
|
||||
|
||||
ObjectSet<String> 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);
|
||||
|
@ -12,6 +12,7 @@ final class EntityManager {
|
||||
private final Array<Entity> toRefresh;
|
||||
|
||||
private final ObjectMap<String, Array<Entity>> groupEntities;
|
||||
private final ObjectMap<String, Entity> 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<Entity> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user