Skip to content

Commit

Permalink
#221 - Fixes returning pooled entity to the entity pool twice
Browse files Browse the repository at this point in the history
  • Loading branch information
dsaltares committed May 31, 2016
1 parent 78957d1 commit fe3ed12
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 38 deletions.
18 changes: 10 additions & 8 deletions ashley/src/com/badlogic/ashley/core/EntityManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ public void processPendingOperations() {
}

protected void removeEntityInternal(Entity entity) {
entity.scheduledForRemoval = false;
entity.removing = true;
entities.removeValue(entity, true);
entitySet.remove(entity);

listener.entityRemoved(entity);
entity.removing = false;
boolean removed = entitySet.remove(entity);

if (removed) {
entity.scheduledForRemoval = false;
entity.removing = true;
entities.removeValue(entity, true);
listener.entityRemoved(entity);
entity.removing = false;
}
}

protected void addEntityInternal(Entity entity) {
Expand All @@ -120,7 +122,7 @@ protected void addEntityInternal(Entity entity) {

listener.entityAdded(entity);
}

private static class EntityOperation implements Pool.Poolable {
public enum Type {
Add,
Expand Down
45 changes: 15 additions & 30 deletions ashley/tests/com/badlogic/ashley/core/PooledEngineTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,6 @@ public void receive (Signal<Entity> signal, Entity object) {
totalCalls++;
}
}

private static class RemoveEntityTwiceSystem extends EntitySystem {
private ImmutableArray<Entity> entities;

@Override
public void addedToEngine (Engine engine) {
entities = engine.getEntitiesFor(Family.all(PositionComponent.class).get());
}

@Override
public void update (float deltaTime) {
Entity entity;
PooledEngine engine = (PooledEngine)getEngine();
for (int i = 0; i < 10; i++) {
entity = engine.createEntity();
assertEquals(0, entity.flags);
entity.flags = 1;
entity.add(engine.createComponent(PositionComponent.class));
engine.addEntity(entity);
}
for (int i = 0; i < entities.size(); ++i) {
entity = entities.get(i);
engine.removeEntity(entity);
engine.removeEntity(entity);
}
}
}

private static class PooledComponentSpy implements Component, Poolable {
public boolean recycled = false;
Expand Down Expand Up @@ -210,10 +183,22 @@ public void recycleEntity () {
@Test
public void removeEntityTwice () {
PooledEngine engine = new PooledEngine();
engine.addSystem(new RemoveEntityTwiceSystem());

for (int j = 0; j < 2; j++) {
engine.update(0);
for (int i = 0; i < 100; ++i) {
Array<Entity> entities = new Array<Entity>();

for (int j = 0; j < 100; ++j) {
Entity entity = engine.createEntity();
engine.addEntity(entity);
assertEquals(0, entity.flags);
entity.flags = 1;
entities.add(entity);
}

for (Entity entity : entities) {
engine.removeEntity(entity);
engine.removeEntity(entity);
}
}
}

Expand Down

0 comments on commit fe3ed12

Please sign in to comment.