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
|
@Override
|
||||||
public void processEntities(float dt) {
|
public void process(float dt) {
|
||||||
Entity[] entities = getEntities().items;
|
Entity[] entities = getEntities().items;
|
||||||
for (int i = 0, n = getEntities().size; i < n-1; i++) {
|
for (int i = 0, n = getEntities().size; i < n-1; i++) {
|
||||||
Entity entityA = entities[i];
|
Entity entityA = entities[i];
|
||||||
|
@ -23,7 +23,7 @@ public class ModelRenderSystem extends EntitySystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void preProcessing() {
|
public void preProcess() {
|
||||||
renderer.setColor(Color.WHITE);
|
renderer.setColor(Color.WHITE);
|
||||||
renderer.begin(ShapeRenderer.ShapeType.Line);
|
renderer.begin(ShapeRenderer.ShapeType.Line);
|
||||||
}
|
}
|
||||||
@ -37,7 +37,7 @@ public class ModelRenderSystem extends EntitySystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postProcessing() {
|
public void postProcess() {
|
||||||
renderer.end();
|
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 Array<Entity> toRemove;
|
||||||
|
|
||||||
private ComponentBag[] components;
|
private ComponentBag[] components;
|
||||||
private Array<EntitySystem> systems;
|
private Array<BaseSystem> systems;
|
||||||
|
|
||||||
private ListenerRegistry listenerRegistry;
|
private ListenerRegistry listenerRegistry;
|
||||||
|
|
||||||
@ -64,10 +64,10 @@ public class Engine {
|
|||||||
deactivatePending();
|
deactivatePending();
|
||||||
removePending();
|
removePending();
|
||||||
|
|
||||||
for (EntitySystem system : systems) {
|
for (BaseSystem system : systems) {
|
||||||
system.preProcessing();
|
system.preProcess();
|
||||||
system.processEntities(dt);
|
system.process(dt);
|
||||||
system.postProcessing();
|
system.postProcess();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,6 +82,32 @@ public class Engine {
|
|||||||
toRemove.add(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() {
|
private void removePending() {
|
||||||
if (toRemove.isEmpty()) {
|
if (toRemove.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
@ -90,6 +116,7 @@ public class Engine {
|
|||||||
for (Entity entity : toRemove) {
|
for (Entity entity : toRemove) {
|
||||||
removeAllEntityComponents(entity.getId());
|
removeAllEntityComponents(entity.getId());
|
||||||
entities.removeValue(entity, true);
|
entities.removeValue(entity, true);
|
||||||
|
removeEntityFromSystems(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
toRemove.clear();
|
toRemove.clear();
|
||||||
@ -105,11 +132,7 @@ public class Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (Entity entity : toActivate) {
|
for (Entity entity : toActivate) {
|
||||||
for (EntitySystem system : systems) {
|
addEntityToSystems(entity);
|
||||||
if (system.interestedIn(entity)) {
|
|
||||||
system.entities.add(entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
toActivate.clear();
|
toActivate.clear();
|
||||||
}
|
}
|
||||||
@ -124,11 +147,8 @@ public class Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (Entity entity : toDeactivate) {
|
for (Entity entity : toDeactivate) {
|
||||||
for (EntitySystem system : systems) {
|
removeEntityFromSystems(entity);
|
||||||
system.entities.removeValue(entity, true);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
toDeactivate.clear();
|
toDeactivate.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,26 +2,23 @@ package com.me.common.ecs;
|
|||||||
|
|
||||||
import com.badlogic.gdx.utils.Array;
|
import com.badlogic.gdx.utils.Array;
|
||||||
|
|
||||||
public abstract class EntitySystem {
|
public abstract class EntitySystem extends BaseSystem {
|
||||||
|
|
||||||
private long typeBits;
|
private long typeBits;
|
||||||
|
|
||||||
protected Engine engine;
|
|
||||||
protected Array<Entity> entities;
|
protected Array<Entity> entities;
|
||||||
|
|
||||||
public EntitySystem(Engine engine, Class<? extends Component>... components) {
|
public EntitySystem(Engine engine, Class<? extends Component>... components) {
|
||||||
this.engine = engine;
|
super(engine);
|
||||||
this.typeBits = ComponentType.getMaskBits(components);
|
this.typeBits = ComponentType.getMaskBits(components);
|
||||||
this.entities = new Array<>(true, 16, Entity.class);
|
this.entities = new Array<>(true, 16, Entity.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void preProcessing() {}
|
|
||||||
|
|
||||||
public Array<Entity> getEntities() {
|
public Array<Entity> getEntities() {
|
||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processEntities(float dt) {
|
public void process(float dt) {
|
||||||
for (int i = 0, n = getEntities().size; i < n; i++) {
|
for (int i = 0, n = getEntities().size; i < n; i++) {
|
||||||
processEntity(entities.get(i), dt);
|
processEntity(entities.get(i), dt);
|
||||||
}
|
}
|
||||||
@ -29,10 +26,16 @@ public abstract class EntitySystem {
|
|||||||
|
|
||||||
public abstract void processEntity(Entity entity, float dt);
|
public abstract void processEntity(Entity entity, float dt);
|
||||||
|
|
||||||
public void postProcessing() {}
|
|
||||||
|
|
||||||
public boolean interestedIn(Entity entity) {
|
public boolean interestedIn(Entity entity) {
|
||||||
return (entity.getComponentBits() & typeBits) == typeBits;
|
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