refactor: enforce new GameOptions signature for useGame
- Remove old signature support (cols, rows, platformHook) - useGame now requires GameOptions object only - Update example app to use new signature - Signature: useGame(options, platformHook?)
This commit is contained in:
+1
-1
@@ -3,7 +3,7 @@ import "@vimazing/vim-snake/game.css";
|
||||
import { useKeyBindings } from "./useKeyBindings";
|
||||
|
||||
function App() {
|
||||
const { containerRef, gameStatus, scoreManager, keyLog, score, level } = useGame(30, 20, useKeyBindings);
|
||||
const { containerRef, gameStatus, scoreManager, keyLog, score, level } = useGame({ cols: 30, rows: 20 }, useKeyBindings);
|
||||
|
||||
const formatTime = (ms: number) => {
|
||||
const seconds = Math.floor(ms / 1000);
|
||||
|
||||
+4
-20
@@ -7,24 +7,8 @@ import { useGameStatus } from "../useGameStatus";
|
||||
import { useKeyBindings, type UseKeyBindingsType } from "./useKeyBindings";
|
||||
import { useScore } from "../useScore";
|
||||
|
||||
export function useGame(optionsOrCols: GameOptions | number, rows?: number, platformHook?: unknown): GameManager {
|
||||
// Support both old signature (cols, rows) and new signature (options)
|
||||
let cols: number;
|
||||
let startingLevel: number | undefined;
|
||||
let actualPlatformHook = platformHook;
|
||||
|
||||
if (typeof optionsOrCols === 'object') {
|
||||
// New signature: useGame(options, platformHook)
|
||||
const options = optionsOrCols as GameOptions;
|
||||
cols = options.cols;
|
||||
rows = options.rows;
|
||||
startingLevel = options.startingLevel;
|
||||
actualPlatformHook = rows as unknown as typeof platformHook;
|
||||
} else {
|
||||
// Old signature: useGame(cols, rows, platformHook)
|
||||
cols = optionsOrCols;
|
||||
rows = rows || 20;
|
||||
}
|
||||
export function useGame(options: GameOptions, platformHook?: unknown): GameManager {
|
||||
const { cols, rows, startingLevel } = options;
|
||||
|
||||
const boardManager = useBoard();
|
||||
const { containerRef, renderBoard } = boardManager;
|
||||
@@ -84,8 +68,8 @@ export function useGame(optionsOrCols: GameOptions | number, rows?: number, plat
|
||||
togglePause,
|
||||
};
|
||||
|
||||
if (typeof actualPlatformHook === "function") {
|
||||
actualPlatformHook(gameManagerResult);
|
||||
if (typeof platformHook === "function") {
|
||||
platformHook(gameManagerResult);
|
||||
}
|
||||
|
||||
return gameManagerResult;
|
||||
|
||||
Reference in New Issue
Block a user