Add entity grouping.
This commit is contained in:
parent
749e378a03
commit
f8f7868398
@ -1,6 +1,7 @@
|
|||||||
package com.me.common.ecs;
|
package com.me.common.ecs;
|
||||||
|
|
||||||
import com.badlogic.gdx.utils.Array;
|
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.Event;
|
||||||
import com.me.common.ecs.event.Listener;
|
import com.me.common.ecs.event.Listener;
|
||||||
|
|
||||||
@ -89,6 +90,23 @@ public class Engine {
|
|||||||
listenerRegistry.callEvent(event);
|
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) {
|
void refreshEntity(Entity entity) {
|
||||||
entityManager.queueRefresh(entity);
|
entityManager.queueRefresh(entity);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.me.common.ecs;
|
package com.me.common.ecs;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.utils.ObjectSet;
|
||||||
|
|
||||||
public final class Entity {
|
public final class Entity {
|
||||||
|
|
||||||
private static int nextId = 0;
|
private static int nextId = 0;
|
||||||
@ -15,11 +17,14 @@ public final class Entity {
|
|||||||
boolean active;
|
boolean active;
|
||||||
boolean removed;
|
boolean removed;
|
||||||
|
|
||||||
|
ObjectSet<String> groups;
|
||||||
|
|
||||||
private long uniqueId;
|
private long uniqueId;
|
||||||
|
|
||||||
protected Entity(Engine engine) {
|
protected Entity(Engine engine) {
|
||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
this.id = nextId++;
|
this.id = nextId++;
|
||||||
|
this.groups = new ObjectSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
@ -89,6 +94,22 @@ public final class Entity {
|
|||||||
return engine.getEntityComponent(this, clazz);
|
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
|
// 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);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.me.common.ecs;
|
package com.me.common.ecs;
|
||||||
|
|
||||||
import com.badlogic.gdx.utils.Array;
|
import com.badlogic.gdx.utils.Array;
|
||||||
|
import com.badlogic.gdx.utils.ObjectMap;
|
||||||
|
|
||||||
final class EntityManager {
|
final class EntityManager {
|
||||||
|
|
||||||
@ -10,12 +11,16 @@ final class EntityManager {
|
|||||||
private final Array<Entity> removedEntities;
|
private final Array<Entity> removedEntities;
|
||||||
private final Array<Entity> toRefresh;
|
private final Array<Entity> toRefresh;
|
||||||
|
|
||||||
|
private final ObjectMap<String, Array<Entity>> groupEntities;
|
||||||
|
|
||||||
EntityManager(Engine engine) {
|
EntityManager(Engine engine) {
|
||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
|
|
||||||
this.entities = new Array<>(false, 16);
|
this.entities = new Array<>(false, 16);
|
||||||
this.removedEntities = new Array<>(false, 16);
|
this.removedEntities = new Array<>(false, 16);
|
||||||
this.toRefresh = new Array<>(false, 16);
|
this.toRefresh = new Array<>(false, 16);
|
||||||
|
|
||||||
|
this.groupEntities = new ObjectMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
Array<Entity> getEntities() {
|
Array<Entity> getEntities() {
|
||||||
@ -47,6 +52,27 @@ final class EntityManager {
|
|||||||
toRefresh.clear();
|
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) {
|
void queueRefresh(Entity entity) {
|
||||||
toRefresh.add(entity);
|
toRefresh.add(entity);
|
||||||
}
|
}
|
||||||
@ -64,7 +90,14 @@ final class EntityManager {
|
|||||||
engine.removeAllEntityComponents(entity.id);
|
engine.removeAllEntityComponents(entity.id);
|
||||||
entities.removeValue(entity, true);
|
entities.removeValue(entity, true);
|
||||||
removedEntities.add(entity);
|
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