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)
This commit is contained in:
2025-10-30 23:00:06 +00:00
parent 6926a080f6
commit 07e3b2071c
+11 -5
View File
@@ -90,14 +90,20 @@ export function useGameStatus(
foodsEatenRef.current += 1; foodsEatenRef.current += 1;
setScore((prev) => { 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) { if (foodsEatenRef.current >= FOODS_PER_LEVEL) {
const newLevel = levelRef.current + 1; currentLevel = levelRef.current + 1;
levelRef.current = newLevel; levelRef.current = currentLevel;
setLevel(newLevel); setLevel(currentLevel);
currentFpsRef.current = INITIAL_FPS + (newLevel - 1); currentFpsRef.current = INITIAL_FPS + (currentLevel - 1);
foodsEatenRef.current = 0; foodsEatenRef.current = 0;
} }
// Add points at the current (possibly new) level
newScore = prev + currentLevel;
foodManagerRef.current.spawnFood( foodManagerRef.current.spawnFood(
snakeManagerRef.current.snakeBodyRef.current, snakeManagerRef.current.snakeBodyRef.current,