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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user