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

55 lines
2.4 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 => {
// If there is nothing to load, do not display the loader.
if (scene.load.list.entries.length === 0) {
return;
}
2021-02-01 16:52:28 +01:00
let loadingText: Phaser.GameObjects.Text|null = null;
2021-02-02 17:11:52 +01:00
const loadingBarWidth: number = Math.floor(scene.game.renderer.width / 3);
const loadingBarHeight: number = 16;
const padding: number = 5;
2021-02-01 16:52:28 +01:00
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-02-02 17:11:52 +01:00
return res(scene.add.image(scene.game.renderer.width / 2, scene.game.renderer.height / 2 - 150, LogoNameIndex));
2021-02-01 16:52:28 +01:00
}else{
//add loading if logo image is not ready
2021-02-02 17:11:52 +01:00
loadingText = scene.add.text(scene.game.renderer.width / 2, scene.game.renderer.height / 2 - 50, 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();
}
2021-02-02 17:11:52 +01:00
return res(scene.add.image(scene.game.renderer.width / 2, scene.game.renderer.height / 2 - 150, LogoNameIndex));
2021-01-30 20:33:31 +01:00
});
});
2021-02-02 17:11:52 +01:00
const progressContainer = scene.add.graphics();
const progress = scene.add.graphics();
2021-02-02 17:11:52 +01:00
progressContainer.fillStyle(0x444444, 0.8);
progressContainer.fillRect((scene.game.renderer.width - loadingBarWidth) / 2 - padding, scene.game.renderer.height / 2 + 50 - padding, loadingBarWidth + padding * 2, loadingBarHeight + padding * 2);
scene.load.on('progress', (value: number) => {
progress.clear();
2021-02-02 17:11:52 +01:00
progress.fillStyle(0xBBBBBB, 1);
progress.fillRect((scene.game.renderer.width - loadingBarWidth) / 2, scene.game.renderer.height / 2 + 50, loadingBarWidth * value, loadingBarHeight);
});
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();
2021-02-02 17:11:52 +01:00
progressContainer.destroy();
});
2021-02-02 17:11:52 +01:00
}