diff --git a/core/src/com/me/common/ecs/Engine.java b/core/src/com/me/common/ecs/Engine.java index 8d6dd51..a0b6c92 100644 --- a/core/src/com/me/common/ecs/Engine.java +++ b/core/src/com/me/common/ecs/Engine.java @@ -9,25 +9,16 @@ import java.util.Map; public class Engine { - private Array entities; - private Array toActivate; - private Array toDeactivate; - private Array toRemove; - private Array removedEntities; - private ComponentBag[] components; - private Array systems; + protected Array systems; + private EntityManager entityManager; private ListenerRegistry listenerRegistry; private Map, ComponentMapper> componentMappers; public Engine() { - this.entities = new Array<>(); - this.toActivate = new Array<>(); - this.toDeactivate = new Array<>(); - this.toRemove = new Array<>(); - this.removedEntities = new Array<>(false, 16); + this.entityManager = new EntityManager(this); this.systems = new Array<>(); this.listenerRegistry = new ListenerRegistry(); @@ -38,7 +29,7 @@ public class Engine { ComponentType.registerComponentType(clazz); } - public void registerSystem(EntitySystem system) { + public void registerSystem(BaseSystem system) { this.systems.add(system); } @@ -53,18 +44,8 @@ public class Engine { } } - public Array getEntities() { - return entities; - } - - public int getEntityCount() { - return entities.size; - } - public void update(float dt) { - activatePending(); - deactivatePending(); - removePending(); + entityManager.update(); for (BaseSystem system : systems) { system.preProcess(); @@ -73,99 +54,37 @@ public class Engine { } } + public Array getEntities() { + return entityManager.entities; + } + + public int getEntityCount() { + return entityManager.entities.size; + } + public Entity createEntity() { - Entity entity; - if (!removedEntities.isEmpty()) { - entity = removedEntities.removeIndex(0); - entity.reset(); - } else { - entity = new Entity(this); - } - entity.updateUniqueId(); - entities.add(entity); - return entity; - } - - protected void removeEntity(Entity entity) { - toRemove.add(entity); - } - - private void removeEntityFromSystems(Entity entity) { - for (BaseSystem system : systems) { - if (!(system instanceof EntitySystem)) { - continue; - } - - EntitySystem entitySystem = (EntitySystem) system; - if (entitySystem.interestedIn(entity)) { - entitySystem.removeEntity(entity); - } - } - } - - private void addEntityToSystems(Entity entity) { - for (BaseSystem system : systems) { - if (!(system instanceof EntitySystem)) { - continue; - } - - EntitySystem entitySystem = (EntitySystem) system; - if (entitySystem.interestedIn(entity)) { - entitySystem.addEntity(entity); - } - } - } - - private void removePending() { - if (toRemove.isEmpty()) { - return; - } - - for (Entity entity : toRemove) { - removeAllEntityComponents(entity.id); - entities.removeValue(entity, true); - removeEntityFromSystems(entity); - removedEntities.add(entity); - } - - toRemove.clear(); + return entityManager.create(); } protected void activateEntity(Entity entity) { - toActivate.add(entity); + entityManager.activate(entity); } - private void activatePending() { - if (toActivate.isEmpty()) { - return; - } - - for (Entity entity : toActivate) { - addEntityToSystems(entity); - } - toActivate.clear(); - } protected void deactivateEntity(Entity entity) { - toDeactivate.add(entity); + entityManager.deactivate(entity); } - private void deactivatePending() { - if (toDeactivate.isEmpty()) { - return; - } - for (Entity entity : toDeactivate) { - removeEntityFromSystems(entity); - } - toDeactivate.clear(); + protected void removeEntity(Entity entity) { + entityManager.remove(entity); } public void callEvent(Event event) { listenerRegistry.callEvent(event); } - private void removeAllEntityComponents(int entityId) { + protected void removeAllEntityComponents(int entityId) { for (int i = 0; i < components.length; i++) { components[i].insert(entityId, null); } @@ -192,10 +111,6 @@ public class Engine { return components[type.id].get(entity.id); } - protected boolean entityHasComponent(Entity entity, ComponentType type) { - return components[type.id].contains(entity.id); - } - @SuppressWarnings("unchecked") public ComponentMapper getComponentMapper(Class typeClass) { ComponentMapper mapper = componentMappers.get(typeClass); diff --git a/core/src/com/me/common/ecs/EntityManager.java b/core/src/com/me/common/ecs/EntityManager.java new file mode 100644 index 0000000..9a6aa86 --- /dev/null +++ b/core/src/com/me/common/ecs/EntityManager.java @@ -0,0 +1,120 @@ +package com.me.common.ecs; + +import com.badlogic.gdx.utils.Array; + +final class EntityManager { + + private Engine engine; + + protected Array entities; + protected Array toActivate; + protected Array toDeactivate; + protected Array toRemove; + protected Array removedEntities; + + public EntityManager(Engine engine) { + this.engine = engine; + + this.entities = new Array<>(); + this.toActivate = new Array<>(); + this.toDeactivate = new Array<>(); + this.toRemove = new Array<>(); + this.removedEntities = new Array<>(false, 16); + } + + public void update() { + activatePending(); + deactivatePending(); + removePending(); + } + + public Entity create() { + Entity entity; + if (!removedEntities.isEmpty()) { + entity = removedEntities.removeIndex(0); + entity.reset(); + } else { + entity = new Entity(engine); + } + entity.updateUniqueId(); + entities.add(entity); + return entity; + } + + public void remove(Entity entity) { + toRemove.add(entity); + } + + public void activate(Entity entity) { + toActivate.add(entity); + } + + public void deactivate(Entity entity) { + toDeactivate.add(entity); + } + + private void removePending() { + if (toRemove.isEmpty()) { + return; + } + + for (Entity entity : toRemove) { + engine.removeAllEntityComponents(entity.id); + entities.removeValue(entity, true); + removeEntityFromSystems(entity); + removedEntities.add(entity); + } + + toRemove.clear(); + } + + private void activatePending() { + if (toActivate.isEmpty()) { + return; + } + + for (Entity entity : toActivate) { + addEntityToSystems(entity); + } + toActivate.clear(); + } + + private void deactivatePending() { + if (toDeactivate.isEmpty()) { + return; + } + + for (Entity entity : toDeactivate) { + removeEntityFromSystems(entity); + } + toDeactivate.clear(); + } + + private void removeEntityFromSystems(Entity entity) { + for (BaseSystem system : engine.systems) { + if (!(system instanceof EntitySystem)) { + continue; + } + + EntitySystem entitySystem = (EntitySystem) system; + if (entitySystem.interestedIn(entity)) { + entitySystem.removeEntity(entity); + } + } + } + + private void addEntityToSystems(Entity entity) { + for (BaseSystem system : engine.systems) { + if (!(system instanceof EntitySystem)) { + continue; + } + + EntitySystem entitySystem = (EntitySystem) system; + if (entitySystem.interestedIn(entity)) { + entitySystem.addEntity(entity); + } + } + } + + +}