diff --git a/ashley/src/com/badlogic/ashley/core/Engine.java b/ashley/src/com/badlogic/ashley/core/Engine.java index 46c22732..d367edd5 100644 --- a/ashley/src/com/badlogic/ashley/core/Engine.java +++ b/ashley/src/com/badlogic/ashley/core/Engine.java @@ -22,6 +22,7 @@ import com.badlogic.ashley.signals.Signal; import com.badlogic.ashley.utils.ImmutableArray; import com.badlogic.gdx.utils.Array; +import com.badlogic.gdx.utils.LongMap; import com.badlogic.gdx.utils.ObjectMap; import com.badlogic.gdx.utils.ObjectMap.Entry; import com.badlogic.gdx.utils.Pool; @@ -43,6 +44,7 @@ public class Engine { private static SystemComparator comparator = new SystemComparator(); private Array entities; + private LongMap entitiesById; private Array entityOperations; private EntityOperationPool entityOperationPool; private Array systems; @@ -64,6 +66,7 @@ public class Engine { public Engine () { entities = new Array(false, 16); + entitiesById = new LongMap(); entityOperations = new Array(false, 16); entityOperationPool = new EntityOperationPool(); systems = new Array(false, 16); @@ -85,9 +88,6 @@ public Engine () { componentOperationHandler = new ComponentOperationHandler(this); } - private long obtainEntityId () { - return nextEntityId++; - } /** Adds an entity to this Engine. */ public void addEntity (Entity entity) { @@ -133,6 +133,10 @@ public void removeAllEntities () { } } } + + public Entity getById(long id) { + return entitiesById.get(id); + } /** Adds the {@link EntitySystem} to this Engine. */ public void addSystem (EntitySystem system) { @@ -221,23 +225,27 @@ public void update (float deltaTime) { updating = false; } + + private long obtainEntityId () { + return nextEntityId++; + } private void updateFamilyMembership (Entity entity) { for (Entry> entry : families.entries()) { Family family = entry.key; - Array entities = entry.value; + Array familyEntities = entry.value; int familyIndex = family.getIndex(); boolean belongsToFamily = entity.getFamilyBits().get(familyIndex); boolean matches = family.matches(entity); if (!belongsToFamily && matches) { - entities.add(entity); + familyEntities.add(entity); entity.getFamilyBits().set(familyIndex); notifyFamilyListenersAdd(family, entity); } else if (belongsToFamily && !matches) { - entities.removeValue(entity, true); + familyEntities.removeValue(entity, true); entity.getFamilyBits().clear(familyIndex); notifyFamilyListenersRemove(family, entity); @@ -248,14 +256,15 @@ private void updateFamilyMembership (Entity entity) { protected void removeEntityInternal (Entity entity) { entity.scheduledForRemoval = false; entities.removeValue(entity, true); + entitiesById.remove(entity.getId()); if (!entity.getFamilyBits().isEmpty()) { for (Entry> entry : families.entries()) { Family family = entry.key; - Array entities = entry.value; + Array familyEntities = entry.value; if (family.matches(entity)) { - entities.removeValue(entity, true); + familyEntities.removeValue(entity, true); entity.getFamilyBits().clear(family.getIndex()); notifyFamilyListenersRemove(family, entity); } @@ -278,6 +287,7 @@ protected void removeEntityInternal (Entity entity) { protected void addEntityInternal (Entity entity) { entities.add(entity); + entitiesById.put(entity.getId(), entity); updateFamilyMembership(entity); @@ -329,14 +339,14 @@ private ImmutableArray registerFamily (Family family) { ImmutableArray immutableEntities = immutableFamilies.get(family); if (immutableEntities == null) { - Array entities = new Array(false, 16); - immutableEntities = new ImmutableArray(entities); - families.put(family, entities); + Array familyEntities = new Array(false, 16); + immutableEntities = new ImmutableArray(familyEntities); + families.put(family, familyEntities); immutableFamilies.put(family, immutableEntities); for (Entity e : this.entities) { if (family.matches(e)) { - entities.add(e); + familyEntities.add(e); e.getFamilyBits().set(family.getIndex()); } } diff --git a/ashley/tests/com/badlogic/ashley/core/EngineTests.java b/ashley/tests/com/badlogic/ashley/core/EngineTests.java index 419c9bd8..53516afb 100644 --- a/ashley/tests/com/badlogic/ashley/core/EngineTests.java +++ b/ashley/tests/com/badlogic/ashley/core/EngineTests.java @@ -572,4 +572,24 @@ public void createManyEntitiesNoStackOverflow () { engine.update(0); } + + @Test + public void getEntityById () { + Engine engine = new Engine(); + Entity entity = new Entity(); + + assertEquals(0L, entity.getId()); + + engine.addEntity(entity); + + long entityId = entity.getId(); + + assertNotEquals(0L, entityId); + + assertEquals(entity, engine.getById(entityId)); + + engine.removeEntity(entity); + + assertEquals(null, engine.getById(entityId)); + } }