workadventure/front/src/Phaser/Companion/CompanionTexturesLoadingManager.ts

35 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-04-01 00:33:05 +02:00
import LoaderPlugin = Phaser.Loader.LoaderPlugin;
import { COMPANION_RESOURCES, CompanionResourceDescriptionInterface } from "./CompanionTextures";
2022-02-02 11:10:52 +01:00
import CancelablePromise from "cancelable-promise";
2021-04-01 00:33:05 +02:00
2021-04-06 18:54:45 +02:00
export const getAllCompanionResources = (loader: LoaderPlugin): CompanionResourceDescriptionInterface[] => {
2021-04-02 23:00:51 +02:00
COMPANION_RESOURCES.forEach((resource: CompanionResourceDescriptionInterface) => {
2022-01-04 16:48:47 +01:00
lazyLoadCompanionResource(loader, resource.name).catch((e) => console.error(e));
2021-04-01 00:33:05 +02:00
});
2021-04-06 18:54:45 +02:00
return COMPANION_RESOURCES;
2021-09-06 14:27:54 +02:00
};
2021-04-01 00:33:05 +02:00
2022-02-02 11:10:52 +01:00
export const lazyLoadCompanionResource = (loader: LoaderPlugin, name: string): CancelablePromise<string> => {
return new CancelablePromise((resolve, reject, cancel) => {
cancel(() => {
return;
});
2021-09-06 14:27:54 +02:00
const resource = COMPANION_RESOURCES.find((item) => item.name === name);
2021-04-01 00:33:05 +02:00
2021-09-06 14:27:54 +02:00
if (typeof resource === "undefined") {
return reject(`Texture '${name}' not found!`);
}
2021-09-06 14:27:54 +02:00
if (loader.textureManager.exists(resource.name)) {
return resolve(resource.name);
}
2021-09-06 14:27:54 +02:00
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));
2021-09-06 14:27:54 +02:00
loader.start(); // It's only automatically started during the Scene preload.
2021-04-01 00:33:05 +02:00
});
2021-09-06 14:27:54 +02:00
};