partey_workadventure/front/src/Phaser/Components/Loader.ts

42 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-02-01 16:52:28 +01:00
import ImageFrameConfig = Phaser.Types.Loader.FileTypes.ImageFrameConfig;
const LogoNameIndex: string = 'logoLoading';
const TextName: string = 'Loading...';
2021-01-30 20:33:31 +01:00
const LogoResource: string = 'resources/logos/logo.png';
2021-02-01 16:52:28 +01:00
const LogoFrame: ImageFrameConfig = {frameWidth: 307, frameHeight: 59};
2021-01-30 20:33:31 +01:00
export const addLoader = (scene: Phaser.Scene): void => {
2021-02-01 16:52:28 +01:00
let loadingText: Phaser.GameObjects.Text|null = null;
2021-01-30 20:33:31 +01:00
const promiseLoadLogoTexture = new Promise<Phaser.GameObjects.Image>((res) => {
2021-02-01 16:52:28 +01:00
if(scene.load.textureManager.exists(LogoNameIndex)){
2021-01-30 20:33:31 +01:00
return res(scene.add.image(scene.game.renderer.width / 2, 100, LogoNameIndex));
2021-02-01 16:52:28 +01:00
}else{
//add loading if logo image is not ready
loadingText = scene.add.text(scene.game.renderer.width / 2, 200, TextName);
2021-01-30 20:33:31 +01:00
}
2021-02-01 16:52:28 +01:00
scene.load.spritesheet(LogoNameIndex, LogoResource, LogoFrame);
scene.load.once(`filecomplete-spritesheet-${LogoNameIndex}`, () => {
if(loadingText){
loadingText.destroy();
}
return res(scene.add.image(scene.game.renderer.width / 2, 100, LogoNameIndex));
2021-01-30 20:33:31 +01:00
});
});
const progress = scene.add.graphics();
scene.load.on('progress', (value: number) => {
progress.clear();
progress.fillStyle(0xffffff, 1);
progress.fillRect(0, 270, 800 * value, 60);
});
scene.load.on('complete', () => {
2021-02-01 16:52:28 +01:00
if(loadingText){
loadingText.destroy();
}
2021-01-30 20:33:31 +01:00
promiseLoadLogoTexture.then((resLoadingImage: Phaser.GameObjects.Image) => {
resLoadingImage.destroy();
});
progress.destroy();
});
}