game start/stop, snake rendered on board

This commit is contained in:
2025-10-16 12:56:15 +01:00
parent 39dde6e5e6
commit 6a3cfad409
7 changed files with 216 additions and 13 deletions
+47 -1
View File
@@ -1,3 +1,49 @@
export function useKeyBindings() {
import { useEffect } from "react";
import type { GameStatus, Direction } from "../types";
export type GameBindingContext = {
gameStatus: GameStatus;
startGame: () => void;
stopGame: () => void;
changeDirection: (dir: Direction) => void;
};
type UseKeyBindingsParams = {
gameManager: GameBindingContext;
};
export function useKeyBindings({ gameManager }: UseKeyBindingsParams) {
const { gameStatus, startGame, stopGame, changeDirection } = gameManager;
useEffect(() => {
const handler = (ev: KeyboardEvent) => {
if (!["started"].includes(gameStatus)) return;
switch (ev.key) {
case "h":
case "H":
changeDirection("left");
break;
case "j":
case "J":
changeDirection("down");
break;
case "k":
case "K":
changeDirection("up");
break;
case "l":
case "L":
changeDirection("right");
break;
}
};
window.addEventListener("keydown", handler);
return () => window.removeEventListener("keydown", handler);
}, [gameStatus, startGame, stopGame, changeDirection]);
return {};
}
export type UseKeyBindingsType = ReturnType<typeof useKeyBindings>;