load texture inside game scene instead inside of inside companion class

This commit is contained in:
Johannes Berthel 2021-04-06 19:10:18 +02:00
parent e5a196a42b
commit 187e21eed9
5 changed files with 20 additions and 15 deletions

View File

@ -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<string>) {
super(scene, x + 14, y + 4);
this.sprites = new Map<string, Sprite>();
@ -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);

View File

@ -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<string>): 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 {

View File

@ -18,7 +18,8 @@ export class RemotePlayer extends Character {
texturesPromise: Promise<string[]>,
direction: PlayerAnimationDirections,
moving: boolean,
companion: string|null
companion: string|null,
companionTexturePromise?: Promise<string>
) {
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);
}
}

View File

@ -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);

View File

@ -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<string>
) {
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);
}
}