2019-03-22 13:16:07 -05:00
|
|
|
import {compose} from '@avocado/core';
|
2019-03-23 18:26:35 -05:00
|
|
|
import {ShapeView} from '@avocado/graphics';
|
2019-03-22 13:16:07 -05:00
|
|
|
import {Vector} from '@avocado/math';
|
|
|
|
import {shapeFromJSON} from '@avocado/physics';
|
|
|
|
|
|
|
|
import {simpleState, Trait} from '../trait';
|
|
|
|
|
|
|
|
const decorate = compose(
|
|
|
|
);
|
|
|
|
|
|
|
|
export class Physical extends decorate(Trait) {
|
|
|
|
|
|
|
|
static defaultParams() {
|
|
|
|
return {
|
|
|
|
shape: undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
initialize() {
|
2019-03-23 18:26:35 -05:00
|
|
|
this._body = undefined;
|
2019-03-22 13:16:07 -05:00
|
|
|
this._shape = shapeFromJSON(this.params.get('shape'));
|
2019-03-23 18:26:35 -05:00
|
|
|
this._shapeView = undefined;
|
|
|
|
this._world = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
if (this._world) {
|
|
|
|
this._world.removeBody(this._body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get body() {
|
|
|
|
return this._body;
|
2019-03-22 13:16:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
get shape() {
|
|
|
|
return this._shape;
|
|
|
|
}
|
|
|
|
|
2019-03-23 18:26:35 -05:00
|
|
|
set world(world) {
|
|
|
|
this._world = world;
|
|
|
|
if (world) {
|
|
|
|
this._body = world.createBodyForEntity(this.entity);
|
|
|
|
world.addBody(this._body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners() {
|
|
|
|
return {
|
|
|
|
|
|
|
|
addedToList: () => {
|
|
|
|
this.entity.world = this.entity.list.world;
|
|
|
|
},
|
|
|
|
|
|
|
|
positionChanged: () => {
|
|
|
|
if (this._body) {
|
|
|
|
this._body.position = this.entity.position;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
traitAdded: (type) => {
|
|
|
|
if (!this._shapeView && this.entity.container) {
|
|
|
|
this._shapeView = new ShapeView(this.entity.shape);
|
|
|
|
this._shapeView.zIndex = 100;
|
|
|
|
this.entity.container.addChild(this._shapeView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
methods() {
|
|
|
|
return {
|
|
|
|
|
|
|
|
applyForce: (force) => {
|
|
|
|
if (this._world) {
|
|
|
|
this._body.applyForce(force);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
applyImpulse: (impulse) => {
|
|
|
|
if (this._world) {
|
|
|
|
this._body.applyImpulse(impulse);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 13:16:07 -05:00
|
|
|
}
|