Skip to content

Commit

Permalink
Throws exceptions when adding an Entity twice and when doing nested E…
Browse files Browse the repository at this point in the history
…ngine updates #173
  • Loading branch information
dsaltares committed Jun 8, 2015
1 parent aae861e commit a8571ab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
17 changes: 16 additions & 1 deletion ashley/src/com/badlogic/ashley/core/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ private long obtainEntityId() {
* Adds an entity to this Engine.
*/
public void addEntity(Entity entity){
if (entity.uuid != 0L) {
throw new IllegalArgumentException("Entity is already registered with an Engine id = " + entity.uuid);
}

entity.uuid = obtainEntityId();
if (updating || notifying) {
EntityOperation operation = entityOperationPool.obtain();
Expand Down Expand Up @@ -257,6 +261,10 @@ public void removeEntityListener(EntityListener listener) {
* @param deltaTime The time passed since the last frame.
*/
public void update(float deltaTime){
if (updating) {
throw new IllegalStateException("Cannot call update() on an Engine that is already updating.");
}

updating = true;
for(int i=0; i<systems.size; i++){
EntitySystem system = systems.get(i);
Expand Down Expand Up @@ -297,9 +305,14 @@ else if (belongsToFamily && !matches) {
}

protected void removeEntityInternal(Entity entity) {
boolean removed;

This comment has been minimized.

Copy link
@antag99

antag99 Jun 9, 2015

Contributor

What is this flag doing? Did you forget something here?


entity.scheduledForRemoval = false;
entities.removeValue(entity, true);
entitiesById.remove(entity.getId());

if (entitiesById.remove(entity.getId()) == entity) {
removed = true;
}

if(!entity.getFamilyBits().isEmpty()){
for (Entry<Family, Array<Entity>> entry : families.entries()) {
Expand All @@ -326,6 +339,8 @@ protected void removeEntityInternal(Entity entity) {
}
entityListeners.end();
notifying = false;

entity.uuid = 0L;
}

protected void addEntityInternal(Entity entity) {
Expand Down
28 changes: 28 additions & 0 deletions ashley/tests/com/badlogic/ashley/core/EngineTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -629,4 +629,32 @@ public void getEntities () {

assertEquals(0, engineEntities.size());
}

@Test(expected=IllegalArgumentException.class)
public void addEntityTwice () {
Engine engine = new Engine();
Entity entity = new Entity();
engine.addEntity(entity);
engine.addEntity(entity);
}

@Test(expected=IllegalStateException.class)
public void nestedUpdateException() {
final Engine engine = new Engine();

engine.addSystem(new EntitySystem() {
boolean duringCallback;

@Override
public void update(float deltaTime) {
if (!duringCallback) {
duringCallback = true;
getEngine().update(deltaTime);
duringCallback = false;
}
}
});

engine.update(deltaTime);
}
}

0 comments on commit a8571ab

Please sign in to comment.