2021-04-01 00:33:05 +02:00
|
|
|
import LoaderPlugin = Phaser.Loader.LoaderPlugin;
|
|
|
|
import { COMPANION_RESOURCES, CompanionResourceDescriptionInterface } from "./CompanionTextures";
|
|
|
|
|
2021-04-02 23:13:03 +02:00
|
|
|
export const getAllResources = (): CompanionResourceDescriptionInterface[] => {
|
|
|
|
return COMPANION_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const lazyLoadAllResources = (loader: LoaderPlugin): Promise<CompanionResourceDescriptionInterface[]> => {
|
2021-04-02 23:00:51 +02:00
|
|
|
const promises: Promise<string>[] = [];
|
2021-04-01 00:33:05 +02:00
|
|
|
|
2021-04-02 23:00:51 +02:00
|
|
|
COMPANION_RESOURCES.forEach((resource: CompanionResourceDescriptionInterface) => {
|
|
|
|
promises.push(lazyLoadResource(loader, resource.name));
|
2021-04-01 00:33:05 +02:00
|
|
|
});
|
|
|
|
|
2021-04-02 23:00:51 +02:00
|
|
|
return Promise.all(promises).then(() => {
|
|
|
|
return COMPANION_RESOURCES;
|
|
|
|
});
|
2021-04-01 00:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const lazyLoadResource = (loader: LoaderPlugin, name: string): Promise<string> => {
|
2021-04-02 21:26:24 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const resource = COMPANION_RESOURCES.find(item => item.name === name);
|
2021-04-01 00:33:05 +02:00
|
|
|
|
2021-04-02 21:26:24 +02:00
|
|
|
if (typeof resource === 'undefined') {
|
|
|
|
return reject(`Texture '${name}' not found!`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loader.textureManager.exists(resource.name)) {
|
|
|
|
return resolve(resource.name);
|
|
|
|
}
|
|
|
|
|
2021-04-01 00:33:05 +02:00
|
|
|
loader.spritesheet(resource.name, resource.img, { frameWidth: 32, frameHeight: 32, endFrame: 12 });
|
|
|
|
loader.once(`filecomplete-spritesheet-${resource.name}`, () => resolve(resource.name));
|
|
|
|
});
|
|
|
|
}
|