From 07e3b2071c408626721f4fb6ba2b40a0b4579035 Mon Sep 17 00:00:00 2001 From: Andre Padez Date: Thu, 30 Oct 2025 23:00:06 +0000 Subject: [PATCH] fix(phase1.4): fix level-up timing in score calculation - Level up BEFORE adding points, not after - When foodsEatenRef reaches threshold: 1. Increment level first 2. Reset food counter 3. THEN add points at new level - Correct flow: eat 2 foods at level 1 (+1,+1=2pts), level up, next food worth +2pts - Was: eat at level 1, level up happens, get 2pts (wrong level) - Now: eat at level 1, eat at level 1, level up, next food gets 2pts (correct) --- src/useGameStatus/useGameStatus.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/useGameStatus/useGameStatus.ts b/src/useGameStatus/useGameStatus.ts index fdaf939..49b9506 100755 --- a/src/useGameStatus/useGameStatus.ts +++ b/src/useGameStatus/useGameStatus.ts @@ -90,14 +90,20 @@ export function useGameStatus( foodsEatenRef.current += 1; setScore((prev) => { - let newScore = prev + levelRef.current; + let newScore = prev; + let currentLevel = levelRef.current; + + // Check if we should level up BEFORE adding points if (foodsEatenRef.current >= FOODS_PER_LEVEL) { - const newLevel = levelRef.current + 1; - levelRef.current = newLevel; - setLevel(newLevel); - currentFpsRef.current = INITIAL_FPS + (newLevel - 1); + currentLevel = levelRef.current + 1; + levelRef.current = currentLevel; + setLevel(currentLevel); + currentFpsRef.current = INITIAL_FPS + (currentLevel - 1); foodsEatenRef.current = 0; } + + // Add points at the current (possibly new) level + newScore = prev + currentLevel; foodManagerRef.current.spawnFood( snakeManagerRef.current.snakeBodyRef.current,