refactor: calculate offsets upfront

This commit is contained in:
cha0s 2019-03-20 21:58:14 -05:00
parent 5a5169bf34
commit 30e286b68c
2 changed files with 15 additions and 13 deletions

View File

@ -92,8 +92,15 @@ class AnimatedBase extends Trait {
// Store keyed animation views.
this.animationViews = {};
this.animationsPromise.then((animations) => {
const animationParams = this.params.get('animations');
animations.forEach(({animation, key}) => {
this.animationViews[key] = new AnimationView(animation);
// Calculate any offset.
const animationView = this.animationViews[key];
const size = Rectangle.size(animation.sourceRectangle);
const halfway = Vector.scale(size, -0.5);
const {offset} = animationParams[key];
animationView.position = Vector.add(halfway, offset);
// Ensure animation is made visible upfront.
const isCurrentAnimation = key === this.entity.currentAnimation;
if (isCurrentAnimation) {
@ -111,12 +118,6 @@ class AnimatedBase extends Trait {
if (!animationView) {
return;
}
const animations = this.params.get('animations');
const {offset} = animations[key];
const {animation} = animationView;
const size = Rectangle.size(animation.sourceRectangle);
const halfway = Vector.scale(size, -0.5);
animationView.position = Vector.add(halfway, offset);
this.entity.container.addChild(animationView);
}

View File

@ -52,7 +52,14 @@ class PicturedBase extends Trait {
for (const key in images) {
const {uri} = images[key];
Image.load(uri).then((image) => {
this.sprites[key] = new Sprite(image);
const sprite = this.sprites[key] = new Sprite(image);
// Calculate any offset.
const images = this.params.get('images');
const size = image.size;
const halfway = Vector.scale(size, -0.5);
const {offset} = images[key];
sprite.position = Vector.add(halfway, offset);
// Set current image upfront.
const isCurrentImage = key === this.entity.currentImage;
if (isCurrentImage) {
this.showImage(key);
@ -69,12 +76,6 @@ class PicturedBase extends Trait {
if (!sprite) {
return;
}
const images = this.params.get('images');
const {offset} = images[key];
const {image} = sprite;
const size = image.size;
const halfway = Vector.scale(size, -0.5);
sprite.position = Vector.add(halfway, offset);
this.entity.container.addChild(sprite);
}