avocado-old/packages/physics/traits/shaped.trait.js
2019-05-05 04:26:35 -05:00

59 lines
1.1 KiB
JavaScript

import {compose} from '@avocado/core';
import {Trait} from '@avocado/entity';
import {shapeFromJSON} from '../shape-from-json';
import {ShapeView} from '../shape-view';
const decorate = compose(
);
export class Shaped extends decorate(Trait) {
static defaultParams() {
return {
shape: undefined,
};
}
static type() {
return 'shaped';
}
initialize() {
this._shape = shapeFromJSON(this.params.shape);
this.shapeView = undefined;
}
destroy() {
this._shape.destroy();
if (this.shapeView) {
this.shapeView.destroy();
this.shapeView = undefined;
}
}
get shape() {
return this._shape;
}
listeners() {
return {
traitAdded: (type) => {
if (AVOCADO_CLIENT) {
if (this.entity.is('visible') && this.entity.is('debuggable')) {
if (!this.shapeView) {
this.shapeView = new ShapeView(this.entity.shape);
this.shapeView.visible = this.entity.isDebugging;
this.shapeView.zIndex = 100;
this.entity.container.addChild(this.shapeView);
}
}
}
}
};
}
}