perf: faster visibleEntities

This commit is contained in:
cha0s 2019-05-02 20:04:25 -05:00
parent b899a9dc48
commit cfa5f986f7

View File

@ -1,7 +1,7 @@
import * as I from 'immutable';
import mapValues from 'lodash.mapvalues';
import {arrayUnique, compose, EventEmitter} from '@avocado/core';
import {compose, EventEmitter} from '@avocado/core';
import {QuadTree, Rectangle, Vector} from '@avocado/math';
import {Synchronized} from '@avocado/state';
@ -133,19 +133,28 @@ export class EntityList extends decorate(class {}) {
visibleEntities(query) {
const entities = [];
const entitiesTrack = [];
const quadTree = this._quadTree;
const nodes = quadTree.search(query);
// First, uniqueify.
for (let i = 0; i < nodes.length; ++i) {
const node = nodes[i];
const entity = node.data[2];
if (-1 === entitiesTrack.indexOf(entity)) {
entitiesTrack.push(entity);
}
}
for (let i = 0; i < entitiesTrack.length; i++) {
const entity = entitiesTrack[i];
// Make sure they're actually in the query due to expanded AABB.
if (!Rectangle.intersects(query, entity.visibleAabb)) {
const visibleAabb = entity.visibleAabb;
if (!Rectangle.intersects(query, visibleAabb)) {
continue;
}
entities.push(entity);
}
// Hitting multiple points for each entity can return duplicates.
return arrayUnique(entities);
return entities;
}
}