chore: attach promises to hydrate

This commit is contained in:
cha0s 2020-06-21 23:06:05 -05:00
parent ed0ede0577
commit de5957fd05

View File

@ -103,7 +103,10 @@ export default class Animated extends decorate(Trait) {
}
hydrate() {
return this.promiseAnimations();
return Promise.all([
this.loadAnimations(),
this.loadAnimationImagesIfPossible(),
]);
}
jitterFor(key) {
@ -117,10 +120,22 @@ export default class Animated extends decorate(Trait) {
if (this.animationsPromise) {
return;
}
this.promiseAnimations();
if (!this.animationsPromise) {
const animationPromises = [];
// Load all animations.
for (const key in this._animations) {
const {uri} = this._animations[key];
const promise = Animation.load(uri).then((animation) => {
// Zip with key to make populating animations and views trivial.
return {animation, key};
});
animationPromises.push(promise);
}
this.animationsPromise = Promise.all(animationPromises);
}
// Store keyed animations.
this.animations = {};
this.animationsPromise.then((animations) => {
return this.animationsPromise.then((animations) => {
animations.forEach(({animation, key}) => {
this.animations[key] = animation;
// Set direction upfront.
@ -128,8 +143,6 @@ export default class Animated extends decorate(Trait) {
});
// Bounding box update.
this.entity.updateVisibleBoundingBox();
}).catch((error) => {
console.error(`Failed loading some or all animations for '${this.entity.uri}'.`);
});
}
@ -145,7 +158,7 @@ export default class Animated extends decorate(Trait) {
}
// Store keyed animation views.
this.animationViews = {};
this.animationsPromise.then((animations) => {
return this.animationsPromise.then((animations) => {
animations.forEach(({animation, key}) => {
this.animationViews[key] = new AnimationView(animation);
// Calculate any offset.
@ -158,8 +171,6 @@ export default class Animated extends decorate(Trait) {
}
});
this.setSpriteScale();
}).catch((error) => {
console.error(`Failed loading some or all animation images for '${this.entity.uri}'.`);
});
}
@ -180,23 +191,6 @@ export default class Animated extends decorate(Trait) {
}
}
promiseAnimations() {
if (!this.animationsPromise) {
const animationPromises = [];
// Load all animations.
for (const key in this._animations) {
const {uri} = this._animations[key];
const promise = Animation.load(uri).then((animation) => {
// Zip with key to make populating animations and views trivial.
return {animation, key};
});
animationPromises.push(promise);
}
this.animationsPromise = Promise.all(animationPromises);
}
return this.animationsPromise;
}
setSpriteScale() {
if (!this.animationViews) {
return;
@ -285,17 +279,6 @@ export default class Animated extends decorate(Trait) {
this.setSpriteScale();
},
traitAdded: (type) => {
if (-1 === [
'animated',
'visible',
].indexOf(type)) {
return;
}
this.loadAnimations();
this.loadAnimationImagesIfPossible();
},
};
}