From ef1dbbe5b50ddb6080eeba8b10fc2692a03a7fff Mon Sep 17 00:00:00 2001 From: cha0s Date: Tue, 19 Mar 2019 11:03:48 -0500 Subject: [PATCH] refactor: container position sync --- packages/entity/traits/graphical.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/entity/traits/graphical.js b/packages/entity/traits/graphical.js index 3fd2fed..3fad561 100644 --- a/packages/entity/traits/graphical.js +++ b/packages/entity/traits/graphical.js @@ -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;