diff --git a/ashley/src/com/badlogic/ashley/core/Engine.java b/ashley/src/com/badlogic/ashley/core/Engine.java index 0f02ab31..e9672bc1 100644 --- a/ashley/src/com/badlogic/ashley/core/Engine.java +++ b/ashley/src/com/badlogic/ashley/core/Engine.java @@ -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; } diff --git a/ashley/tests/com/badlogic/ashley/core/EngineTests.java b/ashley/tests/com/badlogic/ashley/core/EngineTests.java index af907d88..25f57b5d 100644 --- a/ashley/tests/com/badlogic/ashley/core/EngineTests.java +++ b/ashley/tests/com/badlogic/ashley/core/EngineTests.java @@ -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; @@ -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();