avocado-old/packages/entity/traits/graphical.js

50 lines
1.0 KiB
JavaScript
Raw Normal View History

2019-03-18 20:07:05 -05:00
import {compose} from '@avocado/core';
import {hasGraphics, Container} from '@avocado/graphics';
2019-03-18 20:07:05 -05:00
import {simpleState, Trait} from '../trait';
const decorate = compose(
);
class GraphicalBase extends Trait {
static defaultParams() {
return {
trackPosition: true,
};
}
constructor(...args) {
super(...args);
if (hasGraphics) {
this._container = new Container();
}
2019-03-18 20:07:05 -05:00
}
get container() {
return this._container;
}
listeners() {
2019-03-19 10:21:00 -05:00
const listeners = {};
2019-03-18 20:07:05 -05:00
const trackPosition = this.params.get('trackPosition');
if (trackPosition && hasGraphics) {
2019-03-19 10:21:00 -05:00
listeners.traitAdded = (trait) => {
2019-03-18 20:07:05 -05:00
if ('positioned' === trait.constructor.type()) {
this.entity.container.position = this.entity.position;
2019-03-18 20:07:05 -05:00
}
2019-03-19 10:21:00 -05:00
};
listeners.xChanged = (x) => {
this.entity.container.x = x;
2019-03-19 10:21:00 -05:00
};
listeners.yChanged = (y) => {
this.entity.container.y = y;
2019-03-19 10:21:00 -05:00
};
2019-03-18 20:07:05 -05:00
}
2019-03-19 10:21:00 -05:00
return listeners;
2019-03-18 20:07:05 -05:00
}
}
export class Graphical extends decorate(GraphicalBase) {}