From 034f90e6358165f85fb082f2ccfe2aa8c0116557 Mon Sep 17 00:00:00 2001 From: billy Date: Sun, 30 Mar 2025 14:41:19 -0400 Subject: [PATCH] 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. --- src/components/BuildingButton.tsx | 34 ++++++++++++++++------ src/store/gameStore.ts | 47 ++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/src/components/BuildingButton.tsx b/src/components/BuildingButton.tsx index b5fa69c..72b834d 100644 --- a/src/components/BuildingButton.tsx +++ b/src/components/BuildingButton.tsx @@ -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 ( Buy ({cost} points) - +