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 })
This commit is contained in:
+1
-1
@@ -3,7 +3,7 @@ import "@vimazing/vim-snake/game.css";
|
|||||||
import { useKeyBindings } from "./useKeyBindings";
|
import { useKeyBindings } from "./useKeyBindings";
|
||||||
|
|
||||||
function App() {
|
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 formatTime = (ms: number) => {
|
||||||
const seconds = Math.floor(ms / 1000);
|
const seconds = Math.floor(ms / 1000);
|
||||||
|
|||||||
+2
-2
@@ -29,8 +29,8 @@ export type SnakeBody = Position[];
|
|||||||
export type KeyLogEntry = { key: string; timestamp: number };
|
export type KeyLogEntry = { key: string; timestamp: number };
|
||||||
|
|
||||||
export type GameOptions = {
|
export type GameOptions = {
|
||||||
cols: number;
|
cols?: number;
|
||||||
rows: number;
|
rows?: number;
|
||||||
timeLimit?: number;
|
timeLimit?: number;
|
||||||
startingLevel?: number;
|
startingLevel?: number;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import { useGameStatus } from "../useGameStatus";
|
|||||||
import { useKeyBindings, type UseKeyBindingsType } from "./useKeyBindings";
|
import { useKeyBindings, type UseKeyBindingsType } from "./useKeyBindings";
|
||||||
import { useScore } from "../useScore";
|
import { useScore } from "../useScore";
|
||||||
|
|
||||||
export function useGame(options: GameOptions, platformHook?: unknown): GameManager {
|
export function useGame(options?: GameOptions, platformHook?: unknown): GameManager {
|
||||||
const { cols, rows, startingLevel } = options;
|
const { cols = 30, rows = 20, startingLevel } = options ?? {};
|
||||||
|
|
||||||
const boardManager = useBoard();
|
const boardManager = useBoard();
|
||||||
const { containerRef, renderBoard } = boardManager;
|
const { containerRef, renderBoard } = boardManager;
|
||||||
|
|||||||
Reference in New Issue
Block a user