refactor: defaults

This commit is contained in:
cha0s 2021-04-01 17:14:31 -05:00
parent 348bb29213
commit 85b080d910

View File

@ -15,6 +15,8 @@ export default (latus) => class Pictured extends decorate(Trait) {
#currentImage = '';
#images = {};
#sprites = {};
constructor() {
@ -33,7 +35,7 @@ export default (latus) => class Pictured extends decorate(Trait) {
static defaultState() {
return {
currentImage: 'initial',
currentImage: 'default',
};
}
@ -97,7 +99,7 @@ export default (latus) => class Pictured extends decorate(Trait) {
if (this._cachedAabbs[key]) {
return this._cachedAabbs[key];
}
const size = this.sizeFor(key);
const {size} = this.#images[key];
if (Vector.isZero(size)) {
return [0, 0, 0, 0];
}
@ -154,19 +156,25 @@ export default (latus) => class Pictured extends decorate(Trait) {
async load(json = {}) {
await super.load(json);
const {images} = this.params;
this.#currentImage = this.state.currentImage;
if (Object.keys(this.params.images).length > 0) {
const {Image} = latus.get('%resources');
const sprites = await mapValuesAsync(
this.params.images,
async (json) => new Sprite(await Image.load(json.uri)),
);
Object.keys(sprites).forEach((key) => {
// eslint-disable-next-line no-param-reassign
sprites[key].position = this.offsetFor(key);
});
this.#sprites = sprites;
}
images.default = images.default || {};
images.default.uri = images.default.uri || this.entity.uri.replace('.entity.json', '.png');
const {Image} = latus.get('%resources');
this.#images = await mapValuesAsync(images, ({uri}) => Image.load(uri));
this.#sprites = await mapValuesAsync(this.#images, async (image) => new Sprite(image));
Object.keys(this.#sprites).forEach((key) => {
// eslint-disable-next-line no-param-reassign
this.#sprites[key].position = this.offsetFor(key);
});
}
methods() {
return {
image: (key) => this.#sprites[key]?.image,
};
}
offsetFor(key) {
@ -201,11 +209,4 @@ export default (latus) => class Pictured extends decorate(Trait) {
this.entity.container.addChild(sprite);
}
sizeFor(key) {
if (!this.params.images[key] || !this.params.images[key].size) {
return [0, 0];
}
return this.params.images[key].size;
}
};