Skip to content

Commit

Permalink
Engine uses Array<Entity> rather than IntMap<Entity> for faster itera…
Browse files Browse the repository at this point in the history
…tion (even though removal is more expensive)

Engine returns ImmutableArray when querying entity families
Removes ImmutableIntMap
  • Loading branch information
dsaltares committed Aug 6, 2014
1 parent 5ede6b6 commit db641f9
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 360 deletions.
37 changes: 18 additions & 19 deletions ashley/src/com/badlogic/ashley/core/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

import com.badlogic.ashley.signals.Listener;
import com.badlogic.ashley.signals.Signal;
import com.badlogic.ashley.utils.ImmutableIntMap;
import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.ObjectMap.Entry;

Expand Down Expand Up @@ -52,9 +51,9 @@ public class Engine {
/** A hashmap that organises EntitySystems by class for easy retrieval */
private ObjectMap<Class<?>, EntitySystem> systemsByClass;
/** A hashmap that organises all entities into family buckets */
private ObjectMap<Family, IntMap<Entity>> families;
private ObjectMap<Family, Array<Entity>> families;
/** A hashmap that organises all entities into immutable family buckets */
private ObjectMap<Family, ImmutableIntMap<Entity>> immutableFamilies;
private ObjectMap<Family, ImmutableArray<Entity>> immutableFamilies;
/** A collection of entity added/removed event listeners */
private Array<EntityListener> listeners;
/** EntityListeners that await removal */
Expand All @@ -71,8 +70,8 @@ public Engine(){
entities = new Array<Entity>();
systems = new Array<EntitySystem>();
systemsByClass = new ObjectMap<Class<?>, EntitySystem>();
families = new ObjectMap<Family, IntMap<Entity>>();
immutableFamilies = new ObjectMap<Family, ImmutableIntMap<Entity>>();
families = new ObjectMap<Family, Array<Entity>>();
immutableFamilies = new ObjectMap<Family, ImmutableArray<Entity>>();
listeners = new Array<EntityListener>();
removalPendingListeners = new Array<EntityListener>();
notifying = false;
Expand All @@ -98,9 +97,9 @@ public void receive(Signal<Entity> signal, Entity object) {
public void addEntity(Entity entity){
entities.add(entity);

for (Entry<Family, IntMap<Entity>> entry : families.entries()) {
for (Entry<Family, Array<Entity>> entry : families.entries()) {
if(entry.key.matches(entity)){
entry.value.put(entity.getIndex(), entity);
entry.value.add(entity);
entity.getFamilyBits().set(entry.key.getIndex());
}
}
Expand All @@ -123,9 +122,9 @@ public void removeEntity(Entity entity){
entities.removeValue(entity, true);

if(!entity.getFamilyBits().isEmpty()){
for (Entry<Family, IntMap<Entity>> entry : families.entries()) {
for (Entry<Family, Array<Entity>> entry : families.entries()) {
if(entry.key.matches(entity)){
entry.value.remove(entity.getIndex());
entry.value.removeValue(entity, true);
entity.getFamilyBits().clear(entry.key.getIndex());
}
}
Expand Down Expand Up @@ -186,18 +185,18 @@ public <T extends EntitySystem> T getSystem(Class<T> systemType) {
/**
* Returns immutable collection of entities for the specified {@link Family}. Will return the same instance every time.
*/
public ImmutableIntMap<Entity> getEntitiesFor(Family family){
IntMap<Entity> entities = families.get(family, null);
public ImmutableArray<Entity> getEntitiesFor(Family family){
Array<Entity> entities = families.get(family, null);
if(entities == null){
entities = new IntMap<Entity>();
entities = new Array<Entity>();
for(Entity e:this.entities){
if(family.matches(e)) {
entities.put(e.getIndex(), e);
entities.add(e);
e.getFamilyBits().set(family.getIndex());
}
}
families.put(family, entities);
immutableFamilies.put(family, new ImmutableIntMap<Entity>(entities));
immutableFamilies.put(family, new ImmutableArray<Entity>(entities));
}

return immutableFamilies.get(family);
Expand Down Expand Up @@ -235,21 +234,21 @@ public void update(float deltaTime){
}

private void componentAdded(Entity entity){
for (Entry<Family, IntMap<Entity>> entry : families.entries()) {
for (Entry<Family, Array<Entity>> entry : families.entries()) {
if(!entity.getFamilyBits().get(entry.key.getIndex())){
if(entry.key.matches(entity)){
entry.value.put(entity.getIndex(), entity);
entry.value.add(entity);
entity.getFamilyBits().set(entry.key.getIndex());
}
}
}
}

private void componentRemoved(Entity entity){
for (Entry<Family, IntMap<Entity>> entry : families.entries()) {
for (Entry<Family, Array<Entity>> entry : families.entries()) {
if(entity.getFamilyBits().get(entry.key.getIndex())){
if(!entry.key.matches(entity)){
entry.value.remove(entity.getIndex());
entry.value.removeValue(entity, true);
entity.getFamilyBits().clear(entry.key.getIndex());
}
}
Expand Down
11 changes: 6 additions & 5 deletions ashley/src/com/badlogic/ashley/systems/IteratingSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.EntitySystem;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.utils.ImmutableIntMap;
import com.badlogic.gdx.utils.IntMap.Entry;
import com.badlogic.ashley.utils.ImmutableArray;

/**
* A simple EntitySystem that iterates over each entity and calls processEntity() for each entity every time
Expand All @@ -34,7 +33,7 @@ public abstract class IteratingSystem extends EntitySystem {
/** The family describing this systems entities */
private Family family;
/** The entities used by this system */
private ImmutableIntMap<Entity> entities;
private ImmutableArray<Entity> entities;

/**
* Instantiates a system that will iterate over the entities described by the Family.
Expand Down Expand Up @@ -68,8 +67,10 @@ public void removedFromEngine(Engine engine) {

@Override
public void update(float deltaTime) {
for (Entity entity : entities.values()) {
processEntity(entity, deltaTime);
int numEntities = entities.size();

for (int i = 0; i < numEntities; ++i) {
processEntity(entities.get(i), deltaTime);
}
}

Expand Down
188 changes: 0 additions & 188 deletions ashley/src/com/badlogic/ashley/utils/ImmutableIntMap.java

This file was deleted.

Loading

0 comments on commit db641f9

Please sign in to comment.