feat: ShapeView
This commit is contained in:
parent
d9cf9257c1
commit
6c26e87840
|
@ -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';
|
||||
|
|
62
packages/graphics/shape-view.js
Normal file
62
packages/graphics/shape-view.js
Normal 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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user