food score

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