feat: ShapeView

This commit is contained in:
cha0s 2019-03-22 13:15:57 -05:00
parent d9cf9257c1
commit 6c26e87840
2 changed files with 63 additions and 0 deletions

View File

@ -6,4 +6,5 @@ export {Image} from './image';
export {Primitives} from './primitives';
export {Renderable} from './renderable';
export {Renderer} from './renderer';
export {ShapeView} from './shape-view';
export {Sprite} from './sprite';

View File

@ -0,0 +1,62 @@
import {ShapeList, PolygonShape} from '@avocado/physics';
import {Color} from './color';
import {Container} from './container';
import {Primitives} from './primitives';
import {Renderable} from './renderable';
export class ShapeView extends Renderable {
constructor(shape) {
super();
this.container = new Container();
this.shape = shape;
if (shape instanceof PolygonShape) {
this.redrawPolygonLines();
shape.on('aabbChanged', () => {
this.redrawPolygonLines();
});
}
if (shape instanceof ShapeList) {
for (const child of shape) {
const childShapeView = new ShapeView(child);
this.container.addChild(childShapeView);
}
}
this.container.position = shape.position;
shape.on('positionChanged', () => {
this.container.position = shape.position;
})
}
get internal() {
return this.container.internal;
}
redrawPolygonLines() {
const primitives = new Primitives();
let firstVertice;
let lastVertice;
for (const vertice of this.shape) {
if (!firstVertice) {
firstVertice = vertice;
}
if (lastVertice) {
primitives.drawLine(
lastVertice,
vertice,
Primitives.lineStyle(new Color(255, 0, 255), 1),
);
}
lastVertice = vertice;
}
primitives.drawLine(
lastVertice,
firstVertice,
Primitives.lineStyle(new Color(255, 0, 255), 1),
);
this.container.removeAllChildren();
this.container.addChild(primitives);
}
}