Refactor BuildingButton and gameStore to enhance upgrade logic. Introduced methods for calculating upgrade costs and checking upgrade eligibility directly from the store. Updated tooltip messages for better user feedback on upgrade conditions, improving overall gameplay experience.

This commit is contained in:
billy 2025-03-30 14:41:19 -04:00
parent b75e5d8dd8
commit 034f90e635
2 changed files with 71 additions and 10 deletions

View File

@ -71,20 +71,36 @@ export function BuildingButton({
buildingType,
levelRequirement
}: BuildingButtonProps) {
const { points, playerLevel, upgradeBuilding } = useGameStore()
const {
points,
playerLevel,
upgradeBuilding,
canUpgradeBuilding,
getBuildingUpgradeCost
} = useGameStore()
const canAfford = points >= cost
const meetsLevelRequirement = playerLevel >= levelRequirement
const isDisabledStyle = !canAfford || !meetsLevelRequirement
// Calculate upgrade cost
const calculateUpgradeCost = (currentLevel: number): number => {
return Math.floor(cost * Math.pow(1.5, currentLevel - 1))
}
// Get upgrade cost from the store's actual calculation
// This ensures the displayed cost matches the actual cost used in the upgrade function
const upgradeCost = getBuildingUpgradeCost(buildingType as keyof typeof buildingImages)
const upgradeCost = calculateUpgradeCost(level)
const canUpgrade = points >= upgradeCost && owned > 0
// Use the game store's method to determine if upgrade is possible
const canUpgrade = canUpgradeBuilding(buildingType as keyof typeof buildingImages)
const pointsPerSecond = production.points || 0
// Create a helpful tooltip message
const getUpgradeTooltip = () => {
if (owned === 0) {
return "You need to own this building first";
} else if (points < upgradeCost) {
return `Not enough points (${points}/${upgradeCost})`;
}
return `Upgrade to level ${level + 1}`;
};
return (
<Box
bg="gray.700"
@ -171,9 +187,9 @@ export function BuildingButton({
>
Buy ({cost} points)
</Button>
<Tooltip label={owned === 0 ? "You need to own this building first" : ""}>
<Tooltip label={getUpgradeTooltip()}>
<Button
onClick={() => upgradeBuilding(buildingType as any)}
onClick={() => upgradeBuilding(buildingType as keyof typeof buildingImages)}
opacity={!canUpgrade ? 0.4 : 1}
_hover={{ bg: 'green.600', opacity: !canUpgrade ? 0.4 : 1 }}
cursor={!canUpgrade ? 'not-allowed' : 'pointer'}

View File

@ -72,6 +72,8 @@ interface GameState {
tick: () => void
upgradeBuilding: (buildingType: BuildingType) => void
getClickPowerUpgradeCost: () => number
canUpgradeBuilding: (buildingType: BuildingType) => boolean
getBuildingUpgradeCost: (buildingType: BuildingType) => number
// New getter for available buildings
getAvailableBuildings: () => BuildingInfo[]
@ -315,7 +317,14 @@ const calculateUpgradeCost = (buildingType: BuildingType, currentLevel: number):
const tier = getBuildingTier(buildingType)
const multiplier = getTierMultiplier(tier)
return Math.floor(baseCost * Math.pow(multiplier, currentLevel - 1))
// Formula: baseCost * multiplier^(currentLevel - 1)
// This ensures costs scale exponentially with level
const cost = Math.floor(baseCost * Math.pow(multiplier, currentLevel - 1))
// For debugging
console.log(`Upgrade cost for ${buildingType} at level ${currentLevel}: ${cost} points, tier: ${tier}, multiplier: ${multiplier}`)
return cost
}
// Calculate building production based on level, count, and tier
@ -422,10 +431,41 @@ export const useGameStore = create<GameState>()(
return calculateClickPowerUpgradeCost(clickPowerUpgrades)
},
getBuildingUpgradeCost: (buildingType: BuildingType) => {
const state = get()
const currentLevel = state[`${buildingType}Level` as keyof GameState] as number
return calculateUpgradeCost(buildingType, currentLevel)
},
canUpgradeBuilding: (buildingType: BuildingType) => {
const state = get()
// Check if player owns at least one building
if (state[buildingType] <= 0) {
return false
}
// Get current level and calculate upgrade cost
const currentLevel = state[`${buildingType}Level` as keyof GameState] as number
const cost = calculateUpgradeCost(buildingType, currentLevel)
// Check if player has enough points
return state.points >= cost
},
upgradeBuilding: (buildingType: BuildingType) => {
const state = get()
const currentLevel = state[`${buildingType}Level` as keyof GameState] as number
const cost = calculateUpgradeCost(buildingType, currentLevel)
const ownedCount = state[buildingType]
console.log(`Attempting to upgrade ${buildingType}:
- Current level: ${currentLevel}
- Upgrade cost: ${cost}
- Player points: ${state.points}
- Buildings owned: ${ownedCount}
- Can afford: ${state.points >= cost}
- Owns building: ${ownedCount > 0}
`)
if (state.points >= cost && state[buildingType] > 0) {
// Play upgrade sound instead of purchase sound
@ -440,12 +480,17 @@ export const useGameStore = create<GameState>()(
const newProduction = calculateProduction(buildingType, newLevel, count)
const productionDifference = newProduction - oldProduction
console.log(`Upgrade successful! New level: ${newLevel}, Production increase: +${productionDifference}/s`)
return {
points: state.points - cost,
[`${buildingType}Level`]: newLevel,
pointsPerSecond: state.pointsPerSecond + productionDifference,
}
})
} else {
// Log why upgrade failed
console.warn(`Upgrade failed! ${state.points < cost ? 'Not enough points' : 'No buildings owned'}`)
}
},