refactor: Pictured

This commit is contained in:
cha0s 2022-05-04 08:08:37 -05:00
parent 43e81ca9fa
commit 034f6ed267
2 changed files with 24 additions and 14 deletions

View File

@ -22,6 +22,10 @@ export default class Atlas extends JsonResource {
return this.$$image; return this.$$image;
} }
get size() {
return this.$$size;
}
async load(json = {}) { async load(json = {}) {
await super.load(json); await super.load(json);
this.destroy(); this.destroy();
@ -32,9 +36,10 @@ export default class Atlas extends JsonResource {
this.$$image = await Image.load(imageUri); this.$$image = await Image.load(imageUri);
switch (type) { switch (type) {
case 'grid': { case 'grid': {
const {size} = json; let {size} = json;
this.$$size = size;
if (Vector.isNull(size)) { if (Vector.isNull(size)) {
return; ({size} = this.$$image);
} }
const grid = Vector.div(this.$$image.size, size); const grid = Vector.div(this.$$image.size, size);
const rectangle = Rectangle.compose([0, 0], size); const rectangle = Rectangle.compose([0, 0], size);

View File

@ -2,7 +2,7 @@ import {mapValuesAsync} from '@avocado/core';
import {Rectangle, Vector} from '@avocado/math'; import {Rectangle, Vector} from '@avocado/math';
import {compose} from '@flecks/core'; import {compose} from '@flecks/core';
import Image from '../image'; import Atlas from '../atlas';
import Sprite from '../sprite'; import Sprite from '../sprite';
export default (flecks) => { export default (flecks) => {
@ -32,6 +32,7 @@ export default (flecks) => {
static defaultParams() { static defaultParams() {
return { return {
atlas: {},
images: {}, images: {},
}; };
} }
@ -135,18 +136,22 @@ export default (flecks) => {
async load(json = {}) { async load(json = {}) {
await super.load(json); await super.load(json);
const images = Object.fromEntries( const images = {};
Object.entries({ const atlas = await Atlas.load({
...this.params.images, imageUri: this.entity.uri?.replace('.entity.json', '.png'),
default: { ...this.params.atlas,
uri: this.entity.uri?.replace('.entity.json', '.png'), });
...(this.params.images.default || {}), const keys = Object.keys(this.params.images);
}, if (0 === keys.length) {
}) images.default = atlas.subimage(0);
.filter(([, {uri}]) => !!uri), }
); else {
for (let i = 0; i < keys.length; ++i) {
images[keys[i]] = atlas.subimage(i);
}
}
this.$$currentImage = this.state.currentImage; this.$$currentImage = this.state.currentImage;
this.$$images = await mapValuesAsync(images, ({uri}) => Image.load(uri)); this.$$images = images;
this.$$sprites = await mapValuesAsync(this.$$images, async (image) => new Sprite(image)); this.$$sprites = await mapValuesAsync(this.$$images, async (image) => new Sprite(image));
Object.keys(this.$$sprites).forEach((key) => { Object.keys(this.$$sprites).forEach((key) => {
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign