working on useGame

This commit is contained in:
2025-10-15 17:29:53 +01:00
commit 8acc16fc1c
37 changed files with 3320 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
import { useEffect } from "react";
import type { UseGameType } from "../../src/useGame/useGame";
export const useKeyBindings = (gameManager: UseGameType) => {
const { gameStatus, initGame, stopGame, clearLog, resetCount } = gameManager;
useEffect(() => {
const handler = (e: KeyboardEvent) => {
// start game
if (e.key === "i" || e.key === "I") {
if (["waiting", "game-over", "game-won"].includes(gameStatus)) {
clearLog();
resetCount();
if (gameStatus === "game-over" || gameStatus === "game-won") {
stopGame();
setTimeout(() => initGame(true), 0);
} else {
initGame(true);
}
return;
}
}
// ignore other keys while waiting
if (gameStatus === "waiting") return;
// cancel count or stop game
if (e.key === "Escape") {
stopGame();
resetCount();
return;
}
// quit
if (e.key === "q" || e.key === "Q") {
stopGame();
resetCount();
return;
}
};
window.addEventListener("keydown", handler);
return () => window.removeEventListener("keydown", handler);
}, [gameStatus, initGame, stopGame, clearLog, resetCount]);
};