refactor: calculate offsets upfront
This commit is contained in:
parent
5a5169bf34
commit
30e286b68c
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user