refactor: container position sync
This commit is contained in:
parent
fc2d873809
commit
ef1dbbe5b5
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user