From d2e4ee550b53370b4c5d1abf1b43e5a8f23ab675 Mon Sep 17 00:00:00 2001 From: billy Date: Sun, 30 Mar 2025 16:24:59 -0400 Subject: [PATCH] Add player acceptance state to game store and update interaction logic. Implemented playerAccepted flag to control game actions based on acceptance of terms. Enhanced App component to set player acceptance on terms agreement and adjusted global click handlers to check for player acceptance before triggering actions. --- src/App.tsx | 6 ++++-- src/main.tsx | 10 ++++++---- src/store/gameStore.ts | 19 ++++++++++++++++++- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 709f81f..c1297c8 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -36,6 +36,7 @@ function App() { getAvailableBuildings, tick, click, + setPlayerAccepted, } = useGameStore() const [agreedToTerms, setAgreedToTerms] = useState(false) @@ -72,8 +73,9 @@ function App() { if (agreedToTerms) { // Initialize audio on first user interaction initAudio(); - setHasStarted(true) - onClose() + setHasStarted(true); + setPlayerAccepted(true); // Set player as accepted in game store + onClose(); } } diff --git a/src/main.tsx b/src/main.tsx index 2dc7666..8bbd9b0 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -29,18 +29,20 @@ function AppWithGlobalHandlers() { useEffect(() => { const handleGlobalClick = () => { - // Only trigger if game has started + // Only trigger if game has started or the game is in a fresh state const gameState = useGameStore.getState(); - if ((gameStarted || gameState.points > 0) && gameState.playerLevel >= 1) { + // Also check if player has accepted employment + if ((gameStarted || gameState.points >= 0) && gameState.playerAccepted) { playClickSound(); gameState.click(); } }; const handleGlobalKeyPress = () => { - // Only trigger if game has started + // Only trigger if game has started or the game is in a fresh state const gameState = useGameStore.getState(); - if ((gameStarted || gameState.points > 0) && gameState.playerLevel >= 1) { + // Also check if player has accepted employment + if ((gameStarted || gameState.points >= 0) && gameState.playerAccepted) { playClickSound(); gameState.click(); } diff --git a/src/store/gameStore.ts b/src/store/gameStore.ts index e3b56eb..5c59d0c 100644 --- a/src/store/gameStore.ts +++ b/src/store/gameStore.ts @@ -63,6 +63,9 @@ interface GameState { // Player level playerLevel: number + // Player state + playerAccepted: boolean + // Actions click: () => void buyBuilding: (buildingType: BuildingType) => void @@ -73,6 +76,7 @@ interface GameState { getClickPowerUpgradeCost: () => number canUpgradeBuilding: (buildingType: BuildingType) => boolean getBuildingUpgradeCost: (buildingType: BuildingType) => number + setPlayerAccepted: (accepted: boolean) => void // New getter for available buildings getAvailableBuildings: () => BuildingInfo[] @@ -220,6 +224,7 @@ const initialState = { clickPowerUpgrades: 0, pointsPerSecond: 0, playerLevel: 1, + playerAccepted: false, } // Production rates per building @@ -363,7 +368,11 @@ export const useGameStore = create()( ...initialState, click: () => { - const { clickPower, autoClickers } = get() + const { clickPower, autoClickers, playerAccepted } = get() + + // Only allow clicks if player has accepted the employment agreement + if (!playerAccepted) return + const pointsPerClick = clickPower * (1 + autoClickers * 0.1) set((state) => ({ points: state.points + pointsPerClick, @@ -495,11 +504,15 @@ export const useGameStore = create()( resetGame: () => { set(initialState) + window.location.reload() }, tick: () => { const state = get() + // Don't add production if player hasn't accepted employment + if (!state.playerAccepted) return + // Add production from buildings if (state.pointsPerSecond > 0) { set((state) => ({ @@ -525,6 +538,10 @@ export const useGameStore = create()( const { playerLevel } = get() return getAvailableBuildings(playerLevel) }, + + setPlayerAccepted: (accepted: boolean) => { + set({ playerAccepted: accepted }) + }, }), { name: 'clicker-game',