Skip to content

Commit

Permalink
Process delayed removal and addition of entities without exception
Browse files Browse the repository at this point in the history
  • Loading branch information
BryceCicada committed Dec 22, 2015
1 parent db6050f commit c45ba3b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
8 changes: 4 additions & 4 deletions ashley/src/com/badlogic/ashley/core/EntityManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ public void addEntity(Entity entity){
}

public void addEntity(Entity entity, boolean delayed){
if (entitySet.contains(entity)) {
throw new IllegalArgumentException("Entity is already registered " + entity);
}

if (delayed) {
EntityOperation operation = entityOperationPool.obtain();
operation.entity = entity;
Expand Down Expand Up @@ -113,6 +109,10 @@ protected void removeEntityInternal(Entity entity) {
}

protected void addEntityInternal(Entity entity) {
if (entitySet.contains(entity)) {
throw new IllegalArgumentException("Entity is already registered " + entity);
}

entities.add(entity);
entitySet.add(entity);

Expand Down
62 changes: 60 additions & 2 deletions ashley/tests/com/badlogic/ashley/core/EntityManagerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,34 @@ public void getEntities () {
}

@Test(expected=IllegalArgumentException.class)
public void addEntityTwice () {
public void addEntityTwice1 () {
EntityListenerMock listener = new EntityListenerMock();
EntityManager manager = new EntityManager(listener);
Entity entity = new Entity();
manager.addEntity(entity);
manager.addEntity(entity);
}


@Test(expected=IllegalArgumentException.class)
public void addEntityTwice2() {
EntityListenerMock listener = new EntityListenerMock();
EntityManager manager = new EntityManager(listener);
Entity entity = new Entity();
manager.addEntity(entity, false);
manager.addEntity(entity, false);
}

@Test(expected=IllegalArgumentException.class)
public void addEntityTwiceDelayed() {
EntityListenerMock listener = new EntityListenerMock();
EntityManager manager = new EntityManager(listener);

Entity entity = new Entity();
manager.addEntity(entity, true);
manager.addEntity(entity, true);
manager.processPendingOperations();
}

@Test
public void delayedOperationsOrder() {
EntityListenerMock listener = new EntityListenerMock();
Expand All @@ -108,4 +128,42 @@ public void delayedOperationsOrder() {
assertNotEquals(-1, manager.getEntities().indexOf(entityC, true));
assertNotEquals(-1, manager.getEntities().indexOf(entityD, true));
}

@Test
public void removeAndAddEntityDelayed() {
EntityListenerMock listener = new EntityListenerMock();
EntityManager manager = new EntityManager(listener);

Entity entity = new Entity();
manager.addEntity(entity, false); // immediate
assertEquals(1, manager.getEntities().size());

manager.removeEntity(entity, true); // delayed
assertEquals(1, manager.getEntities().size());

manager.addEntity(entity, true); // delayed
assertEquals(1, manager.getEntities().size());

manager.processPendingOperations();
assertEquals(1, manager.getEntities().size());
}

@Test
public void removeAllAndAddEntityDelayed() {
EntityListenerMock listener = new EntityListenerMock();
EntityManager manager = new EntityManager(listener);

Entity entity = new Entity();
manager.addEntity(entity, false); // immediate
assertEquals(1, manager.getEntities().size());

manager.removeAllEntities(true); // delayed
assertEquals(1, manager.getEntities().size());

manager.addEntity(entity, true); // delayed
assertEquals(1, manager.getEntities().size());

manager.processPendingOperations();
assertEquals(1, manager.getEntities().size());
}
}

0 comments on commit c45ba3b

Please sign in to comment.