feat(phase1.2b): implement Nokia-style key buffering system

- Add nextDirectionRef to buffer player input
- Update changeDirection() to queue input instead of immediate execution
- Add applyBufferedDirection() to safely apply buffered input
- Buffer prevents 180° reversals for safe turns
- Call applyBufferedDirection() after each successful move in game loop
- Remove collision grace period - no longer needed with key buffering
- Remove collisionGracePeriodRef and related logic
- Clean up all console.log statements from useGameStatus
- Game now feels responsive at high speeds
- Allows pre-emptive turning on board edges (Nokia Snake style)
- Build and example app work correctly
This commit is contained in:
2025-10-30 22:39:21 +00:00
parent f9cd9c60e7
commit 762bc86878
3 changed files with 30 additions and 36 deletions
-10
View File
@@ -58,9 +58,7 @@ export function useGameStatus(
};
useEffect(() => {
console.log("useGameStatus effect - gameStatus:", gameStatus);
if (gameStatus === "started") {
console.log("Starting game loop");
if (paused) return;
let shouldGrow = false;
lastUpdateRef.current = performance.now();
@@ -70,9 +68,7 @@ export function useGameStatus(
const elapsed = timestamp - lastUpdateRef.current;
if (elapsed >= msPerFrame) {
console.log("Game loop tick");
const result = snakeManagerRef.current.moveSnake(shouldGrow);
console.log("Move result:", result);
shouldGrow = false;
@@ -96,7 +92,6 @@ export function useGameStatus(
const head = snakeManagerRef.current.snakeBodyRef.current[0];
if (head && foodManagerRef.current.checkFoodCollision(head)) {
console.log("Food eaten!");
foodManagerRef.current.removeFood(head);
shouldGrow = true;
@@ -104,15 +99,12 @@ export function useGameStatus(
setScore((prev) => {
let newScore = prev + levelRef.current;
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);
console.log(`Level up! Level ${newLevel}, FPS: ${currentFpsRef.current}`);
foodsEatenRef.current = 0;
}
@@ -120,7 +112,6 @@ export function useGameStatus(
snakeManagerRef.current.snakeBodyRef.current,
1
);
console.log('Final newScore to be set:', newScore);
return newScore;
});
@@ -135,7 +126,6 @@ export function useGameStatus(
gameLoopRef.current = requestAnimationFrame(gameLoop);
return () => {
console.log("Cleaning up game loop");
if (gameLoopRef.current) {
cancelAnimationFrame(gameLoopRef.current);
gameLoopRef.current = null;