refactor: simplify aabb query

This commit is contained in:
cha0s 2019-05-09 01:32:49 -05:00
parent 3e4f48c517
commit f15b23c437
2 changed files with 10 additions and 15 deletions

View File

@ -161,27 +161,22 @@ export class EntityList extends decorate(class {}) {
visibleEntities(query) {
const entities = [];
const entitiesTrack = [];
const entitiesChecked = [];
const quadTree = this._quadTree;
const nodes = quadTree.search(query);
// First, uniqueify.
// Check all nodes.
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.
const visibleAabb = entity.visibleAabb;
if (!Rectangle.intersects(query, visibleAabb)) {
continue;
}
const aabb = node.data[3];
if (-1 === entitiesChecked.indexOf(entity)) {
entitiesChecked.push(entity);
// Make sure the AABB is actually in the query due to expansion.
if (Rectangle.intersects(query, aabb)) {
entities.push(entity);
}
// Hitting multiple points for each entity can return duplicates.
}
}
return entities;
}

View File

@ -37,7 +37,7 @@ export class Listed extends Trait {
const expandedAabb = Rectangle.expand(aabb, [32, 32]);
this.quadTreeAabb = expandedAabb;
const points = Rectangle.toPoints(expandedAabb);
this.quadTreeNodes = points.map((point) => [...point, this.entity]);
this.quadTreeNodes = points.map((point) => [...point, this.entity, aabb]);
// Add points to quad tree.
const quadTree = list.quadTree;
for (const node of this.quadTreeNodes) {