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:
commit
3d1c2dc05a
@ -59,9 +59,11 @@ export const lazyLoadPlayerCharacterTextures = (loadPlugin: LoaderPlugin, textur
|
|||||||
} else {
|
} else {
|
||||||
returnPromise = Promise.resolve(texturekeys);
|
returnPromise = Promise.resolve(texturekeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//If the loading fail, we render the default model instead.
|
||||||
return returnPromise.then((keys) => keys.map((key) => {
|
return returnPromise.then((keys) => keys.map((key) => {
|
||||||
return typeof key !== 'string' ? key.name : key;
|
return typeof key !== 'string' ? key.name : key;
|
||||||
}))
|
})).catch(() => lazyLoadPlayerCharacterTextures(loadPlugin, ["color_22", "eyes_23"]));
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getRessourceDescriptor = (textureKey: string|BodyResourceDescriptionInterface): BodyResourceDescriptionInterface => {
|
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) => {
|
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)) {
|
if (loadPlugin.textureManager.exists(playerResourceDescriptor.name)) {
|
||||||
return res(playerResourceDescriptor);
|
return res(playerResourceDescriptor);
|
||||||
}
|
}
|
||||||
loadPlugin.spritesheet(playerResourceDescriptor.name, playerResourceDescriptor.img, frameConfig);
|
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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -192,6 +192,7 @@ export class GameScene extends DirtyScene implements CenterListener {
|
|||||||
private pinchManager: PinchManager|undefined;
|
private pinchManager: PinchManager|undefined;
|
||||||
private mapTransitioning: boolean = false; //used to prevent transitions happenning at the same time.
|
private mapTransitioning: boolean = false; //used to prevent transitions happenning at the same time.
|
||||||
private emoteManager!: EmoteManager;
|
private emoteManager!: EmoteManager;
|
||||||
|
private preloading: boolean = true;
|
||||||
|
|
||||||
constructor(private room: Room, MapUrlFile: string, customKey?: string|undefined) {
|
constructor(private room: Room, MapUrlFile: string, customKey?: string|undefined) {
|
||||||
super({
|
super({
|
||||||
@ -259,11 +260,15 @@ export class GameScene extends DirtyScene implements CenterListener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.scene.start(ErrorSceneName, {
|
//once preloading is over, we don't want loading errors to crash the game, so we need to disable this behavior after preloading.
|
||||||
title: 'Network error',
|
console.error('Error when loading: ', file);
|
||||||
subTitle: 'An error occurred while loading resource:',
|
if (this.preloading) {
|
||||||
message: this.originalMapUrl ?? file.src
|
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.load.on('filecomplete-tilemapJSON-'+this.MapUrlFile, (key: string, type: string, data: unknown) => {
|
||||||
this.onMapLoad(data);
|
this.onMapLoad(data);
|
||||||
@ -388,6 +393,7 @@ export class GameScene extends DirtyScene implements CenterListener {
|
|||||||
|
|
||||||
//hook create scene
|
//hook create scene
|
||||||
create(): void {
|
create(): void {
|
||||||
|
this.preloading = false;
|
||||||
this.trackDirtyAnims();
|
this.trackDirtyAnims();
|
||||||
|
|
||||||
gameManager.gameSceneIsCreated(this);
|
gameManager.gameSceneIsCreated(this);
|
||||||
|
Loading…
Reference in New Issue
Block a user