Add BaseSystem which all systems (entity processing or not) extend from
This commit is contained in:
parent
28dbff3d16
commit
5d9d49971f
@ -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];
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
17
core/src/com/me/common/ecs/BaseSystem.java
Normal file
17
core/src/com/me/common/ecs/BaseSystem.java
Normal file
@ -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() {}
|
||||
|
||||
}
|
@ -15,7 +15,7 @@ public class Engine {
|
||||
private Array<Entity> toRemove;
|
||||
|
||||
private ComponentBag[] components;
|
||||
private Array<EntitySystem> systems;
|
||||
private Array<BaseSystem> 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();
|
||||
}
|
||||
|
||||
|
@ -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<Entity> entities;
|
||||
|
||||
public EntitySystem(Engine engine, Class<? extends Component>... components) {
|
||||
this.engine = engine;
|
||||
super(engine);
|
||||
this.typeBits = ComponentType.getMaskBits(components);
|
||||
this.entities = new Array<>(true, 16, Entity.class);
|
||||
}
|
||||
|
||||
public void preProcessing() {}
|
||||
|
||||
public Array<Entity> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user