From 835c81a4b642eef74d3d682c73c866d488ad1b8b Mon Sep 17 00:00:00 2001 From: Mees Date: Sat, 19 Oct 2024 11:17:50 +0200 Subject: [PATCH] fix: few bugs (equipment and mining bugs) --- src/stores/adventureStore.ts | 3 +- src/stores/equipmentStore.ts | 83 +++++++++++--------- src/stores/inventoryStore.ts | 106 ++++++++++++++++---------- src/views/Training/TrainingMining.vue | 6 +- 4 files changed, 118 insertions(+), 80 deletions(-) diff --git a/src/stores/adventureStore.ts b/src/stores/adventureStore.ts index 5a82618..6b6f0db 100644 --- a/src/stores/adventureStore.ts +++ b/src/stores/adventureStore.ts @@ -151,8 +151,6 @@ export const useAdventureStore = defineStore('adventureStore', { this.spawnRandomEnemy() } }, - - // Stop all active battles (useful when managing multiple loops) stopAllBattles() { this.battleStatus = 'idle' }, @@ -574,6 +572,7 @@ export const useAdventureStore = defineStore('adventureStore', { this.enemySpawned = true }, + getAdventureState() { return { armyHealth: this.armyHealth, diff --git a/src/stores/equipmentStore.ts b/src/stores/equipmentStore.ts index e177c2b..f8a4be8 100644 --- a/src/stores/equipmentStore.ts +++ b/src/stores/equipmentStore.ts @@ -165,6 +165,7 @@ export const useEquipmentStore = defineStore('equipmentStore', { const gameStore = useResourcesStore() const adventureStore = useAdventureStore() const context = {gameStore, adventureStore} + item = inventoryStore.getItemFromInventory(item.id) // Remove the item from the inventory if (slotType === 'accessory') { @@ -397,63 +398,77 @@ export const useEquipmentStore = defineStore('equipmentStore', { }, loadEquipmentState(state) { - const inventoryStore = useInventoryStore() const gameStore = useResourcesStore() const adventureStore = useAdventureStore() - const context = {gameStore, adventureStore} + const context = { gameStore, adventureStore } + + this.resetState(state) + if (!state.equippedItems) return + + // Load equipped items for main slots (head, body, legs, weapon) + this.loadEquippedItems(state.equippedItems, context) + + // Load accessory slots + this.loadAccessories(state.equippedItems.accessories, context) + // Recalculate bonuses and stats + this.finalizeLoading() + }, + + resetState(state) { this.activeSetBonus = null this.loadOuts = state.loadOuts ?? [] this.maxLoadOuts = state.maxLoadOuts ?? this.maxLoadOuts - this.maxAccessories = 2 - if (!state.equippedItems) { - return - } + }, - // Load equipped items and apply their effects - ['head', 'body', 'legs', 'weapon'].forEach((slotType) => { - const itemId = state.equippedItems[slotType]?.id || null + loadEquippedItems(equippedItems, context) { + const inventoryStore = useInventoryStore() + // Iterate over the main slots and load items + const slots = ['head', 'body', 'legs', 'weapon'] + slots.forEach((slotType) => { + const itemId = equippedItems[slotType]?.id || null if (itemId) { const item = inventoryStore.getItemById(itemId) if (item) { this.equippedItems[slotType] = item + item.level = equippedItems[slotType]?.level || 1 + this.applyItemEffect(item, context) + } + return + } - item.level = state.equippedItems[slotType]?.level || 1 // Set level from state + this.equippedItems[slotType] = null + }) + }, - if (item.effect) { - item.effect(context, item) - } + loadAccessories(accessories = [], context) { + const inventoryStore = useInventoryStore() + + accessories.forEach((accessoryState, index) => { + const itemId = accessoryState?.id || null + if (itemId) { + const item = inventoryStore.getItemById(itemId) + if (item) { + this.equippedItems.accessories[index] = item + item.level = accessoryState?.level || 1 + this.applyItemEffect(item, context) } } else { - this.equippedItems[slotType] = null + this.equippedItems.accessories[index] = null } }) + }, - // Load accessories - if (Array.isArray(state.equippedItems.accessories)) { - state.equippedItems.accessories.forEach((accessoryState, index) => { - const itemId = accessoryState?.id || null - if (itemId) { - const item = inventoryStore.getItemById(itemId) - if (item) { - this.equippedItems.accessories[index] = item - item.level = accessoryState?.level || 1 // Set level from state - if (item.effect) { - item.effect(context, item) - } - } - } else { - this.equippedItems.accessories[index] = null - } - }) + applyItemEffect(item, context) { + if (item.effect) { + item.effect(context, item) } + }, - // Recalculate set bonuses + finalizeLoading() { this.checkForSetBonus() - - // Add this line to recalculate adventure stats after loading equipment useGameStore().setupAdventureStats() }, diff --git a/src/stores/inventoryStore.ts b/src/stores/inventoryStore.ts index 9450fe7..652d64b 100644 --- a/src/stores/inventoryStore.ts +++ b/src/stores/inventoryStore.ts @@ -32,62 +32,82 @@ export const useInventoryStore = defineStore('inventoryStore', { return false } - // Handle equipment items - if (registryItem.type === 'equipment') { - const equippedItem = equipmentStore.findEquippedItemById(registryItem.id) - if (equippedItem) { - // Level up equipped item - if (equippedItem.level < equippedItem.maxLevel) { - equipmentStore.levelUpEquippedItem(equippedItem) - return true - } - return false // Early return - } + // Handle equipment items separately + if (this.isEquipmentItem(registryItem)) { + return this.handleEquipmentItem(registryItem, itemData, equipmentStore) + } - const inventoryItem = this.findEquipmentItemById(registryItem.id) - if (inventoryItem) { - // Level up inventory item - if (inventoryItem.level < inventoryItem.maxLevel) { - this.levelUpInventoryItem(inventoryItem) - return true - } - return false // Early return - } + // Handle non-equipment items + return this.handleNonEquipmentItem(registryItem, itemData) + }, - // Add new equipment item to inventory with amount: 1 - const newItem: Item = { - ...registryItem, - amount: 1, // Ensure amount is set to 1 - level: itemData.level || 1, - maxLevel: registryItem.maxLevel || 5, - // Include other necessary properties - } - this.inventory.push(newItem) - this.sortInventory() - return true // Early return + isEquipmentItem(item: Item): boolean { + return item.type === 'equipment' + }, + + handleEquipmentItem(registryItem: Item, itemData: Partial, equipmentStore) { + const equippedItem = equipmentStore.findEquippedItemById(registryItem.id) + if (equippedItem) { + return this.levelUpIfPossible(equippedItem, equipmentStore.levelUpEquippedItem) + } + + const inventoryItem = this.findEquipmentItemById(registryItem.id) + if (inventoryItem) { + return this.levelUpIfPossible(inventoryItem, this.levelUpInventoryItem) + } + + // If the equipment item is not found in inventory or equipped, add as a new item + this.addNewEquipmentItem(registryItem, itemData) + return true + }, + + levelUpIfPossible(item: Item, levelUpFn: (item: Item) => void): boolean { + if (item.level < item.maxLevel) { + levelUpFn(item) + return true + } + return false + }, + + addNewEquipmentItem(registryItem: Item, itemData: Partial) { + const newItem: Item = { + ...registryItem, + amount: 1, + level: itemData.level || 1, + maxLevel: registryItem.maxLevel || 5, } + this.inventory.push(newItem) + this.sortInventory() + }, - // Handle non-equipment items (stackable) + handleNonEquipmentItem(registryItem: Item, itemData: Partial): boolean { const existingItem = this.inventory.find((i) => i.id === itemData.id) + if (existingItem) { - if (existingItem.type !== 'passive') { - existingItem.amount += itemData.amount || 1 - } - this.sortInventory() - return true // Early return + return this.updateExistingItem(existingItem, itemData) } - // Add new non-equipment item + // Add as a new non-equipment item + this.addNewNonEquipmentItem(registryItem, itemData) + return true + }, + + updateExistingItem(existingItem: Item, itemData: Partial): boolean { + if (existingItem.type !== 'passive') { + existingItem.amount += itemData.amount || 1 + } + this.sortInventory() + return true + }, + + addNewNonEquipmentItem(registryItem: Item, itemData: Partial) { const newItem: Item = { ...registryItem, amount: itemData.amount || 1, - // Include other necessary properties } this.inventory.push(newItem) this.sortInventory() - return true }, - findEquipmentItemById(itemId: string): Item | null { return this.inventory.find( (item) => item.id === itemId && item.type === 'equipment', @@ -272,6 +292,10 @@ export const useInventoryStore = defineStore('inventoryStore', { return itemRegistry.find(item => item.id === itemId) ?? null }, + getItemFromInventory(itemId: string): Item | null { + return this.inventory.find(item => item.id === itemId) ?? null + }, + applyPassiveEffects() { console.log('Applying passive effects') this.inventory.forEach(item => { diff --git a/src/views/Training/TrainingMining.vue b/src/views/Training/TrainingMining.vue index aec9613..f37435f 100644 --- a/src/views/Training/TrainingMining.vue +++ b/src/views/Training/TrainingMining.vue @@ -91,7 +91,7 @@ @@ -166,8 +166,8 @@ function startMining(resource: MiningResource) { } // Stop mining action -function stopMining() { - trainingStore.stopMining() +function stopMining(resource: MiningResource) { + trainingStore.stopMining(resource.name) } // Helper to check if a resource is currently being mined