food score

This commit is contained in:
2025-10-16 17:50:53 +01:00
parent d02a8e9618
commit 835f68de72
+14 -6
View File
@@ -18,6 +18,7 @@ export function useGameStatus(
const [gameStatus, setGameStatus] = useState<GameStatus>("waiting");
const [score, setScore] = useState(0);
const [level, setLevel] = useState(1);
const levelRef = useRef(1);
const foodsEatenRef = useRef(0);
const gameLoopRef = useRef<number | null>(null);
const lastUpdateRef = useRef<number>(0);
@@ -34,6 +35,7 @@ export function useGameStatus(
spawnFood(snakeBodyRef.current, 3);
setScore(0);
setLevel(1);
levelRef.current = 1;
foodsEatenRef.current = 0;
currentFpsRef.current = INITIAL_FPS;
collisionGracePeriodRef.current = false;
@@ -99,22 +101,28 @@ export function useGameStatus(
foodsEatenRef.current += 1;
setScore((prev) => {
let newScore = prev + levelRef.current;
console.log('newscore:', newScore, 'level:');
if (foodsEatenRef.current >= FOODS_PER_LEVEL) {
setLevel((lvl) => {
const newLevel = lvl + 1;
newScore--;
console.log('newScore after decrement:', newScore);
const newLevel = levelRef.current + 1;
levelRef.current = newLevel;
setLevel(newLevel);
currentFpsRef.current = INITIAL_FPS + (newLevel - 1);
console.log(`Level up! Level ${newLevel}, FPS: ${currentFpsRef.current}`);
return newLevel;
});
foodsEatenRef.current = 0;
}
setScore((prev) => prev + level);
foodManagerRef.current.spawnFood(
snakeManagerRef.current.snakeBodyRef.current,
1
);
console.log('Final newScore to be set:', newScore);
return newScore;
});
}
lastUpdateRef.current = timestamp;