refactor: container position sync

This commit is contained in:
cha0s 2019-03-19 11:03:48 -05:00
parent fc2d873809
commit ef1dbbe5b5

View File

@ -19,25 +19,37 @@ class GraphicalBase extends Trait {
if (hasGraphics) {
this._container = new Container();
}
this.trackPosition = this.params.get('trackPosition');
if (this.shouldSynchronizePosition()) {
this.synchronizePosition();
}
}
get container() {
return this._container;
}
shouldSynchronizePosition() {
return hasGraphics && this.trackPosition && 'position' in this.entity;
}
synchronizePosition() {
if ('position' in this.entity) {
this._container.position = this.entity.position;
}
}
listeners() {
const listeners = {};
if (hasGraphics && this.params.get('trackPosition')) {
if (this.shouldSynchronizePosition()) {
listeners.traitAdded = (trait) => {
if ('positioned' === trait.constructor.type()) {
this.entity.container.position = this.entity.position;
if ('positioned' !== trait.constructor.type()) {
return;
}
this.synchronizePosition();
};
listeners.xChanged = (x) => {
this.entity.container.x = x;
};
listeners.yChanged = (y) => {
this.entity.container.y = y;
listeners.positionChanged = () => {
this.synchronizePosition();
};
}
return listeners;