refactor: cleans, checks, searching moved to list
This commit is contained in:
parent
9a1b19c00b
commit
222a0247b3
|
@ -1,15 +1,11 @@
|
|||
import * as I from 'immutable';
|
||||
|
||||
import {arrayUnique} from '@avocado/core';
|
||||
import {Trait} from '@avocado/entity';
|
||||
import {Rectangle, Vector} from '@avocado/math';
|
||||
|
||||
export class Listed extends Trait {
|
||||
|
||||
initialize() {
|
||||
this.lastNearby = I.Set();
|
||||
this._list = undefined;
|
||||
this.quadTreeNodes = [];
|
||||
this.quadTreeNodes = undefined;
|
||||
}
|
||||
|
||||
destroy() {
|
||||
|
@ -29,11 +25,14 @@ export class Listed extends Trait {
|
|||
}
|
||||
|
||||
addQuadTreeNodes() {
|
||||
const list = this.entity.list;
|
||||
const list = this._list;
|
||||
if (!list) {
|
||||
return;
|
||||
}
|
||||
const quadTree = list.quadTree();
|
||||
const quadTree = list.quadTree;
|
||||
if (!('graphicalBoundingBox' in this.entity)) {
|
||||
return;
|
||||
}
|
||||
const aabb = this.entity.graphicalBoundingBox;
|
||||
const points = Rectangle.toPoints(aabb);
|
||||
this.quadTreeNodes = points.map((point) => [...point, this.entity]);
|
||||
|
@ -44,57 +43,41 @@ export class Listed extends Trait {
|
|||
}
|
||||
|
||||
removeQuadTreeNodes() {
|
||||
const list = this.entity.list;
|
||||
const list = this._list;
|
||||
if (!list) {
|
||||
return;
|
||||
}
|
||||
if (this.quadTreeNodes) {
|
||||
const quadTree = list.quadTree();
|
||||
const quadTree = list.quadTree;
|
||||
for (const node of this.quadTreeNodes) {
|
||||
quadTree.remove(node);
|
||||
}
|
||||
this.quadTreeNodes = undefined;
|
||||
}
|
||||
this.quadTreeNodes = [];
|
||||
}
|
||||
|
||||
resetQuadTreeNodes() {
|
||||
this.removeQuadTreeNodes();
|
||||
this.addQuadTreeNodes();
|
||||
}
|
||||
|
||||
listeners() {
|
||||
return {
|
||||
|
||||
graphicalBoundingBoxesUpdated: () => {
|
||||
this.entity.resetQuadTreeNodes();
|
||||
this.resetQuadTreeNodes();
|
||||
},
|
||||
|
||||
positionChanged: () => {
|
||||
this.entity.resetQuadTreeNodes();
|
||||
this.resetQuadTreeNodes();
|
||||
},
|
||||
|
||||
traitAdded: () => {
|
||||
this.entity.resetQuadTreeNodes();
|
||||
this.resetQuadTreeNodes();
|
||||
},
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
methods() {
|
||||
return {
|
||||
|
||||
nearbyEntities: (size, position = this.entity.position) => {
|
||||
const quadTree = this.entity.list.quadTree();
|
||||
position = Vector.add(position, Vector.scale(size, -0.5));
|
||||
const query = Rectangle.compose(position, size);
|
||||
const entities = quadTree.search(query).map((node) => {
|
||||
return node.data[2];
|
||||
});
|
||||
// Hitting multiple points for each entity can return duplicates.
|
||||
return arrayUnique(entities);
|
||||
},
|
||||
|
||||
resetQuadTreeNodes: () => {
|
||||
this.removeQuadTreeNodes();
|
||||
this.addQuadTreeNodes();
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user