Merge pull request #1207 from thecodingmachine/fixLoadError

FIX: loading errors after the preload stage should not crash the game anymore
This commit is contained in:
Kharhamel 2021-06-21 11:49:14 +02:00 committed by GitHub
commit 3d1c2dc05a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 8 deletions

View File

@ -59,9 +59,11 @@ export const lazyLoadPlayerCharacterTextures = (loadPlugin: LoaderPlugin, textur
} else {
returnPromise = Promise.resolve(texturekeys);
}
//If the loading fail, we render the default model instead.
return returnPromise.then((keys) => keys.map((key) => {
return typeof key !== 'string' ? key.name : key;
}))
})).catch(() => lazyLoadPlayerCharacterTextures(loadPlugin, ["color_22", "eyes_23"]));
}
export const getRessourceDescriptor = (textureKey: string|BodyResourceDescriptionInterface): BodyResourceDescriptionInterface => {
@ -80,11 +82,25 @@ export const getRessourceDescriptor = (textureKey: string|BodyResourceDescriptio
}
export const createLoadingPromise = (loadPlugin: LoaderPlugin, playerResourceDescriptor: BodyResourceDescriptionInterface, frameConfig: FrameConfig) => {
return new Promise<BodyResourceDescriptionInterface>((res) => {
return new Promise<BodyResourceDescriptionInterface>((res, rej) => {
console.log('count', loadPlugin.listenerCount('loaderror'));
if (loadPlugin.textureManager.exists(playerResourceDescriptor.name)) {
return res(playerResourceDescriptor);
}
loadPlugin.spritesheet(playerResourceDescriptor.name, playerResourceDescriptor.img, frameConfig);
loadPlugin.once('filecomplete-spritesheet-' + playerResourceDescriptor.name, () => res(playerResourceDescriptor));
const errorCallback = (file: {src: string}) => {
if (file.src !== playerResourceDescriptor.img) return;
console.error('failed loading player ressource: ', playerResourceDescriptor)
rej(playerResourceDescriptor);
loadPlugin.off('filecomplete-spritesheet-' + playerResourceDescriptor.name, successCallback);
loadPlugin.off('loaderror', errorCallback);
}
const successCallback = () => {
loadPlugin.off('loaderror', errorCallback);
res(playerResourceDescriptor);
}
loadPlugin.once('filecomplete-spritesheet-' + playerResourceDescriptor.name, successCallback);
loadPlugin.on('loaderror', errorCallback);
});
}

View File

@ -192,6 +192,7 @@ export class GameScene extends DirtyScene implements CenterListener {
private pinchManager: PinchManager|undefined;
private mapTransitioning: boolean = false; //used to prevent transitions happenning at the same time.
private emoteManager!: EmoteManager;
private preloading: boolean = true;
constructor(private room: Room, MapUrlFile: string, customKey?: string|undefined) {
super({
@ -259,11 +260,15 @@ export class GameScene extends DirtyScene implements CenterListener {
return;
}
this.scene.start(ErrorSceneName, {
title: 'Network error',
subTitle: 'An error occurred while loading resource:',
message: this.originalMapUrl ?? file.src
});
//once preloading is over, we don't want loading errors to crash the game, so we need to disable this behavior after preloading.
console.error('Error when loading: ', file);
if (this.preloading) {
this.scene.start(ErrorSceneName, {
title: 'Network error',
subTitle: 'An error occurred while loading resource:',
message: this.originalMapUrl ?? file.src
});
}
});
this.load.on('filecomplete-tilemapJSON-'+this.MapUrlFile, (key: string, type: string, data: unknown) => {
this.onMapLoad(data);
@ -388,6 +393,7 @@ export class GameScene extends DirtyScene implements CenterListener {
//hook create scene
create(): void {
this.preloading = false;
this.trackDirtyAnims();
gameManager.gameSceneIsCreated(this);