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

View File

@ -72,6 +72,8 @@ interface GameState {
tick: () => void tick: () => void
upgradeBuilding: (buildingType: BuildingType) => void upgradeBuilding: (buildingType: BuildingType) => void
getClickPowerUpgradeCost: () => number getClickPowerUpgradeCost: () => number
canUpgradeBuilding: (buildingType: BuildingType) => boolean
getBuildingUpgradeCost: (buildingType: BuildingType) => number
// New getter for available buildings // New getter for available buildings
getAvailableBuildings: () => BuildingInfo[] getAvailableBuildings: () => BuildingInfo[]
@ -315,7 +317,14 @@ const calculateUpgradeCost = (buildingType: BuildingType, currentLevel: number):
const tier = getBuildingTier(buildingType) const tier = getBuildingTier(buildingType)
const multiplier = getTierMultiplier(tier) 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 // Calculate building production based on level, count, and tier
@ -422,10 +431,41 @@ export const useGameStore = create<GameState>()(
return calculateClickPowerUpgradeCost(clickPowerUpgrades) 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) => { upgradeBuilding: (buildingType: BuildingType) => {
const state = get() const state = get()
const currentLevel = state[`${buildingType}Level` as keyof GameState] as number const currentLevel = state[`${buildingType}Level` as keyof GameState] as number
const cost = calculateUpgradeCost(buildingType, currentLevel) 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) { if (state.points >= cost && state[buildingType] > 0) {
// Play upgrade sound instead of purchase sound // Play upgrade sound instead of purchase sound
@ -440,12 +480,17 @@ export const useGameStore = create<GameState>()(
const newProduction = calculateProduction(buildingType, newLevel, count) const newProduction = calculateProduction(buildingType, newLevel, count)
const productionDifference = newProduction - oldProduction const productionDifference = newProduction - oldProduction
console.log(`Upgrade successful! New level: ${newLevel}, Production increase: +${productionDifference}/s`)
return { return {
points: state.points - cost, points: state.points - cost,
[`${buildingType}Level`]: newLevel, [`${buildingType}Level`]: newLevel,
pointsPerSecond: state.pointsPerSecond + productionDifference, pointsPerSecond: state.pointsPerSecond + productionDifference,
} }
}) })
} else {
// Log why upgrade failed
console.warn(`Upgrade failed! ${state.points < cost ? 'Not enough points' : 'No buildings owned'}`)
} }
}, },