From cfa5f986f7bf707f37c5d25c9a07e84b27f42a43 Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 2 May 2019 20:04:25 -0500 Subject: [PATCH] perf: faster visibleEntities --- packages/entity/list/index.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/entity/list/index.js b/packages/entity/list/index.js index f015079..50c5d2c 100644 --- a/packages/entity/list/index.js +++ b/packages/entity/list/index.js @@ -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; } }