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) { if (hasGraphics) {
this._container = new Container(); this._container = new Container();
} }
this.trackPosition = this.params.get('trackPosition');
if (this.shouldSynchronizePosition()) {
this.synchronizePosition();
}
} }
get container() { get container() {
return this._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() { listeners() {
const listeners = {}; const listeners = {};
if (hasGraphics && this.params.get('trackPosition')) { if (this.shouldSynchronizePosition()) {
listeners.traitAdded = (trait) => { listeners.traitAdded = (trait) => {
if ('positioned' === trait.constructor.type()) { if ('positioned' !== trait.constructor.type()) {
this.entity.container.position = this.entity.position; return;
} }
this.synchronizePosition();
}; };
listeners.xChanged = (x) => { listeners.positionChanged = () => {
this.entity.container.x = x; this.synchronizePosition();
};
listeners.yChanged = (y) => {
this.entity.container.y = y;
}; };
} }
return listeners; return listeners;