refactor: graphicalBoundingBox

This commit is contained in:
cha0s 2019-03-26 17:17:09 -05:00
parent 43dec37764
commit b8b1b98580
4 changed files with 30 additions and 22 deletions

View File

@ -94,7 +94,7 @@ class AnimatedBase extends Trait {
animation.on('indexChanged', this.animationListeners[key]);
});
// Bounding box update.
this.entity.emit('boundingBoxesUpdated');
this.entity.emit('graphicalBoundingBoxesUpdated');
});
}
@ -158,7 +158,7 @@ class AnimatedBase extends Trait {
hooks() {
return {
boundingBoxes: () => {
graphicalBoundingBoxes: () => {
const key = this.entity.currentAnimation;
const animation = this.animations[key];
if (!animation) {
@ -181,7 +181,7 @@ class AnimatedBase extends Trait {
oldAnimation.reset();
}
// Bounding box update.
this.entity.emit('boundingBoxesUpdated');
this.entity.emit('graphicalBoundingBoxesUpdated');
// Only client/graphics.
if (!this.animationViews) {
return;

View File

@ -1,5 +1,6 @@
import {compose} from '@avocado/core';
import {hasGraphics, Container} from '@avocado/graphics';
import {Rectangle} from '@avocado/math';
import {StateProperty, Trait} from '../trait';
@ -31,6 +32,24 @@ class GraphicalBase extends Trait {
return this._container;
}
get graphicalBoundingBox() {
// Collect all bounding boxes.
const graphicalBoundingBoxes = this.entity.invokeHookFlat(
'graphicalBoundingBoxes'
);
if (0 === graphicalBoundingBoxes.length) {
return [0, 0, 0, 0];
}
let unifiedBoundingBox = [0, 0, 0, 0];
for (const graphicalBoundingBox of graphicalBoundingBoxes) {
unifiedBoundingBox = Rectangle.united(
unifiedBoundingBox,
graphicalBoundingBox,
);
}
return unifiedBoundingBox;
}
shouldSynchronizePosition() {
return this.entity.container && this.trackPosition;
}

View File

@ -34,22 +34,11 @@ export class Listed extends Trait {
return;
}
const quadTree = list.quadTree();
// Collect all bounding boxes.
const boundingBoxes = this.entity.invokeHookFlat('boundingBoxes');
if (0 === boundingBoxes.length) {
return;
}
let unifiedBoundingBox = [0, 0, 0, 0];
for (const boundingBox of boundingBoxes) {
unifiedBoundingBox = Rectangle.united(unifiedBoundingBox, boundingBox);
}
if (Rectangle.isNull(unifiedBoundingBox)) {
return;
}
const aabb = this.entity.graphicalBoundingBox;
// Break into 4 points.
const width = unifiedBoundingBox[2] - .0001;
const height = unifiedBoundingBox[3] - .0001;
const upperLeft = Rectangle.position(unifiedBoundingBox);
const width = aabb[2] - .0001;
const height = aabb[3] - .0001;
const upperLeft = Rectangle.position(aabb);
const upperRight = Vector.add(upperLeft, [width, 0]);
const lowerLeft = Vector.add(upperLeft, [0, height]);
const lowerRight = Vector.add(upperLeft, [width, height]);
@ -83,7 +72,7 @@ export class Listed extends Trait {
listeners() {
return {
boundingBoxesUpdated: () => {
graphicalBoundingBoxesUpdated: () => {
this.entity.resetQuadTreeNodes();
},

View File

@ -27,7 +27,7 @@ class PicturedBase extends Trait {
initialize() {
this.sprites = undefined;
// Bounding box update.
this.entity.emit('boundingBoxesUpdated');
this.entity.emit('graphicalBoundingBoxesUpdated');
}
destroy() {
@ -118,7 +118,7 @@ class PicturedBase extends Trait {
hooks() {
return {
boundingBoxes: () => {
graphicalBoundingBoxes: () => {
const key = this.entity.currentImage;
const images = this.params.get('images');
const image = images[key];
@ -136,7 +136,7 @@ class PicturedBase extends Trait {
return {
currentImageChanged: (oldKey) => {
// Bounding box update.
this.entity.emit('boundingBoxesUpdated');
this.entity.emit('graphicalBoundingBoxesUpdated');
// Only client/graphics.
if (!this.sprites) {
return;