From 187e21eed98c3b017eb366aa019920203c3207f0 Mon Sep 17 00:00:00 2001 From: Johannes Berthel Date: Tue, 6 Apr 2021 19:10:18 +0200 Subject: [PATCH] load texture inside game scene instead inside of inside companion class --- front/src/Phaser/Companion/Companion.ts | 12 +++++------- front/src/Phaser/Entity/Character.ts | 6 ++++-- front/src/Phaser/Entity/RemotePlayer.ts | 5 +++-- front/src/Phaser/Game/GameScene.ts | 7 +++++-- front/src/Phaser/Player/Player.ts | 5 +++-- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/front/src/Phaser/Companion/Companion.ts b/front/src/Phaser/Companion/Companion.ts index 48a3fa4b..72491ae1 100644 --- a/front/src/Phaser/Companion/Companion.ts +++ b/front/src/Phaser/Companion/Companion.ts @@ -23,7 +23,7 @@ export class Companion extends Container { private direction: PlayerAnimationDirections; private animationType: PlayerAnimationTypes; - constructor(scene: Phaser.Scene, x: number, y: number, name: string) { + constructor(scene: Phaser.Scene, x: number, y: number, name: string, texturePromise: Promise) { super(scene, x + 14, y + 4); this.sprites = new Map(); @@ -37,12 +37,10 @@ export class Companion extends Container { this.companionName = name; - lazyLoadCompanionResource(this.scene.load, this.companionName) - .then(resource => { - this.addResource(resource); - this.invisible = false; - }) - .catch(error => console.error(error)); + texturePromise.then(resource => { + this.addResource(resource); + this.invisible = false; + }) this.scene.physics.world.enableBody(this); diff --git a/front/src/Phaser/Entity/Character.ts b/front/src/Phaser/Entity/Character.ts index eb8e45fd..5b541267 100644 --- a/front/src/Phaser/Entity/Character.ts +++ b/front/src/Phaser/Entity/Character.ts @@ -71,8 +71,10 @@ export abstract class Character extends Container { this.playAnimation(direction, moving); } - public addCompanion(name: string): void { - this.companion = new Companion(this.scene, this.x, this.y, name); + public addCompanion(name: string, texturePromise?: Promise): void { + if (typeof texturePromise !== 'undefined') { + this.companion = new Companion(this.scene, this.x, this.y, name, texturePromise); + } } public addTextures(textures: string[], frame?: string | number): void { diff --git a/front/src/Phaser/Entity/RemotePlayer.ts b/front/src/Phaser/Entity/RemotePlayer.ts index b405d8df..41e2e2df 100644 --- a/front/src/Phaser/Entity/RemotePlayer.ts +++ b/front/src/Phaser/Entity/RemotePlayer.ts @@ -18,7 +18,8 @@ export class RemotePlayer extends Character { texturesPromise: Promise, direction: PlayerAnimationDirections, moving: boolean, - companion: string|null + companion: string|null, + companionTexturePromise?: Promise ) { super(Scene, x, y, texturesPromise, name, direction, moving, 1); @@ -26,7 +27,7 @@ export class RemotePlayer extends Character { this.userId = userId; if (typeof companion === 'string') { - this.addCompanion(companion); + this.addCompanion(companion, companionTexturePromise); } } diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 222f7ed5..7cbefb39 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -69,6 +69,7 @@ import FILE_LOAD_ERROR = Phaser.Loader.Events.FILE_LOAD_ERROR; import DOMElement = Phaser.GameObjects.DOMElement; import {Subscription} from "rxjs"; import {worldFullMessageStream} from "../../Connexion/WorldFullMessageStream"; +import { lazyLoadCompanionResource } from "../Companion/CompanionTexturesLoadingManager"; export interface GameSceneInitInterface { initPosition: PointInterface|null, @@ -1024,7 +1025,8 @@ ${escapedMessage} PlayerAnimationDirections.Down, false, this.userInputManager, - this.companion + this.companion, + this.companion !== null ? lazyLoadCompanionResource(this.load, this.companion) : undefined ); }catch (err){ if(err instanceof TextureError) { @@ -1217,7 +1219,8 @@ ${escapedMessage} texturesPromise, addPlayerData.position.direction as PlayerAnimationDirections, addPlayerData.position.moving, - addPlayerData.companion + addPlayerData.companion, + addPlayerData.companion !== null ? lazyLoadCompanionResource(this.load, addPlayerData.companion) : undefined ); this.MapPlayers.add(player); this.MapPlayersByKey.set(player.userId, player); diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index d018a41f..bb961115 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -22,7 +22,8 @@ export class Player extends Character implements CurrentGamerInterface { direction: PlayerAnimationDirections, moving: boolean, private userInputManager: UserInputManager, - companion: string|null + companion: string|null, + companionTexturePromise?: Promise ) { super(Scene, x, y, texturesPromise, name, direction, moving, 1); @@ -30,7 +31,7 @@ export class Player extends Character implements CurrentGamerInterface { this.getBody().setImmovable(false); if (typeof companion === 'string') { - this.addCompanion(companion); + this.addCompanion(companion, companionTexturePromise); } }