Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overridable updateSystem, processOperations in Engine #316

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 37 additions & 21 deletions ashley/src/com/badlogic/ashley/core/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@
*/
public class Engine {
private static Family empty = Family.all().get();

private final Listener<Entity> componentAdded = new ComponentListener();
private final Listener<Entity> 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;

/**
Expand Down Expand Up @@ -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}.
*/
Expand Down Expand Up @@ -171,7 +171,7 @@ public ImmutableArray<EntitySystem> 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<Entity> getEntitiesFor(Family family){
Expand Down Expand Up @@ -229,51 +229,67 @@ public void update(float deltaTime){
if (updating) {
throw new IllegalStateException("Cannot call update() on an Engine that is already updating.");
}

updating = true;
ImmutableArray<EntitySystem> 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);

entity.componentAdded.remove(componentAdded);
entity.componentRemoved.remove(componentRemoved);
entity.componentOperationHandler = null;
}

private class ComponentListener implements Listener<Entity> {
@Override
public void receive(Signal<Entity> signal, Entity object) {
familyManager.updateFamilyMembership(object);
}
}

private class EngineSystemListener implements SystemListener {
@Override
public void systemAdded (EntitySystem system) {
Expand All @@ -285,7 +301,7 @@ public void systemRemoved (EntitySystem system) {
system.removedFromEngineInternal(Engine.this);
}
}

private class EngineEntityListener implements EntityListener {
@Override
public void entityAdded (Entity entity) {
Expand All @@ -297,7 +313,7 @@ public void entityRemoved (Entity entity) {
removeEntityInternal(entity);
}
}

private class EngineDelayedInformer implements BooleanInformer {
@Override
public boolean value () {
Expand Down