little PlayerTextures class refactor
This commit is contained in:
parent
060cdf3310
commit
9f823506b9
@ -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;
|
||||
|
@ -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,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -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 });
|
||||
});
|
||||
|
@ -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<BodyResourceDescriptionInterface[]> {
|
||||
const textures = PlayerTextures.PLAYER_RESOURCES;
|
||||
const textures = this.playerTextures.getTexturesResources(PlayerTexturesKey.Woka);
|
||||
const promises: CancelablePromise<BodyResourceDescriptionInterface>[] = [];
|
||||
if (textures) {
|
||||
for (const texture of Object.values(textures)) {
|
||||
@ -32,7 +28,7 @@ export abstract class AbstractCharacterScene extends ResizableScene {
|
||||
|
||||
loadSelectSceneCharacters(): Promise<BodyResourceDescriptionInterface[]> {
|
||||
const promises: CancelablePromise<BodyResourceDescriptionInterface>[] = [];
|
||||
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;
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user