Files
the-last-startyper/example/src/useKeyBindings.ts
T
pastilhas 80f7aa3f8a chore: initialize typing-chud project with Unified API structure
- Copy vim-snake structure stripped of game-specific logic
- Remove finalScore calculation (raw data only)
- Setup example app with keybinding state management
- Empty board renders and waits for game logic
2025-11-03 21:36:09 +00:00

39 lines
1.0 KiB
TypeScript
Executable File

import { useEffect } from "react";
import type { GameManager } from "../../src";
export const useKeyBindings = (gameManager: GameManager) => {
const { gameStatus, renderBoard, startGame, quitGame, clearKeyLog } = gameManager;
useEffect(() => {
const handler = (ev: KeyboardEvent) => {
// start game
if (ev.code === "Space") {
if (["waiting", "game-over", "game-won"].includes(gameStatus)) {
clearKeyLog();
renderBoard();
if (gameStatus === "game-over" || gameStatus === "game-won") {
quitGame();
setTimeout(() => startGame(), 0);
} else {
startGame();
}
return;
}
}
// ignore other keys while waiting
if (gameStatus === "waiting") return;
// quit
if (ev.key === "q" || ev.key === "Q") {
quitGame();
return;
}
};
window.addEventListener("keydown", handler);
return () => window.removeEventListener("keydown", handler);
}, [gameStatus]);
};