Skip to content

Commit

Permalink
engine.addSystem() replaces the old system if there was one of the sa…
Browse files Browse the repository at this point in the history
…me class #175
  • Loading branch information
dsaltares committed Jun 9, 2015
1 parent 9e6e260 commit 1dbfcb4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
28 changes: 19 additions & 9 deletions ashley/src/com/badlogic/ashley/core/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ private long obtainEntityId() {

/**
* Adds an entity to this Engine.
* This will throw an IllegalArgumentException if the given entity
* was already registered with an engine.
*/
public void addEntity(Entity entity){
if (entity.uuid != 0L) {
Expand Down Expand Up @@ -174,17 +176,23 @@ public ImmutableArray<Entity> getEntities() {

/**
* Adds the {@link EntitySystem} to this Engine.
* If the Engine already had a system of the same class,
* the new one will replace the old one.
*/
public void addSystem(EntitySystem system){
Class<? extends EntitySystem> systemType = system.getClass();

if (!systemsByClass.containsKey(systemType)) {
systems.add(system);
systemsByClass.put(systemType, system);
system.addedToEngineInternal(this);

systems.sort(comparator);

EntitySystem oldSytem = getSystem(systemType);

if (oldSytem != null) {
removeSystem(oldSytem);
}

systems.add(system);
systemsByClass.put(systemType, system);
system.addedToEngineInternal(this);

systems.sort(comparator);
}

/**
Expand Down Expand Up @@ -305,7 +313,7 @@ else if (belongsToFamily && !matches) {
}

protected void removeEntityInternal(Entity entity) {
boolean removed;
boolean removed = false;

entity.scheduledForRemoval = false;
entities.removeValue(entity, true);
Expand Down Expand Up @@ -340,7 +348,9 @@ protected void removeEntityInternal(Entity entity) {
entityListeners.end();
notifying = false;

entity.uuid = 0L;
if (removed) {
entity.uuid = 0L;
}
}

protected void addEntityInternal(Entity entity) {
Expand Down
19 changes: 19 additions & 0 deletions ashley/tests/com/badlogic/ashley/core/EngineTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,25 @@ public void getSystems () {

assertEquals(2, engine.getSystems().size());
}

@Test
public void addTwoSystemsOfSameClass () {
Engine engine = new Engine();
EntitySystemMockA system1 = new EntitySystemMockA();
EntitySystemMockA system2 = new EntitySystemMockA();

assertEquals(0, engine.getSystems().size());

engine.addSystem(system1);

assertEquals(1, engine.getSystems().size());
assertEquals(system1, engine.getSystem(EntitySystemMockA.class));

engine.addSystem(system2);

assertEquals(1, engine.getSystems().size());
assertEquals(system2, engine.getSystem(EntitySystemMockA.class));
}

@Test
public void systemUpdate () {
Expand Down

0 comments on commit 1dbfcb4

Please sign in to comment.