Skip to content

Commit

Permalink
fix: few bugs (equipment and mining bugs)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mees committed Oct 19, 2024
1 parent 4fc80bd commit 835c81a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 80 deletions.
3 changes: 1 addition & 2 deletions src/stores/adventureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ export const useAdventureStore = defineStore('adventureStore', {
this.spawnRandomEnemy()
}
},

// Stop all active battles (useful when managing multiple loops)
stopAllBattles() {
this.battleStatus = 'idle'
},
Expand Down Expand Up @@ -574,6 +572,7 @@ export const useAdventureStore = defineStore('adventureStore', {

this.enemySpawned = true
},

getAdventureState() {
return {
armyHealth: this.armyHealth,
Expand Down
83 changes: 49 additions & 34 deletions src/stores/equipmentStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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()
},

Expand Down
106 changes: 65 additions & 41 deletions src/stores/inventoryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item>, 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<Item>) {
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<Item>): 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<Item>): boolean {
if (existingItem.type !== 'passive') {
existingItem.amount += itemData.amount || 1
}
this.sortInventory()
return true
},

addNewNonEquipmentItem(registryItem: Item, itemData: Partial<Item>) {
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',
Expand Down Expand Up @@ -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 => {
Expand Down
6 changes: 3 additions & 3 deletions src/views/Training/TrainingMining.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<button
v-if="isMiningResource(resource) || resource.isDepleted"
class="bg-red-500 text-white px-4 py-2 rounded-lg font-bold hover:bg-red-600"
@click="stopMining"
@click="stopMining(resource)"
>
Stop Mining
</button>
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 835c81a

Please sign in to comment.