Move character input handling to game module

- Add keyboard handler for character input in useGame
- Character validation and typing now handled at module level
- platformHook focuses only on lifecycle: Space (start/restart), Escape (quit)
- Game module self-contained for core typing logic
This commit is contained in:
2025-11-05 10:43:52 +00:00
parent 5187e2e21e
commit 04846be9e5
2 changed files with 19 additions and 12 deletions
@@ -29,13 +29,6 @@ export const usePlatformHook = (gameManager: GameManager) => {
gameManager.quitGame();
return;
}
// handle letter and punctuation typing during game
if (gameManager.gameStatus === "started") {
if (ev.key.length === 1 && /[a-zA-Z0-9.,!?;:'"()\-/& ]/.test(ev.key)) {
gameManager.handleTypedLetter(ev.key);
}
}
};
window.addEventListener("keydown", handler);
+19 -5
View File
@@ -79,12 +79,26 @@ export function useGame(
gameStatusManager.setGameLoopCallback(handleGameLoopTick);
}, []);
// Initial render on mount
useEffect(() => {
renderBoardWrapped();
}, []);
// Initial render on mount
useEffect(() => {
renderBoardWrapped();
}, []);
const scoreManager = useScore(gameStatusManager);
// Handle character input for typing
useEffect(() => {
const handleCharacterInput = (ev: KeyboardEvent) => {
if (gameStatusManager.gameStatus === "started") {
if (ev.key.length === 1 && /[a-zA-Z0-9.,!?;:'"()\-/& ]/.test(ev.key)) {
gameStatusManager.handleTypedLetter(ev.key);
}
}
};
window.addEventListener("keydown", handleCharacterInput);
return () => window.removeEventListener("keydown", handleCharacterInput);
}, [gameStatusManager.gameStatus]);
const scoreManager = useScore(gameStatusManager);
const gameManager: GameManager = {
containerRef: boardManager.containerRef,