From 1f654485ca9b9de485a23eb73300dc6a3cfe54dc Mon Sep 17 00:00:00 2001 From: Andre Padez Date: Thu, 30 Oct 2025 23:07:51 +0000 Subject: [PATCH] fix(phase1.4): save current level BEFORE leveling up for score calculation - Save pointsToAdd = levelRef.current at start of setScore - Use saved value for scoring, not levelRef.current which gets changed - This ensures 1st food adds 1 point at level 1, then level up to 2 - 2nd food adds 2 points at level 2, then level up to 3, etc. - Correct sequence: +1, +2, +3, +4... with level ups after each --- src/useGameStatus/useGameStatus.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/useGameStatus/useGameStatus.ts b/src/useGameStatus/useGameStatus.ts index 123ef00..e728bed 100755 --- a/src/useGameStatus/useGameStatus.ts +++ b/src/useGameStatus/useGameStatus.ts @@ -95,8 +95,11 @@ export function useGameStatus( console.log(`[FOOD] Eaten: ${foodsEatenRef.current}, Will Level Up: ${willLevelUp}, Current Level: ${levelRef.current}`); setScore((prev) => { + // Save the current level BEFORE any changes + const pointsToAdd = levelRef.current; + // Add points at CURRENT level - let newScore = prev + levelRef.current; + let newScore = prev + pointsToAdd; // Level up AFTER this food if threshold was reached if (willLevelUp) { @@ -108,7 +111,7 @@ export function useGameStatus( console.log(`[LEVELUP] New Level: ${newLevel}`); } - console.log(`[SCORE] Adding ${levelRef.current} points, Total: ${newScore}`); + console.log(`[SCORE] Adding ${pointsToAdd} points, Total: ${newScore}`); foodManagerRef.current.spawnFood( snakeManagerRef.current.snakeBodyRef.current,