From 5cbb2ee94cd1460a8f92f6fadc13e7df17211275 Mon Sep 17 00:00:00 2001 From: Andre Padez Date: Fri, 31 Oct 2025 00:19:43 +0000 Subject: [PATCH] refactor: make all GameOptions optional with sensible defaults - cols defaults to 30 (was required) - rows defaults to 20 (was required) - timeLimit and startingLevel remain optional - Can now call useGame() with no arguments - Example: useGame(), useGame({}), useGame({ startingLevel: 5 }) --- example/src/App.tsx | 2 +- src/types.ts | 4 ++-- src/useGame/useGame.ts | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index 962af7c..7ba21fd 100755 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -3,7 +3,7 @@ import "@vimazing/vim-snake/game.css"; import { useKeyBindings } from "./useKeyBindings"; function App() { - const { containerRef, gameStatus, scoreManager, keyLog, score, level } = useGame({ cols: 30, rows: 20 }, useKeyBindings); + const { containerRef, gameStatus, scoreManager, keyLog, score, level } = useGame({}, useKeyBindings); const formatTime = (ms: number) => { const seconds = Math.floor(ms / 1000); diff --git a/src/types.ts b/src/types.ts index d5c3123..66e0423 100755 --- a/src/types.ts +++ b/src/types.ts @@ -29,8 +29,8 @@ export type SnakeBody = Position[]; export type KeyLogEntry = { key: string; timestamp: number }; export type GameOptions = { - cols: number; - rows: number; + cols?: number; + rows?: number; timeLimit?: number; startingLevel?: number; }; diff --git a/src/useGame/useGame.ts b/src/useGame/useGame.ts index 0aed33e..283fd97 100755 --- a/src/useGame/useGame.ts +++ b/src/useGame/useGame.ts @@ -7,8 +7,8 @@ import { useGameStatus } from "../useGameStatus"; import { useKeyBindings, type UseKeyBindingsType } from "./useKeyBindings"; import { useScore } from "../useScore"; -export function useGame(options: GameOptions, platformHook?: unknown): GameManager { - const { cols, rows, startingLevel } = options; +export function useGame(options?: GameOptions, platformHook?: unknown): GameManager { + const { cols = 30, rows = 20, startingLevel } = options ?? {}; const boardManager = useBoard(); const { containerRef, renderBoard } = boardManager;