diff --git a/core/src/com/me/asteroids/systems/CollisionSystem.java b/core/src/com/me/asteroids/systems/CollisionSystem.java index 50f1354..a76c551 100644 --- a/core/src/com/me/asteroids/systems/CollisionSystem.java +++ b/core/src/com/me/asteroids/systems/CollisionSystem.java @@ -18,7 +18,7 @@ public class CollisionSystem extends EntitySystem { } @Override - public void processEntities(float dt) { + public void process(float dt) { Entity[] entities = getEntities().items; for (int i = 0, n = getEntities().size; i < n-1; i++) { Entity entityA = entities[i]; diff --git a/core/src/com/me/asteroids/systems/ModelRenderSystem.java b/core/src/com/me/asteroids/systems/ModelRenderSystem.java index 95c3597..587c875 100644 --- a/core/src/com/me/asteroids/systems/ModelRenderSystem.java +++ b/core/src/com/me/asteroids/systems/ModelRenderSystem.java @@ -23,7 +23,7 @@ public class ModelRenderSystem extends EntitySystem { } @Override - public void preProcessing() { + public void preProcess() { renderer.setColor(Color.WHITE); renderer.begin(ShapeRenderer.ShapeType.Line); } @@ -37,7 +37,7 @@ public class ModelRenderSystem extends EntitySystem { } @Override - public void postProcessing() { + public void postProcess() { renderer.end(); } diff --git a/core/src/com/me/common/ecs/BaseSystem.java b/core/src/com/me/common/ecs/BaseSystem.java new file mode 100644 index 0000000..d9589a3 --- /dev/null +++ b/core/src/com/me/common/ecs/BaseSystem.java @@ -0,0 +1,17 @@ +package com.me.common.ecs; + +public abstract class BaseSystem { + + protected Engine engine; + + public BaseSystem(Engine engine) { + this.engine = engine; + } + + public void preProcess() {} + + public abstract void process(float dt); + + public void postProcess() {} + +} diff --git a/core/src/com/me/common/ecs/Engine.java b/core/src/com/me/common/ecs/Engine.java index 921d528..7bf4d65 100644 --- a/core/src/com/me/common/ecs/Engine.java +++ b/core/src/com/me/common/ecs/Engine.java @@ -15,7 +15,7 @@ public class Engine { private Array toRemove; private ComponentBag[] components; - private Array systems; + private Array systems; private ListenerRegistry listenerRegistry; @@ -64,10 +64,10 @@ public class Engine { deactivatePending(); removePending(); - for (EntitySystem system : systems) { - system.preProcessing(); - system.processEntities(dt); - system.postProcessing(); + for (BaseSystem system : systems) { + system.preProcess(); + system.process(dt); + system.postProcess(); } } @@ -82,6 +82,32 @@ public class Engine { 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; @@ -90,6 +116,7 @@ public class Engine { for (Entity entity : toRemove) { removeAllEntityComponents(entity.getId()); entities.removeValue(entity, true); + removeEntityFromSystems(entity); } toRemove.clear(); @@ -105,11 +132,7 @@ public class Engine { } for (Entity entity : toActivate) { - for (EntitySystem system : systems) { - if (system.interestedIn(entity)) { - system.entities.add(entity); - } - } + addEntityToSystems(entity); } toActivate.clear(); } @@ -124,11 +147,8 @@ public class Engine { } for (Entity entity : toDeactivate) { - for (EntitySystem system : systems) { - system.entities.removeValue(entity, true); - } + removeEntityFromSystems(entity); } - toDeactivate.clear(); } diff --git a/core/src/com/me/common/ecs/EntitySystem.java b/core/src/com/me/common/ecs/EntitySystem.java index b70696e..89e0b8c 100644 --- a/core/src/com/me/common/ecs/EntitySystem.java +++ b/core/src/com/me/common/ecs/EntitySystem.java @@ -2,26 +2,23 @@ package com.me.common.ecs; import com.badlogic.gdx.utils.Array; -public abstract class EntitySystem { +public abstract class EntitySystem extends BaseSystem { private long typeBits; - protected Engine engine; protected Array entities; public EntitySystem(Engine engine, Class... components) { - this.engine = engine; + super(engine); this.typeBits = ComponentType.getMaskBits(components); this.entities = new Array<>(true, 16, Entity.class); } - public void preProcessing() {} - public Array getEntities() { return entities; } - public void processEntities(float dt) { + public void process(float dt) { for (int i = 0, n = getEntities().size; i < n; i++) { processEntity(entities.get(i), dt); } @@ -29,10 +26,16 @@ public abstract class EntitySystem { public abstract void processEntity(Entity entity, float dt); - public void postProcessing() {} - public boolean interestedIn(Entity entity) { return (entity.getComponentBits() & typeBits) == typeBits; } + protected void addEntity(Entity entity) { + entities.add(entity); + } + + protected void removeEntity(Entity entity) { + entities.removeValue(entity, true); + } + }