Skip to content

Commit

Permalink
Fixed listeners not called if component is added inside listener
Browse files Browse the repository at this point in the history
  • Loading branch information
chartinger committed Sep 23, 2015
1 parent f87edde commit b611dd9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ashley/src/com/badlogic/ashley/core/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,11 @@ protected void addEntityInternal(Entity entity) {
entities.add(entity);
entitiesById.put(entity.getId(), entity);

updateFamilyMembership(entity, false);

entity.componentAdded.add(componentAdded);
entity.componentRemoved.add(componentRemoved);

updateFamilyMembership(entity, false);

entity.componentOperationHandler = componentOperationHandler;
}

Expand Down
27 changes: 27 additions & 0 deletions ashley/tests/com/badlogic/ashley/core/EngineTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public void entityRemoved (Entity entity) {
assertNotNull(entity);
}
}

private static class AddComponentBEntityListenerMock extends EntityListenerMock {
@Override
public void entityAdded (Entity entity) {
super.entityAdded(entity);
entity.add(new ComponentB());
}
}

private static class EntitySystemMock extends EntitySystem {
public int updateCalls = 0;
Expand Down Expand Up @@ -175,6 +183,25 @@ public void addAndRemoveEntity () {
assertEquals(2, listenerB.removedCount);
}

@Test
public void addComponentInsideListener() {
Engine engine = new Engine();

EntityListenerMock listenerA = new AddComponentBEntityListenerMock();
EntityListenerMock listenerB = new EntityListenerMock();

engine.addEntityListener(Family.all(ComponentA.class).get(), listenerA);
engine.addEntityListener(Family.all(ComponentB.class).get(), listenerB);

Entity entity1 = new Entity();
entity1.add(new ComponentA());
engine.addEntity(entity1);

assertEquals(1, listenerA.addedCount);
assertNotNull(entity1.getComponent(ComponentB.class));
assertEquals(1, listenerB.addedCount);
}

@Test
public void addAndRemoveSystem () {
Engine engine = new Engine();
Expand Down

0 comments on commit b611dd9

Please sign in to comment.