Skip to content

Commit

Permalink
Update getEntitiesWith method to iterate over all caches
Browse files Browse the repository at this point in the history
Also updates the implementation of said method to be a more concise
stream-based operation.
  • Loading branch information
Vizaxo committed Jun 27, 2017
1 parent 74411b3 commit 6c9e508
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

import com.google.common.collect.Lists;
import com.google.common.collect.MapMaker;
import gnu.trove.iterator.TLongObjectIterator;
import gnu.trove.list.TLongList;
import gnu.trove.list.array.TLongArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.entitySystem.Component;
Expand Down Expand Up @@ -361,30 +358,12 @@ private EntityRef createEntityRef(long entityId) {
@SafeVarargs
@Override
public final Iterable<EntityRef> getEntitiesWith(Class<? extends Component>... componentClasses) {
if (componentClasses.length == 0) {
return getAllEntities();
}
TLongList idList = new TLongArrayList();
TLongObjectIterator<? extends Component> primeIterator = componentStore.componentIterator(componentClasses[0]);
if (primeIterator == null) {
return Collections.emptyList();
}

while (primeIterator.hasNext()) {
primeIterator.advance();
long id = primeIterator.key();
boolean discard = false;
for (int i = 1; i < componentClasses.length; ++i) {
if (componentStore.get(id, componentClasses[i]) == null) {
discard = true;
break;
}
}
if (!discard) {
idList.add(primeIterator.key());
}
}
return () -> new EntityIterator(idList.iterator(), this);
return () -> entityStore.keySet().stream()
//Keep entities which have all of the required components
.filter(id -> Arrays.stream(componentClasses)
.allMatch(component -> componentStore.get(id, component) != null))
.map(id -> createEntityRef(id))
.iterator();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.google.common.collect.Sets;
import gnu.trove.iterator.TLongObjectIterator;
import gnu.trove.list.TLongList;
import gnu.trove.list.array.TLongArrayList;
import gnu.trove.set.TLongSet;
import gnu.trove.set.hash.TLongHashSet;
import org.slf4j.Logger;
Expand Down Expand Up @@ -257,50 +256,9 @@ public Iterable<EntityRef> getAllEntities() {

@SafeVarargs
@Override
//Todo: implement iterating over multiple caches
public final Iterable<EntityRef> getEntitiesWith(Class<? extends Component>... componentClasses) {
if (componentClasses.length == 0) {
return getAllEntities();
}
if (componentClasses.length == 1) {
return iterateEntities(componentClasses[0]);
}
TLongList idList = new TLongArrayList();
TLongObjectIterator<? extends Component> primeIterator = globalCache.getComponentStore().componentIterator(componentClasses[0]);
if (primeIterator == null) {
return Collections.emptyList();
}

while (primeIterator.hasNext()) {
primeIterator.advance();
long id = primeIterator.key();
boolean discard = false;
for (int i = 1; i < componentClasses.length; ++i) {
if (globalCache.getComponentStore().get(id, componentClasses[i]) == null) {
discard = true;
break;
}
}
if (!discard) {
idList.add(primeIterator.key());
}
}
return new EntityIterable(idList);
}

private Iterable<EntityRef> iterateEntities(Class<? extends Component> componentClass) {
TLongList idList = new TLongArrayList();
TLongObjectIterator<? extends Component> primeIterator = globalCache.getComponentStore().componentIterator(componentClass);
if (primeIterator == null) {
return Collections.emptyList();
}

while (primeIterator.hasNext()) {
primeIterator.advance();
long id = primeIterator.key();
idList.add(primeIterator.key());
}
return new EntityIterable(idList);
return Iterables.concat(globalCache.getEntitiesWith(componentClasses),
sectorManager.getEntitiesWith(componentClasses));
}

@Override
Expand Down

0 comments on commit 6c9e508

Please sign in to comment.