diff --git a/ashley/src/com/badlogic/ashley/core/Engine.java b/ashley/src/com/badlogic/ashley/core/Engine.java index fdd019d..f687aa8 100644 --- a/ashley/src/com/badlogic/ashley/core/Engine.java +++ b/ashley/src/com/badlogic/ashley/core/Engine.java @@ -42,14 +42,14 @@ */ public class Engine { private static Family empty = Family.all().get(); - + private final Listener componentAdded = new ComponentListener(); private final Listener componentRemoved = new ComponentListener(); - + private SystemManager systemManager = new SystemManager(new EngineSystemListener()); private EntityManager entityManager = new EntityManager(new EngineEntityListener()); private ComponentOperationHandler componentOperationHandler = new ComponentOperationHandler(new EngineDelayedInformer()); - private FamilyManager familyManager = new FamilyManager(entityManager.getEntities()); + private FamilyManager familyManager = new FamilyManager(entityManager.getEntities()); private boolean updating; /** @@ -89,7 +89,7 @@ public void removeEntity(Entity entity){ boolean delayed = updating || familyManager.notifying(); entityManager.removeEntity(entity, delayed); } - + /** * Removes all entities of the given {@link Family}. */ @@ -171,7 +171,7 @@ public ImmutableArray getSystems() { return systemManager.getSystems(); } - /** Returns immutable collection of entities for the specified {@link Family}. + /** Returns immutable collection of entities for the specified {@link Family}. * Returns the same instance every time for the same Family. */ public ImmutableArray getEntitiesFor(Family family){ @@ -229,36 +229,52 @@ public void update(float deltaTime){ if (updating) { throw new IllegalStateException("Cannot call update() on an Engine that is already updating."); } - + updating = true; ImmutableArray systems = systemManager.getSystems(); try { for (int i = 0; i < systems.size(); ++i) { EntitySystem system = systems.get(i); - + if (system.checkProcessing()) { - system.update(deltaTime); - } - - while(componentOperationHandler.hasOperationsToProcess() || entityManager.hasPendingOperations()) { - componentOperationHandler.processOperations(); - entityManager.processPendingOperations(); + updateSystem(system, deltaTime); } + + processOperations(); } } finally { updating = false; - } + } + } + + /** + * Updates the given system. + * @param system The system to update. + * @param deltaTime The time passed since the last frame. + */ + protected void updateSystem(EntitySystem system, float deltaTime) { + system.update(deltaTime); } - + + /** + * Processes all pending operations (add/remove) of entities and components. + */ + protected void processOperations() { + while(componentOperationHandler.hasOperationsToProcess() || entityManager.hasPendingOperations()) { + componentOperationHandler.processOperations(); + entityManager.processPendingOperations(); + } + } + protected void addEntityInternal(Entity entity) { entity.componentAdded.add(componentAdded); entity.componentRemoved.add(componentRemoved); entity.componentOperationHandler = componentOperationHandler; - + familyManager.updateFamilyMembership(entity); } - + protected void removeEntityInternal(Entity entity) { familyManager.updateFamilyMembership(entity); @@ -266,14 +282,14 @@ protected void removeEntityInternal(Entity entity) { entity.componentRemoved.remove(componentRemoved); entity.componentOperationHandler = null; } - + private class ComponentListener implements Listener { @Override public void receive(Signal signal, Entity object) { familyManager.updateFamilyMembership(object); } } - + private class EngineSystemListener implements SystemListener { @Override public void systemAdded (EntitySystem system) { @@ -285,7 +301,7 @@ public void systemRemoved (EntitySystem system) { system.removedFromEngineInternal(Engine.this); } } - + private class EngineEntityListener implements EntityListener { @Override public void entityAdded (Entity entity) { @@ -297,7 +313,7 @@ public void entityRemoved (Entity entity) { removeEntityInternal(entity); } } - + private class EngineDelayedInformer implements BooleanInformer { @Override public boolean value () {