Add entity grouping.
This commit is contained in:
parent
749e378a03
commit
f8f7868398
@ -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<Entity> 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);
|
||||
}
|
||||
|
@ -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<String> 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<String> 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);
|
||||
|
@ -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<Entity> removedEntities;
|
||||
private final Array<Entity> toRefresh;
|
||||
|
||||
private final ObjectMap<String, Array<Entity>> 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<Entity> getEntities() {
|
||||
@ -47,6 +52,27 @@ final class EntityManager {
|
||||
toRefresh.clear();
|
||||
}
|
||||
|
||||
Array<Entity> getGroupEntities(String group) {
|
||||
Array<Entity> 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user