diff --git a/front/src/Phaser/Components/CustomizeWoka/CustomWokaPreviewer.ts b/front/src/Phaser/Components/CustomizeWoka/CustomWokaPreviewer.ts index a015082c..570e07eb 100644 --- a/front/src/Phaser/Components/CustomizeWoka/CustomWokaPreviewer.ts +++ b/front/src/Phaser/Components/CustomizeWoka/CustomWokaPreviewer.ts @@ -1,3 +1,4 @@ +import { MathUtils } from "../../../Utils/MathUtils"; import { getPlayerAnimations, PlayerAnimationDirections, PlayerAnimationTypes } from "../../Player/Animation"; export enum CustomWokaBodyPart { @@ -73,6 +74,11 @@ export class CustomWokaPreviewer extends Phaser.GameObjects.Container { this.animate(); } + public setDisplaySize(width: number, height: number): this { + const [newWidth, newHeight] = MathUtils.getWholePixelsNewSize(this.SIZE, this.SIZE, width, height); + return super.setDisplaySize(newWidth, newHeight); + } + public changeAnimation(direction: PlayerAnimationDirections, moving: boolean): void { this.animationDirection = direction; this.moving = moving; diff --git a/front/src/Phaser/Components/CustomizeWoka/WokaBodyPartSlot.ts b/front/src/Phaser/Components/CustomizeWoka/WokaBodyPartSlot.ts index e2bad69c..5aceb34d 100644 --- a/front/src/Phaser/Components/CustomizeWoka/WokaBodyPartSlot.ts +++ b/front/src/Phaser/Components/CustomizeWoka/WokaBodyPartSlot.ts @@ -1,5 +1,6 @@ import { GridItem } from "@home-based-studio/phaser3-utils"; import { GridItemEvent } from "@home-based-studio/phaser3-utils/lib/utils/gui/containers/grids/GridItem"; +import { MathUtils } from "../../../Utils/MathUtils"; export interface WokaBodyPartSlotConfig { color: number; @@ -61,6 +62,11 @@ export class WokaBodyPartSlot extends GridItem { this.scene.add.existing(this); } + public setDisplaySize(width: number, height: number): this { + const [newWidth, newHeight] = MathUtils.getWholePixelsNewSize(this.SIZE, this.SIZE, width, height, 32, 32); + return super.setDisplaySize(newWidth, newHeight); + } + public setTextures(bodyTextureKey?: string, imageTextureKey?: string): void { this.setBodyTexture(bodyTextureKey); this.setImageTexture(imageTextureKey); diff --git a/front/src/Phaser/Login/CustomizeScene.ts b/front/src/Phaser/Login/CustomizeScene.ts index 30f4e86f..ab65a619 100644 --- a/front/src/Phaser/Login/CustomizeScene.ts +++ b/front/src/Phaser/Login/CustomizeScene.ts @@ -190,11 +190,11 @@ export class CustomizeScene extends AbstractCharacterScene { private handleCustomWokaPreviewerOnResize(): void { const slotDimension = - Math.min(innerWidth * (this.isVertical ? 0.2 : 0.15), innerHeight * (this.isVertical ? 0.2 : 0.15)) / + Math.min(innerWidth * (this.isVertical ? 0.2 : 0.2), innerHeight * (this.isVertical ? 0.2 : 0.2)) / waScaleManager.getActualZoom(); const boxDimension = - Math.min(innerWidth * (this.isVertical ? 0.4 : 0.3), innerHeight * (this.isVertical ? 0.4 : 0.3)) / + Math.min(innerWidth * (this.isVertical ? 0.4 : 0.5), innerHeight * (this.isVertical ? 0.4 : 0.5)) / waScaleManager.getActualZoom(); this.customWokaPreviewer.setDisplaySize(boxDimension, boxDimension); @@ -206,12 +206,16 @@ export class CustomizeScene extends AbstractCharacterScene { private handleBodyPartSlotsOnResize(): void { const slotDimension = - Math.min(innerWidth * (this.isVertical ? 0.2 : 0.15), innerHeight * (this.isVertical ? 0.2 : 0.15)) / + Math.min(innerWidth * (this.isVertical ? 0.2 : 0.25), innerHeight * (this.isVertical ? 0.2 : 0.25)) / waScaleManager.getActualZoom(); + // 1; + console.log("zoom: ", waScaleManager.getActualZoom()); + console.log("slotDimension: ", slotDimension); for (const part in this.bodyPartsSlots) { this.bodyPartsSlots[part as CustomWokaBodyPart].setDisplaySize(slotDimension, slotDimension); } + console.log(this.bodyPartsSlots.Body.displayWidth); const slotSize = this.bodyPartsSlots.Accessory.displayHeight; diff --git a/front/src/Utils/MathUtils.ts b/front/src/Utils/MathUtils.ts index fc055d11..ebe8fb34 100644 --- a/front/src/Utils/MathUtils.ts +++ b/front/src/Utils/MathUtils.ts @@ -35,4 +35,43 @@ export class MathUtils { public static randomFromArray(array: T[]): T { return array[Math.floor(Math.random() * array.length)]; } + + /** + * + * @param baseWidth Object's default width not affected by any scaling + * @param baseHeight Object's default height not affected by any scaling + * @param requestedWidth Width we would like to achieve + * @param requestedHeight Height we would like to achieve + * @param unitSizeWidth Smallest possible unit of our 'scale step' for width + * @param unitSizeHeight Smallest possible unit of our 'scale step' for height + * @returns [ newWidth, newHeight ] + */ + public static getWholePixelsNewSize( + baseWidth: number, + baseHeight: number, + requestedWidth: number, + requestedHeight: number, + unitSizeWidth: number = 32, + unitSizeHeight: number = 32 + ): [number, number] { + // Demanded scale to be applied + const newScaleW = requestedWidth / baseWidth; + const newScaleH = requestedHeight / baseHeight; + + // How would it affect our sprites + const spriteWidth = Math.floor(unitSizeWidth * newScaleW); + const spriteHeight = Math.floor(unitSizeHeight * newScaleH); + + // Expected nearest sprite size to maintain crisp pixels + const expectedSpriteWidth = spriteWidth - (spriteWidth % unitSizeWidth); + const expectedSpriteHeight = spriteHeight - (spriteHeight % unitSizeHeight); + + // Expected nearest scale + const neededScaleWidth = expectedSpriteWidth / unitSizeWidth; + const neededScaleHeight = expectedSpriteHeight / unitSizeHeight; + console.log(neededScaleWidth, neededScaleHeight); + + // Calculate new width and height and apply it to the whole container + return [baseWidth * neededScaleWidth, baseHeight * neededScaleHeight]; + } }