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:
@@ -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
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user