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.

This commit is contained in:
billy 2025-03-30 16:24:59 -04:00
parent f000fecba2
commit d2e4ee550b
3 changed files with 28 additions and 7 deletions

View File

@ -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();
}
}

View File

@ -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();
}

View File

@ -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<GameState>()(
...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<GameState>()(
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<GameState>()(
const { playerLevel } = get()
return getAvailableBuildings(playerLevel)
},
setPlayerAccepted: (accepted: boolean) => {
set({ playerAccepted: accepted })
},
}),
{
name: 'clicker-game',