From ca21ad737878ddffe5aa6dc81512a38076b26352 Mon Sep 17 00:00:00 2001 From: cha0s Date: Fri, 19 Apr 2019 16:32:17 -0500 Subject: [PATCH] refactor: sprite default anchor to middle --- packages/graphics/sprite.js | 1 + packages/graphics/traits/pictured.trait.js | 1 - packages/timing/traits/animated.trait.js | 15 ++------------- packages/topdown/layer-view.js | 1 + packages/topdown/tiles-renderer.js | 1 + 5 files changed, 5 insertions(+), 14 deletions(-) diff --git a/packages/graphics/sprite.js b/packages/graphics/sprite.js index 9ee7b0e..2371db9 100644 --- a/packages/graphics/sprite.js +++ b/packages/graphics/sprite.js @@ -8,6 +8,7 @@ export class Sprite extends Renderable { super(); this._image = image; this.sprite = new PIXI.Sprite(image.texture); + this.anchor = [0.5, 0.5]; } destroy() { diff --git a/packages/graphics/traits/pictured.trait.js b/packages/graphics/traits/pictured.trait.js index 9f77575..3ce34a5 100644 --- a/packages/graphics/traits/pictured.trait.js +++ b/packages/graphics/traits/pictured.trait.js @@ -65,7 +65,6 @@ export class Pictured extends decorate(Trait) { const {uri} = this._images[key]; const imagePromise = Image.load(uri).then((image) => { const sprite = this.sprites[key] = new Sprite(image); - sprite.anchor = [0.5, 0.5]; // Calculate any offset. sprite.position = this.offsetFor(key); // Set current image upfront. diff --git a/packages/timing/traits/animated.trait.js b/packages/timing/traits/animated.trait.js index cb507d7..323c880 100644 --- a/packages/timing/traits/animated.trait.js +++ b/packages/timing/traits/animated.trait.js @@ -107,7 +107,7 @@ class AnimatedBase extends Trait { this.animationViews[key] = new AnimationView(animation); // Calculate any offset. const animationView = this.animationViews[key]; - animationView.position = this.viewPositionFor(key); + animationView.position = this.offsetFor(key); // Ensure animation is made visible upfront. const isCurrentAnimation = key === this.entity.currentAnimation; if (isCurrentAnimation) { @@ -135,17 +135,6 @@ class AnimatedBase extends Trait { this.entity.container.addChild(animationView); } - viewPositionFor(key) { - const animation = this.animations[key]; - if (!animation) { - return [0, 0]; - } - const size = animation.frameSize; - const halfway = Vector.scale(size, -0.5); - const offset = this.offsetFor(key); - return Vector.add(halfway, offset); - } - hooks() { return { @@ -155,7 +144,7 @@ class AnimatedBase extends Trait { if (!animation) { return [0, 0, 0, 0]; } - const viewPosition = this.viewPositionFor(key); + const viewPosition = this.offsetFor(key); const position = Vector.add(this.entity.position, viewPosition); return Rectangle.compose(position, animation.frameSize); }, diff --git a/packages/topdown/layer-view.js b/packages/topdown/layer-view.js index ae57d11..55fab77 100644 --- a/packages/topdown/layer-view.js +++ b/packages/topdown/layer-view.js @@ -72,6 +72,7 @@ export class LayerView extends Container { const tilesRenderer = new TilesRenderer(this.layer.tiles, this.layer.tileset); const chunk = tilesRenderer.renderChunk(this.renderer); const tilesSprite = new Sprite(chunk); + tilesSprite.anchor = [0, 0]; this.layerContainer.addChild(tilesSprite); } diff --git a/packages/topdown/tiles-renderer.js b/packages/topdown/tiles-renderer.js index f3ed187..74af1bd 100644 --- a/packages/topdown/tiles-renderer.js +++ b/packages/topdown/tiles-renderer.js @@ -30,6 +30,7 @@ export class TilesRenderer { const subimage = this.tileset.subimage(index); if (subimage) { const sprite = new Sprite(subimage); + sprite.anchor = [0, 0]; sprite.position = position; container.addChild(sprite); }