refactor: vertices

This commit is contained in:
cha0s 2021-01-04 00:31:34 -06:00
parent 5fcf87c5cd
commit 6eb07a81f3
3 changed files with 16 additions and 12 deletions

View File

@ -114,8 +114,8 @@ export default class Body extends AbstractBody {
return [body, shapePosition, [0, 0]];
}
const vectors = [];
for (let i = 0; i < shape._translatedVertices.length; i++) {
const vertice = shape._translatedVertices[i];
for (let i = 0; i < shape.vertices.length; i++) {
const vertice = shape.vertices[i];
const [x, y] = Vector.scale(vertice, 1 / SCALE);
vectors.push({x, y});
}

View File

@ -4,10 +4,14 @@ import Shape from './shape';
export default class PolygonShape extends Shape {
#cachedTranslatedVertices;
#vertices;
constructor(shape) {
super(shape);
this._translatedVertices = [];
this._vertices = [];
this.#cachedTranslatedVertices = [];
this.#vertices = [];
this.on([
'parentOriginChanged',
'parentRotationChanged',
@ -36,13 +40,13 @@ export default class PolygonShape extends Shape {
}
get aabb() {
if (0 === this._vertices.length) {
if (0 === this.#vertices.length) {
return [0, 0, 0, 0];
}
const min = [Infinity, Infinity];
const max = [-Infinity, -Infinity];
for (let i = 0; i < this._translatedVertices.length; i++) {
const vertice = this._translatedVertices[i];
for (let i = 0; i < this.#cachedTranslatedVertices.length; i++) {
const vertice = this.#cachedTranslatedVertices[i];
min[0] = vertice[0] < min[0] ? vertice[0] : min[0];
min[1] = vertice[1] < min[1] ? vertice[1] : min[1];
max[0] = vertice[0] > max[0] ? vertice[0] : max[0];
@ -61,18 +65,18 @@ export default class PolygonShape extends Shape {
const rotation = this.rotation + parentRotation;
const parentScale = this.parent ? this.parent.scale : 1;
const scale = this.scale * parentScale;
this._translatedVertices = this._vertices.map((vertice) => (
this.#cachedTranslatedVertices = this.#vertices.map((vertice) => (
Vertice.translate(vertice, origin, rotation, scale)
));
this.emit('aabbChanged');
}
get vertices() {
return this._vertices;
return this.#cachedTranslatedVertices;
}
set vertices(vertices) {
this._vertices = [...vertices];
this.#vertices = [...vertices];
this.emit('verticesChanged');
}

View File

@ -76,8 +76,8 @@ export default class ShapeView extends Renderable {
const primitives = new Primitives();
let firstVertice;
let lastVertice;
for (let i = 0; i < this.shape._translatedVertices.length; i++) {
const vertice = this.shape._translatedVertices[i];
for (let i = 0; i < this.shape.vertices.length; i++) {
const vertice = this.shape.vertices[i];
if (!firstVertice) {
firstVertice = vertice;
}