diff --git a/front/src/Exception/TextureError.ts b/front/src/Exception/TextureError.ts new file mode 100644 index 00000000..39a339f6 --- /dev/null +++ b/front/src/Exception/TextureError.ts @@ -0,0 +1 @@ +export class TextureError extends Error{} \ No newline at end of file diff --git a/front/src/Phaser/Entity/Character.ts b/front/src/Phaser/Entity/Character.ts index a1ed30d5..5c89447e 100644 --- a/front/src/Phaser/Entity/Character.ts +++ b/front/src/Phaser/Entity/Character.ts @@ -3,6 +3,7 @@ import {SpeechBubble} from "./SpeechBubble"; import BitmapText = Phaser.GameObjects.BitmapText; import Container = Phaser.GameObjects.Container; import Sprite = Phaser.GameObjects.Sprite; +import {TextureError} from "../../Exception/TextureError"; export interface PlayerResourceDescriptionInterface { name: string, @@ -94,6 +95,9 @@ export abstract class Character extends Container { public addTextures(textures: string[], frame?: string | number): void { for (const texture of textures) { + if(!this.scene.textures.exists(texture)){ + throw new TextureError('texture not found'); + } const sprite = new Sprite(this.scene, 0, 0, texture, frame); sprite.setInteractive({useHandCursor: true}); this.add(sprite); @@ -106,7 +110,9 @@ export abstract class Character extends Container { }); }) // Needed, otherwise, animations are not handled correctly. - this.scene.sys.updateList.add(sprite); + if(this.scene) { + this.scene.sys.updateList.add(sprite); + } this.sprites.set(texture, sprite); } } @@ -152,10 +158,7 @@ export abstract class Character extends Container { if (moving && (!sprite.anims.currentAnim || sprite.anims.currentAnim.key !== direction)) { sprite.play(texture+'-'+direction, true); } else if (!moving) { - /*if (this.anims.currentAnim) { - this.anims.stop(); - }*/ - sprite.play(texture+'-'+direction, true); + sprite.anims.play(texture + '-' + direction, true); sprite.anims.stop(); } } @@ -229,7 +232,9 @@ export abstract class Character extends Container { this.scene.events.removeListener('postupdate', this.postupdate.bind(this)); } for (const sprite of this.sprites.values()) { - this.scene.sys.updateList.remove(sprite); + if(this.scene) { + this.scene.sys.updateList.remove(sprite); + } } super.destroy(fromScene); this.playerName.destroy(); diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 2a331069..67380309 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -63,6 +63,8 @@ import {urlManager} from "../../Url/UrlManager"; import {PresentationModeIcon} from "../Components/PresentationModeIcon"; import {ChatModeIcon} from "../Components/ChatModeIcon"; import {OpenChatIcon, openChatIconName} from "../Components/OpenChatIcon"; +import {SelectCharacterScene, SelectCharacterSceneName} from "../Login/SelectCharacterScene"; +import {TextureError} from "../../Exception/TextureError"; export interface GameSceneInitInterface { initPosition: PointInterface|null, @@ -670,8 +672,12 @@ export class GameScene extends ResizableScene implements CenterListener { public cleanupClosingScene(): void { // We are completely destroying the current scene to avoid using a half-backed instance when coming back to the same map. - this.connection.closeConnection(); - this.simplePeer.unregister(); + if(this.connection) { + this.connection.closeConnection(); + } + if(this.simplePeer) { + this.simplePeer.unregister(); + } } private switchLayoutMode(): void { @@ -818,16 +824,23 @@ export class GameScene extends ResizableScene implements CenterListener { createCurrentPlayer(){ //initialise player //TODO create animation moving between exit and start - this.CurrentPlayer = new Player( - this, - this.startX, - this.startY, - this.playerName, - this.characterLayers, - PlayerAnimationNames.WalkDown, - false, - this.userInputManager - ); + try { + this.CurrentPlayer = new Player( + this, + this.startX, + this.startY, + this.playerName, + this.characterLayers, + PlayerAnimationNames.WalkDown, + false, + this.userInputManager + ); + }catch (err){ + if(err instanceof TextureError) { + gameManager.leaveGame(this, SelectCharacterSceneName, new SelectCharacterScene()); + } + throw err; + } //create collision this.createCollisionWithPlayer();