|
| 1 | +import { getGameState, setGameState } from './utils/gameState'; |
| 2 | +import { k } from './kplayCtx'; |
| 3 | + |
| 4 | +const backpackBtn = document.getElementById('backpack-btn'); |
| 5 | + |
| 6 | +let isOpened = false; |
| 7 | + |
| 8 | +export class Backpack { |
| 9 | + constructor(state, maxSlots = 20) { |
| 10 | + this.maxSlots = maxSlots; |
| 11 | + this.inventory = state; |
| 12 | + } |
| 13 | + |
| 14 | + getInventorySlotHtml(item) { |
| 15 | + return `<div class="inventory-slot"> |
| 16 | + <img src="${item.assetUrl}" with="100%" alt="" /> |
| 17 | + <span class="quantity-badge">${item.qty || 1}</span> |
| 18 | + <div class="tooltip">${item.itemName} ×${item.qty || 1}</div> |
| 19 | + </div>`; |
| 20 | + } |
| 21 | + |
| 22 | + getInventorySlotsHtml() { |
| 23 | + const slots = this.inventory.map((item) => { |
| 24 | + return this.getInventorySlotHtml(item); |
| 25 | + }); |
| 26 | + const emptySlotsCount = this.maxSlots - this.inventory.length; |
| 27 | + for (let i = 0; i < emptySlotsCount; i++) { |
| 28 | + slots.push('<div class="inventory-slot empty"></div>'); |
| 29 | + } |
| 30 | + return slots; |
| 31 | + } |
| 32 | + |
| 33 | + addItemToBackpack({ title, assetUrl }) { |
| 34 | + const itemInd = this.inventory.findIndex( |
| 35 | + (item) => item.itemName === title |
| 36 | + ); |
| 37 | + if (itemInd !== -1) { |
| 38 | + const existedItem = this.inventory[itemInd]; |
| 39 | + this.inventory[itemInd] = { |
| 40 | + ...existedItem, |
| 41 | + qty: (existedItem.qty || 1) + 1, |
| 42 | + }; |
| 43 | + } else { |
| 44 | + // Check if we have empty slots |
| 45 | + if (this.inventory.length < this.maxSlots - 1) { |
| 46 | + this.inventory.push({ |
| 47 | + itemName: title, |
| 48 | + assetUrl: assetUrl, |
| 49 | + qty: 1, |
| 50 | + }); |
| 51 | + } else { |
| 52 | + k.debug.log( |
| 53 | + 'No empty slots left. Please remove items from backpack.' |
| 54 | + ); |
| 55 | + } |
| 56 | + } |
| 57 | + const newState = getGameState(); |
| 58 | + newState.player.backpack.items = this.inventory; |
| 59 | + setGameState(newState); |
| 60 | + } |
| 61 | + |
| 62 | + render() { |
| 63 | + const backpack = document.getElementById('backpack-content'); |
| 64 | + backpack.style.display = 'flex'; |
| 65 | + |
| 66 | + const slots = this.getInventorySlotsHtml(); |
| 67 | + const inventory = document.getElementById('inventory-grid'); |
| 68 | + inventory.innerHTML = slots.join(' '); |
| 69 | + |
| 70 | + const statSlots = document.getElementById('stat-slots'); |
| 71 | + statSlots.innerHTML = `${this.inventory.length} / ${this.maxSlots}`; |
| 72 | + } |
| 73 | + |
| 74 | + hide() { |
| 75 | + const backpackElement = document.getElementById('backpack-content'); |
| 76 | + backpackElement.style.display = 'none'; |
| 77 | + } |
| 78 | + |
| 79 | + static getInstance() { |
| 80 | + const gameState = getGameState(); |
| 81 | + if (gameState.player.backpack) { |
| 82 | + return new Backpack( |
| 83 | + gameState.player.backpack.items, |
| 84 | + gameState.player.backpack.maxSlots |
| 85 | + ); |
| 86 | + } |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + static addItem(params) { |
| 91 | + const instance = Backpack.getInstance(); |
| 92 | + if (!instance) return; |
| 93 | + instance.addItemToBackpack(params); |
| 94 | + } |
| 95 | + |
| 96 | + static initState() { |
| 97 | + const gameState = getGameState(); |
| 98 | + gameState.player.backpack = { |
| 99 | + items: [], |
| 100 | + maxSlots: 20, |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + handleCloseBackpack() { |
| 105 | + if (!k.isFocused()) { |
| 106 | + k.pressButton('backpack'); |
| 107 | + k.canvas.focus(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + openBackpack() { |
| 112 | + this.render(); |
| 113 | + document.addEventListener('keypress', this.handleCloseBackpack); |
| 114 | + isOpened = true; |
| 115 | + } |
| 116 | + |
| 117 | + hideBackpack() { |
| 118 | + this.hide(); |
| 119 | + document.removeEventListener('keypress', this.handleCloseBackpack); |
| 120 | + isOpened = false; |
| 121 | + } |
| 122 | + |
| 123 | + static init() { |
| 124 | + const instance = Backpack.getInstance(); |
| 125 | + if (instance) { |
| 126 | + backpackBtn.style.display = 'block'; |
| 127 | + |
| 128 | + backpackBtn.addEventListener('click', () => { |
| 129 | + instance.openBackpack(); |
| 130 | + }); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + static backpackInteractions() { |
| 135 | + k.onButtonPress('backpack', () => { |
| 136 | + const instance = Backpack.getInstance(); |
| 137 | + if (!instance) return; |
| 138 | + |
| 139 | + if (!isOpened) { |
| 140 | + instance.openBackpack(); |
| 141 | + } else { |
| 142 | + instance.hideBackpack(); |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + static showButton() { |
| 148 | + backpackBtn.style.display = 'block'; |
| 149 | + } |
| 150 | + |
| 151 | + static removeButton() { |
| 152 | + backpackBtn.style.display = 'none'; |
| 153 | + } |
| 154 | +} |
0 commit comments