- Add setCell method to useBoard to update board state - Handle digit keys 1-9 in edit mode in useCursor - Move boardRendered counter to useBoard for proper cursor re-rendering - Fix golden glow disappearing after digit entry - Digits display with .user-entered class via renderer
58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
import type { GameManager, GameOptions } from './types';
|
|
import { useBoard } from './useBoard/useBoard';
|
|
import { useGameStatus } from './useGameStatus';
|
|
import { useCursor } from './useCursor';
|
|
import { useCursorRender } from './useCursor/useCursorRender';
|
|
|
|
export function useGame(options: GameOptions = {}, platformHook?: unknown): GameManager {
|
|
const { difficulty = 'easy' } = options;
|
|
const board = useBoard();
|
|
const { containerRef, renderBoard, generateBoard, boardRendered } = board;
|
|
|
|
const gameStatusManager = useGameStatus();
|
|
const { gameStatus } = gameStatusManager;
|
|
|
|
const [keyLog, setKeyLog] = useState<{ key: string; timestamp: number }[]>([]);
|
|
|
|
const cursor = useCursor(gameStatusManager, containerRef, board);
|
|
const cursorPosition = cursor.position();
|
|
const cursorMode = cursor.mode();
|
|
|
|
useCursorRender(containerRef, cursorPosition, cursorMode, gameStatus, boardRendered);
|
|
|
|
useEffect(() => {
|
|
renderBoard();
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (gameStatus === 'started') {
|
|
generateBoard(difficulty);
|
|
renderBoard();
|
|
} else if (gameStatus === 'waiting') {
|
|
renderBoard();
|
|
}
|
|
}, [gameStatus, difficulty]);
|
|
|
|
const gameManager: GameManager = {
|
|
containerRef,
|
|
renderBoard,
|
|
...gameStatusManager,
|
|
cursor,
|
|
scoreManager: {
|
|
timeValue: 0,
|
|
keystrokes: 0,
|
|
finalScore: null,
|
|
},
|
|
keyLog,
|
|
clearKeyLog: () => setKeyLog([]),
|
|
getKeyLog: () => [...keyLog],
|
|
};
|
|
|
|
if (typeof platformHook === 'function') {
|
|
platformHook(gameManager);
|
|
}
|
|
|
|
return gameManager;
|
|
}
|