little PlayerTextures class refactor
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
import type { SignalData } from "simple-peer";
|
import type { SignalData } from "simple-peer";
|
||||||
import type { RoomConnection } from "./RoomConnection";
|
import type { RoomConnection } from "./RoomConnection";
|
||||||
import type { BodyResourceDescriptionInterface } from "../Phaser/Entity/PlayerTextures";
|
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 {
|
export interface PointInterface {
|
||||||
x: number;
|
x: number;
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ export const mapLayerToLevel = {
|
|||||||
accessory: 5,
|
accessory: 5,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum PlayerTexturesKey {
|
export enum PlayerTexturesKey {
|
||||||
Accessory = "accessory",
|
Accessory = "accessory",
|
||||||
Body = "body",
|
Body = "body",
|
||||||
Clothes = "clothes",
|
Clothes = "clothes",
|
||||||
@@ -52,35 +52,58 @@ interface PlayerTexturesRecord {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class PlayerTextures {
|
export class PlayerTextures {
|
||||||
public static PLAYER_RESOURCES: BodyResourceDescriptionListInterface;
|
private PLAYER_RESOURCES: BodyResourceDescriptionListInterface = {};
|
||||||
public static COLOR_RESOURCES: BodyResourceDescriptionListInterface;
|
private COLOR_RESOURCES: BodyResourceDescriptionListInterface = {};
|
||||||
public static EYES_RESOURCES: BodyResourceDescriptionListInterface;
|
private EYES_RESOURCES: BodyResourceDescriptionListInterface = {};
|
||||||
public static HAIR_RESOURCES: BodyResourceDescriptionListInterface;
|
private HAIR_RESOURCES: BodyResourceDescriptionListInterface = {};
|
||||||
public static CLOTHES_RESOURCES: BodyResourceDescriptionListInterface;
|
private CLOTHES_RESOURCES: BodyResourceDescriptionListInterface = {};
|
||||||
public static HATS_RESOURCES: BodyResourceDescriptionListInterface;
|
private HATS_RESOURCES: BodyResourceDescriptionListInterface = {};
|
||||||
public static ACCESSORIES_RESOURCES: BodyResourceDescriptionListInterface;
|
private ACCESSORIES_RESOURCES: BodyResourceDescriptionListInterface = {};
|
||||||
public static LAYERS: BodyResourceDescriptionListInterface[];
|
private LAYERS: BodyResourceDescriptionListInterface[] = [];
|
||||||
|
|
||||||
public loadPlayerTexturesMetadata(metadata: PlayerTexturesMetadata): void {
|
public loadPlayerTexturesMetadata(metadata: PlayerTexturesMetadata): void {
|
||||||
this.mapTexturesMetadataIntoResources(metadata);
|
this.mapTexturesMetadataIntoResources(metadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
private mapTexturesMetadataIntoResources(metadata: PlayerTexturesMetadata): void {
|
public getTexturesResources(key: PlayerTexturesKey): BodyResourceDescriptionListInterface {
|
||||||
PlayerTextures.PLAYER_RESOURCES = this.getMappedResources(metadata.woka);
|
switch (key) {
|
||||||
PlayerTextures.COLOR_RESOURCES = this.getMappedResources(metadata.body);
|
case PlayerTexturesKey.Accessory:
|
||||||
PlayerTextures.EYES_RESOURCES = this.getMappedResources(metadata.eyes);
|
return this.ACCESSORIES_RESOURCES;
|
||||||
PlayerTextures.HAIR_RESOURCES = this.getMappedResources(metadata.hair);
|
case PlayerTexturesKey.Body:
|
||||||
PlayerTextures.CLOTHES_RESOURCES = this.getMappedResources(metadata.clothes);
|
return this.COLOR_RESOURCES;
|
||||||
PlayerTextures.HATS_RESOURCES = this.getMappedResources(metadata.hat);
|
case PlayerTexturesKey.Clothes:
|
||||||
PlayerTextures.ACCESSORIES_RESOURCES = this.getMappedResources(metadata.accessory);
|
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 = [
|
public getLayers(): BodyResourceDescriptionListInterface[] {
|
||||||
PlayerTextures.COLOR_RESOURCES,
|
return this.LAYERS;
|
||||||
PlayerTextures.EYES_RESOURCES,
|
}
|
||||||
PlayerTextures.HAIR_RESOURCES,
|
|
||||||
PlayerTextures.CLOTHES_RESOURCES,
|
private mapTexturesMetadataIntoResources(metadata: PlayerTexturesMetadata): void {
|
||||||
PlayerTextures.HATS_RESOURCES,
|
this.PLAYER_RESOURCES = this.getMappedResources(metadata.woka);
|
||||||
PlayerTextures.ACCESSORIES_RESOURCES,
|
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 LoaderPlugin = Phaser.Loader.LoaderPlugin;
|
||||||
import type { CharacterTexture } from "../../Connexion/LocalUser";
|
import type { CharacterTexture } from "../../Connexion/LocalUser";
|
||||||
import { BodyResourceDescriptionInterface, mapLayerToLevel, PlayerTextures } from "./PlayerTextures";
|
import { BodyResourceDescriptionInterface, mapLayerToLevel, PlayerTextures, PlayerTexturesKey } from "./PlayerTextures";
|
||||||
import CancelablePromise from "cancelable-promise";
|
import CancelablePromise from "cancelable-promise";
|
||||||
|
|
||||||
export interface FrameConfig {
|
export interface FrameConfig {
|
||||||
@@ -8,9 +8,12 @@ export interface FrameConfig {
|
|||||||
frameHeight: number;
|
frameHeight: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const loadAllLayers = (load: LoaderPlugin): BodyResourceDescriptionInterface[][] => {
|
export const loadAllLayers = (
|
||||||
|
load: LoaderPlugin,
|
||||||
|
playerTextures: PlayerTextures
|
||||||
|
): BodyResourceDescriptionInterface[][] => {
|
||||||
const returnArray: BodyResourceDescriptionInterface[][] = [];
|
const returnArray: BodyResourceDescriptionInterface[][] = [];
|
||||||
PlayerTextures.LAYERS.forEach((layer) => {
|
playerTextures.getLayers().forEach((layer) => {
|
||||||
const layerArray: BodyResourceDescriptionInterface[] = [];
|
const layerArray: BodyResourceDescriptionInterface[] = [];
|
||||||
Object.values(layer).forEach((textureDescriptor) => {
|
Object.values(layer).forEach((textureDescriptor) => {
|
||||||
layerArray.push(textureDescriptor);
|
layerArray.push(textureDescriptor);
|
||||||
@@ -20,8 +23,11 @@ export const loadAllLayers = (load: LoaderPlugin): BodyResourceDescriptionInterf
|
|||||||
});
|
});
|
||||||
return returnArray;
|
return returnArray;
|
||||||
};
|
};
|
||||||
export const loadAllDefaultModels = (load: LoaderPlugin): BodyResourceDescriptionInterface[] => {
|
export const loadAllDefaultModels = (
|
||||||
const returnArray = Object.values(PlayerTextures.PLAYER_RESOURCES);
|
load: LoaderPlugin,
|
||||||
|
playerTextures: PlayerTextures
|
||||||
|
): BodyResourceDescriptionInterface[] => {
|
||||||
|
const returnArray = Object.values(playerTextures.getTexturesResources(PlayerTexturesKey.Woka));
|
||||||
returnArray.forEach((playerResource: BodyResourceDescriptionInterface) => {
|
returnArray.forEach((playerResource: BodyResourceDescriptionInterface) => {
|
||||||
load.spritesheet(playerResource.id, playerResource.img, { frameWidth: 32, frameHeight: 32 });
|
load.spritesheet(playerResource.id, playerResource.img, { frameWidth: 32, frameHeight: 32 });
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
import { ResizableScene } from "./ResizableScene";
|
import { ResizableScene } from "./ResizableScene";
|
||||||
import { localUserStore } from "../../Connexion/LocalUserStore";
|
import { BodyResourceDescriptionInterface, PlayerTexturesKey } from "../Entity/PlayerTextures";
|
||||||
import type { BodyResourceDescriptionInterface } from "../Entity/PlayerTextures";
|
|
||||||
import { loadWokaTexture } from "../Entity/PlayerTexturesLoadingManager";
|
import { loadWokaTexture } from "../Entity/PlayerTexturesLoadingManager";
|
||||||
import type { CharacterTexture } from "../../Connexion/LocalUser";
|
|
||||||
import type CancelablePromise from "cancelable-promise";
|
import type CancelablePromise from "cancelable-promise";
|
||||||
import { PlayerTextures } from "../Entity/PlayerTextures";
|
import { PlayerTextures } from "../Entity/PlayerTextures";
|
||||||
import { Loader } from "../Components/Loader";
|
|
||||||
import { CustomizeSceneName } from "./CustomizeScene";
|
|
||||||
|
|
||||||
export abstract class AbstractCharacterScene extends ResizableScene {
|
export abstract class AbstractCharacterScene extends ResizableScene {
|
||||||
protected playerTextures: PlayerTextures;
|
protected playerTextures: PlayerTextures;
|
||||||
@@ -17,7 +13,7 @@ export abstract class AbstractCharacterScene extends ResizableScene {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadCustomSceneSelectCharacters(): Promise<BodyResourceDescriptionInterface[]> {
|
loadCustomSceneSelectCharacters(): Promise<BodyResourceDescriptionInterface[]> {
|
||||||
const textures = PlayerTextures.PLAYER_RESOURCES;
|
const textures = this.playerTextures.getTexturesResources(PlayerTexturesKey.Woka);
|
||||||
const promises: CancelablePromise<BodyResourceDescriptionInterface>[] = [];
|
const promises: CancelablePromise<BodyResourceDescriptionInterface>[] = [];
|
||||||
if (textures) {
|
if (textures) {
|
||||||
for (const texture of Object.values(textures)) {
|
for (const texture of Object.values(textures)) {
|
||||||
@@ -32,7 +28,7 @@ export abstract class AbstractCharacterScene extends ResizableScene {
|
|||||||
|
|
||||||
loadSelectSceneCharacters(): Promise<BodyResourceDescriptionInterface[]> {
|
loadSelectSceneCharacters(): Promise<BodyResourceDescriptionInterface[]> {
|
||||||
const promises: CancelablePromise<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)) {
|
for (const texture of Object.values(textures)) {
|
||||||
if (texture.level !== -1) {
|
if (texture.level !== -1) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ export class CustomizeScene extends AbstractCharacterScene {
|
|||||||
})
|
})
|
||||||
.catch((e) => console.error(e));
|
.catch((e) => console.error(e));
|
||||||
|
|
||||||
this.layers = loadAllLayers(this.load);
|
this.layers = loadAllLayers(this.load, this.playerTextures);
|
||||||
this.lazyloadingAttempt = false;
|
this.lazyloadingAttempt = false;
|
||||||
|
|
||||||
//this function must stay at the end of preload function
|
//this function must stay at the end of preload function
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ export class SelectCharacterScene extends AbstractCharacterScene {
|
|||||||
this.lazyloadingAttempt = true;
|
this.lazyloadingAttempt = true;
|
||||||
})
|
})
|
||||||
.catch((e) => console.error(e));
|
.catch((e) => console.error(e));
|
||||||
this.playerModels = loadAllDefaultModels(this.load);
|
this.playerModels = loadAllDefaultModels(this.load, this.playerTextures);
|
||||||
this.lazyloadingAttempt = false;
|
this.lazyloadingAttempt = false;
|
||||||
|
|
||||||
//this function must stay at the end of preload function
|
//this function must stay at the end of preload function
|
||||||
@@ -299,7 +299,7 @@ export class SelectCharacterScene extends AbstractCharacterScene {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private isCustomizationAvailable(): boolean {
|
private isCustomizationAvailable(): boolean {
|
||||||
for (const layer of PlayerTextures.LAYERS) {
|
for (const layer of this.playerTextures.getLayers()) {
|
||||||
if (Object.keys(layer).length > 0) {
|
if (Object.keys(layer).length > 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user