diff --git a/scene/package.json b/scene/package.json index c874bb8..8552560 100644 --- a/scene/package.json +++ b/scene/package.json @@ -37,4 +37,4 @@ "fp-future": "^1.0.1", "typescript": "5.5.4" } -} +} \ No newline at end of file diff --git a/scene/src/controllers/ui.controller.tsx b/scene/src/controllers/ui.controller.tsx index 75465e0..1e55e7e 100644 --- a/scene/src/controllers/ui.controller.tsx +++ b/scene/src/controllers/ui.controller.tsx @@ -18,10 +18,12 @@ import { CreationPlayerController } from './creation-player.controller' import { type GameController } from './game.controller' import { MainHudController } from './main-hud' import { type RealmType } from '../realms/types' +import { ConfirmLoot } from '../ui/confirmloot/confirmloot' export class UIController { loadingUI: LoadingUI playDungeonUI: PlayDungeonUI + confirmLoot: ConfirmLoot // Banner gameController: GameController @@ -46,6 +48,7 @@ export class UIController { constructor(gameController: GameController) { this.gameController = gameController this.loadingUI = new LoadingUI(this) + this.confirmLoot = new ConfirmLoot(this) this.playDungeonUI = new PlayDungeonUI(this) ReactEcsRenderer.setUiRenderer(this.ui.bind(this)) } @@ -152,6 +155,9 @@ export class UIController { {/* Loadin screen */} {this.loadingUI.visible() && this.loadingUI.mainUi()} + + {/* Confirm Loot */} + {this.confirmLoot.isVisible && this.confirmLoot.mainUi()} ) } diff --git a/scene/src/ui/confirmloot/confirmloot.tsx b/scene/src/ui/confirmloot/confirmloot.tsx new file mode 100644 index 0000000..fb3fb4d --- /dev/null +++ b/scene/src/ui/confirmloot/confirmloot.tsx @@ -0,0 +1,225 @@ +import ReactEcs, { Label, UiEntity } from '@dcl/react-ecs' +import { type UIController } from '../../controllers/ui.controller' +import Canvas from '../canvas/Canvas' +import { UiCanvasInformation, engine } from '@dcl/sdk/ecs' +import { Player } from '../../player/player' +import { ITEM_TYPES } from '../../inventory/playerInventoryMap' + +type ConfirmLootType = { + item: string + amount: number + currency: 'dungeonToken' | 'mana' +} +export class ConfirmLoot { + uiController: UIController + public isVisible: boolean = false + public payClaim_visible: boolean = true + public payClaimUnavail_visible: boolean = false + public declineLoot_visible: boolean = true + public declineLootUnavail_visible: boolean = false + private lootItem = '' + private payAmount = 0 + private currencyType: 'dungeonToken' | 'mana' = 'dungeonToken' + public confirmLootText_value: string = `Are you sure you want to claim the\n${this.lootItem} for ${this.payAmount} ${this.currencyType}?` + public confirmLootText_visible = true + constructor(uiController: UIController) { + this.uiController = uiController + } + + mainUi(): ReactEcs.JSX.Element { + const canvasInfo = UiCanvasInformation.get(engine.RootEntity) + return ( + + + + + ) + } + + visible(): boolean { + return this.isVisible + } + + openConfirmLoot(confirmLoot: ConfirmLootType): void { + this.lootItem = confirmLoot.item + this.payAmount = confirmLoot.amount + this.currencyType = confirmLoot.currency + + this.isVisible = true + this.confirmLootText_value = `Are you sure you want to claim the\n${this.lootItem} for ${this.payAmount} ${this.currencyType}?` + this.confirmLootText_visible = true + + this.payClaim_visible = true + this.declineLoot_visible = true + + // Save the callback for later use + // confirmCallback = onConfirmCallback + } + + confirmLoot(confirmLoot: ConfirmLootType): void { + const player = Player.getInstance() + // Hide UI elements + this.hideConfirmLootUI() + + if (confirmLoot.currency === 'dungeonToken') { + this.useItemsForTokens(player, confirmLoot.amount) + } + + if (confirmLoot.currency === 'mana') { + // Take MANA + } + + // Call the callback function if it exists + // if (confirmCallback) { + // confirmCallback({ item, amount, currency }) + // } + } + + hideConfirmLootUI(): void { + this.isVisible = false + this.payClaim_visible = false + this.payClaimUnavail_visible = false + this.declineLoot_visible = false + this.declineLootUnavail_visible = false + this.confirmLootText_visible = false + } + + useItemsForTokens(player: Player, totalTokensNeeded: number): void { + const iceShardCount = player.inventory.getItemCount(ITEM_TYPES.ICESHARD) + const iceHeartCount = player.inventory.getItemCount(ITEM_TYPES.ICEHEART) + + let tokensFormed = 0 + let shardsUsed = 0 + let heartsUsed = 0 + + while ( + tokensFormed < totalTokensNeeded && + (shardsUsed < iceShardCount || heartsUsed < iceHeartCount) + ) { + if (shardsUsed < iceShardCount) { + shardsUsed++ + tokensFormed++ + } else if (heartsUsed < iceHeartCount) { + heartsUsed++ + tokensFormed++ + } + } + + if (tokensFormed === totalTokensNeeded) { + if (shardsUsed > 0) + player.inventory.reduceItem(ITEM_TYPES.ICESHARD, shardsUsed) + if (heartsUsed > 0) + player.inventory.reduceItem(ITEM_TYPES.ICEHEART, heartsUsed) + console.log( + `Used ${shardsUsed} Ice Shards and ${heartsUsed} Ice Hearts to form ${tokensFormed} tokens.` + ) + } else { + console.log('Not enough items to form the required number of tokens.') + } + } +}