starting new structure

This commit is contained in:
2025-10-19 17:59:12 +01:00
parent 4c6bd0d4d4
commit d0a51d7079
50 changed files with 2219 additions and 432 deletions
+13 -22
View File
@@ -1,22 +1,21 @@
import { useEffect } from "react";
import type { UseGameType } from "../../src/";
import type { GameManager } from "../../src";
export const useKeyBindings = (gameManager: UseGameType) => {
const { gameStatus, initGame, stopGame, clearLog, resetCount } = gameManager;
export const useKeyBindings = (gameManager: GameManager) => {
const { gameStatus, renderBoard, startGame, quitGame, clearKeyLog } = gameManager;
useEffect(() => {
const handler = (e: KeyboardEvent) => {
console.log(e.key);
const handler = (ev: KeyboardEvent) => {
// start game
if (e.code === "Space") {
if (ev.code === "Space") {
if (["waiting", "game-over", "game-won"].includes(gameStatus)) {
clearLog();
resetCount();
clearKeyLog();
renderBoard();
if (gameStatus === "game-over" || gameStatus === "game-won") {
stopGame();
setTimeout(() => initGame(), 0);
quitGame();
setTimeout(() => startGame(), 0);
} else {
initGame();
startGame();
}
return;
}
@@ -25,23 +24,15 @@ export const useKeyBindings = (gameManager: UseGameType) => {
// 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();
if (ev.key === "q" || ev.key === "Q") {
quitGame();
return;
}
};
window.addEventListener("keydown", handler);
return () => window.removeEventListener("keydown", handler);
}, [gameStatus, initGame, stopGame, clearLog, resetCount]);
}, [gameStatus]);
};