refactor: simplify aabb query
This commit is contained in:
parent
3e4f48c517
commit
f15b23c437
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user