From 9f823506b980319b9eded1607db6dc4258150680 Mon Sep 17 00:00:00 2001 From: Hanusiak Piotr Date: Thu, 10 Mar 2022 16:18:47 +0100 Subject: [PATCH] little PlayerTextures class refactor --- front/src/Connexion/ConnexionModels.ts | 2 - front/src/Phaser/Entity/PlayerTextures.ts | 71 ++++++++++++------- .../Entity/PlayerTexturesLoadingManager.ts | 16 +++-- .../Phaser/Login/AbstractCharacterScene.ts | 10 +-- front/src/Phaser/Login/CustomizeScene.ts | 2 +- .../src/Phaser/Login/SelectCharacterScene.ts | 4 +- 6 files changed, 64 insertions(+), 41 deletions(-) diff --git a/front/src/Connexion/ConnexionModels.ts b/front/src/Connexion/ConnexionModels.ts index bd12d866..c681fd37 100644 --- a/front/src/Connexion/ConnexionModels.ts +++ b/front/src/Connexion/ConnexionModels.ts @@ -1,8 +1,6 @@ import type { SignalData } from "simple-peer"; import type { RoomConnection } from "./RoomConnection"; import type { BodyResourceDescriptionInterface } from "../Phaser/Entity/PlayerTextures"; -import { PositionMessage_Direction } from "../Messages/ts-proto-generated/messages"; -import { CharacterLayer } from "../../../back/src/Model/Websocket/CharacterLayer"; export interface PointInterface { x: number; diff --git a/front/src/Phaser/Entity/PlayerTextures.ts b/front/src/Phaser/Entity/PlayerTextures.ts index 035ddf8d..02d9dd3a 100644 --- a/front/src/Phaser/Entity/PlayerTextures.ts +++ b/front/src/Phaser/Entity/PlayerTextures.ts @@ -23,7 +23,7 @@ export const mapLayerToLevel = { accessory: 5, }; -enum PlayerTexturesKey { +export enum PlayerTexturesKey { Accessory = "accessory", Body = "body", Clothes = "clothes", @@ -52,35 +52,58 @@ interface PlayerTexturesRecord { } export class PlayerTextures { - public static PLAYER_RESOURCES: BodyResourceDescriptionListInterface; - public static COLOR_RESOURCES: BodyResourceDescriptionListInterface; - public static EYES_RESOURCES: BodyResourceDescriptionListInterface; - public static HAIR_RESOURCES: BodyResourceDescriptionListInterface; - public static CLOTHES_RESOURCES: BodyResourceDescriptionListInterface; - public static HATS_RESOURCES: BodyResourceDescriptionListInterface; - public static ACCESSORIES_RESOURCES: BodyResourceDescriptionListInterface; - public static LAYERS: BodyResourceDescriptionListInterface[]; + private PLAYER_RESOURCES: BodyResourceDescriptionListInterface = {}; + private COLOR_RESOURCES: BodyResourceDescriptionListInterface = {}; + private EYES_RESOURCES: BodyResourceDescriptionListInterface = {}; + private HAIR_RESOURCES: BodyResourceDescriptionListInterface = {}; + private CLOTHES_RESOURCES: BodyResourceDescriptionListInterface = {}; + private HATS_RESOURCES: BodyResourceDescriptionListInterface = {}; + private ACCESSORIES_RESOURCES: BodyResourceDescriptionListInterface = {}; + private LAYERS: BodyResourceDescriptionListInterface[] = []; public loadPlayerTexturesMetadata(metadata: PlayerTexturesMetadata): void { this.mapTexturesMetadataIntoResources(metadata); } - private mapTexturesMetadataIntoResources(metadata: PlayerTexturesMetadata): void { - PlayerTextures.PLAYER_RESOURCES = this.getMappedResources(metadata.woka); - PlayerTextures.COLOR_RESOURCES = this.getMappedResources(metadata.body); - PlayerTextures.EYES_RESOURCES = this.getMappedResources(metadata.eyes); - PlayerTextures.HAIR_RESOURCES = this.getMappedResources(metadata.hair); - PlayerTextures.CLOTHES_RESOURCES = this.getMappedResources(metadata.clothes); - PlayerTextures.HATS_RESOURCES = this.getMappedResources(metadata.hat); - PlayerTextures.ACCESSORIES_RESOURCES = this.getMappedResources(metadata.accessory); + public getTexturesResources(key: PlayerTexturesKey): BodyResourceDescriptionListInterface { + switch (key) { + case PlayerTexturesKey.Accessory: + return this.ACCESSORIES_RESOURCES; + case PlayerTexturesKey.Body: + return this.COLOR_RESOURCES; + case PlayerTexturesKey.Clothes: + return this.CLOTHES_RESOURCES; + case PlayerTexturesKey.Eyes: + return this.EYES_RESOURCES; + case PlayerTexturesKey.Hair: + return this.HAIR_RESOURCES; + case PlayerTexturesKey.Hat: + return this.HATS_RESOURCES; + case PlayerTexturesKey.Woka: + return this.PLAYER_RESOURCES; + } + } - PlayerTextures.LAYERS = [ - PlayerTextures.COLOR_RESOURCES, - PlayerTextures.EYES_RESOURCES, - PlayerTextures.HAIR_RESOURCES, - PlayerTextures.CLOTHES_RESOURCES, - PlayerTextures.HATS_RESOURCES, - PlayerTextures.ACCESSORIES_RESOURCES, + public getLayers(): BodyResourceDescriptionListInterface[] { + return this.LAYERS; + } + + private mapTexturesMetadataIntoResources(metadata: PlayerTexturesMetadata): void { + this.PLAYER_RESOURCES = this.getMappedResources(metadata.woka); + this.COLOR_RESOURCES = this.getMappedResources(metadata.body); + this.EYES_RESOURCES = this.getMappedResources(metadata.eyes); + this.HAIR_RESOURCES = this.getMappedResources(metadata.hair); + this.CLOTHES_RESOURCES = this.getMappedResources(metadata.clothes); + this.HATS_RESOURCES = this.getMappedResources(metadata.hat); + this.ACCESSORIES_RESOURCES = this.getMappedResources(metadata.accessory); + + this.LAYERS = [ + this.COLOR_RESOURCES, + this.EYES_RESOURCES, + this.HAIR_RESOURCES, + this.CLOTHES_RESOURCES, + this.HATS_RESOURCES, + this.ACCESSORIES_RESOURCES, ]; } diff --git a/front/src/Phaser/Entity/PlayerTexturesLoadingManager.ts b/front/src/Phaser/Entity/PlayerTexturesLoadingManager.ts index 165a6063..f3c9d273 100644 --- a/front/src/Phaser/Entity/PlayerTexturesLoadingManager.ts +++ b/front/src/Phaser/Entity/PlayerTexturesLoadingManager.ts @@ -1,6 +1,6 @@ import LoaderPlugin = Phaser.Loader.LoaderPlugin; import type { CharacterTexture } from "../../Connexion/LocalUser"; -import { BodyResourceDescriptionInterface, mapLayerToLevel, PlayerTextures } from "./PlayerTextures"; +import { BodyResourceDescriptionInterface, mapLayerToLevel, PlayerTextures, PlayerTexturesKey } from "./PlayerTextures"; import CancelablePromise from "cancelable-promise"; export interface FrameConfig { @@ -8,9 +8,12 @@ export interface FrameConfig { frameHeight: number; } -export const loadAllLayers = (load: LoaderPlugin): BodyResourceDescriptionInterface[][] => { +export const loadAllLayers = ( + load: LoaderPlugin, + playerTextures: PlayerTextures +): BodyResourceDescriptionInterface[][] => { const returnArray: BodyResourceDescriptionInterface[][] = []; - PlayerTextures.LAYERS.forEach((layer) => { + playerTextures.getLayers().forEach((layer) => { const layerArray: BodyResourceDescriptionInterface[] = []; Object.values(layer).forEach((textureDescriptor) => { layerArray.push(textureDescriptor); @@ -20,8 +23,11 @@ export const loadAllLayers = (load: LoaderPlugin): BodyResourceDescriptionInterf }); return returnArray; }; -export const loadAllDefaultModels = (load: LoaderPlugin): BodyResourceDescriptionInterface[] => { - const returnArray = Object.values(PlayerTextures.PLAYER_RESOURCES); +export const loadAllDefaultModels = ( + load: LoaderPlugin, + playerTextures: PlayerTextures +): BodyResourceDescriptionInterface[] => { + const returnArray = Object.values(playerTextures.getTexturesResources(PlayerTexturesKey.Woka)); returnArray.forEach((playerResource: BodyResourceDescriptionInterface) => { load.spritesheet(playerResource.id, playerResource.img, { frameWidth: 32, frameHeight: 32 }); }); diff --git a/front/src/Phaser/Login/AbstractCharacterScene.ts b/front/src/Phaser/Login/AbstractCharacterScene.ts index bc260718..b31db769 100644 --- a/front/src/Phaser/Login/AbstractCharacterScene.ts +++ b/front/src/Phaser/Login/AbstractCharacterScene.ts @@ -1,12 +1,8 @@ import { ResizableScene } from "./ResizableScene"; -import { localUserStore } from "../../Connexion/LocalUserStore"; -import type { BodyResourceDescriptionInterface } from "../Entity/PlayerTextures"; +import { BodyResourceDescriptionInterface, PlayerTexturesKey } from "../Entity/PlayerTextures"; import { loadWokaTexture } from "../Entity/PlayerTexturesLoadingManager"; -import type { CharacterTexture } from "../../Connexion/LocalUser"; import type CancelablePromise from "cancelable-promise"; import { PlayerTextures } from "../Entity/PlayerTextures"; -import { Loader } from "../Components/Loader"; -import { CustomizeSceneName } from "./CustomizeScene"; export abstract class AbstractCharacterScene extends ResizableScene { protected playerTextures: PlayerTextures; @@ -17,7 +13,7 @@ export abstract class AbstractCharacterScene extends ResizableScene { } loadCustomSceneSelectCharacters(): Promise { - const textures = PlayerTextures.PLAYER_RESOURCES; + const textures = this.playerTextures.getTexturesResources(PlayerTexturesKey.Woka); const promises: CancelablePromise[] = []; if (textures) { for (const texture of Object.values(textures)) { @@ -32,7 +28,7 @@ export abstract class AbstractCharacterScene extends ResizableScene { loadSelectSceneCharacters(): Promise { const promises: CancelablePromise[] = []; - for (const textures of PlayerTextures.LAYERS) { + for (const textures of this.playerTextures.getLayers()) { for (const texture of Object.values(textures)) { if (texture.level !== -1) { continue; diff --git a/front/src/Phaser/Login/CustomizeScene.ts b/front/src/Phaser/Login/CustomizeScene.ts index 9227d3e3..918cf9cf 100644 --- a/front/src/Phaser/Login/CustomizeScene.ts +++ b/front/src/Phaser/Login/CustomizeScene.ts @@ -74,7 +74,7 @@ export class CustomizeScene extends AbstractCharacterScene { }) .catch((e) => console.error(e)); - this.layers = loadAllLayers(this.load); + this.layers = loadAllLayers(this.load, this.playerTextures); this.lazyloadingAttempt = false; //this function must stay at the end of preload function diff --git a/front/src/Phaser/Login/SelectCharacterScene.ts b/front/src/Phaser/Login/SelectCharacterScene.ts index 5fe0ecc9..f7fd3c8a 100644 --- a/front/src/Phaser/Login/SelectCharacterScene.ts +++ b/front/src/Phaser/Login/SelectCharacterScene.ts @@ -70,7 +70,7 @@ export class SelectCharacterScene extends AbstractCharacterScene { this.lazyloadingAttempt = true; }) .catch((e) => console.error(e)); - this.playerModels = loadAllDefaultModels(this.load); + this.playerModels = loadAllDefaultModels(this.load, this.playerTextures); this.lazyloadingAttempt = false; //this function must stay at the end of preload function @@ -299,7 +299,7 @@ export class SelectCharacterScene extends AbstractCharacterScene { } private isCustomizationAvailable(): boolean { - for (const layer of PlayerTextures.LAYERS) { + for (const layer of this.playerTextures.getLayers()) { if (Object.keys(layer).length > 0) { return true; }