From 1dbfcb4dc1ea0ea0a0fdb0d481c00b7dd9b8dd8f Mon Sep 17 00:00:00 2001 From: David Saltares Date: Tue, 9 Jun 2015 19:27:09 +0100 Subject: [PATCH] engine.addSystem() replaces the old system if there was one of the same class #175 --- .../src/com/badlogic/ashley/core/Engine.java | 28 +++++++++++++------ .../com/badlogic/ashley/core/EngineTests.java | 19 +++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/ashley/src/com/badlogic/ashley/core/Engine.java b/ashley/src/com/badlogic/ashley/core/Engine.java index a8bd2130..bd874ead 100644 --- a/ashley/src/com/badlogic/ashley/core/Engine.java +++ b/ashley/src/com/badlogic/ashley/core/Engine.java @@ -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) { @@ -174,17 +176,23 @@ public ImmutableArray 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 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); } /** @@ -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); @@ -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) { diff --git a/ashley/tests/com/badlogic/ashley/core/EngineTests.java b/ashley/tests/com/badlogic/ashley/core/EngineTests.java index fd1865c4..af907d88 100644 --- a/ashley/tests/com/badlogic/ashley/core/EngineTests.java +++ b/ashley/tests/com/badlogic/ashley/core/EngineTests.java @@ -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 () {