From 520a640ea0850bfccd7bd6657dafabb1e37889e6 Mon Sep 17 00:00:00 2001 From: Andre Padez Date: Fri, 31 Oct 2025 00:21:20 +0000 Subject: [PATCH] feat: add foodsPerLevel to GameOptions with default of 10 - New option: foodsPerLevel (defaults to 10) - Controls how many foods must be eaten to level up - Example: useGame({ foodsPerLevel: 5 }) --- src/types.ts | 1 + src/useGame/useGame.ts | 4 ++-- src/useGameStatus/useGameStatus.ts | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/types.ts b/src/types.ts index 66e0423..6270e93 100755 --- a/src/types.ts +++ b/src/types.ts @@ -33,6 +33,7 @@ export type GameOptions = { rows?: number; timeLimit?: number; startingLevel?: number; + foodsPerLevel?: number; }; // ============================================================================ diff --git a/src/useGame/useGame.ts b/src/useGame/useGame.ts index 283fd97..d795183 100755 --- a/src/useGame/useGame.ts +++ b/src/useGame/useGame.ts @@ -8,7 +8,7 @@ import { useKeyBindings, type UseKeyBindingsType } from "./useKeyBindings"; import { useScore } from "../useScore"; export function useGame(options?: GameOptions, platformHook?: unknown): GameManager { - const { cols = 30, rows = 20, startingLevel } = options ?? {}; + const { cols = 30, rows = 20, startingLevel, foodsPerLevel } = options ?? {}; const boardManager = useBoard(); const { containerRef, renderBoard } = boardManager; @@ -18,7 +18,7 @@ export function useGame(options?: GameOptions, platformHook?: unknown): GameMana const foodManager = useFood(cols, rows, boardManager); - const gameManager = useGameStatus(boardManager, snakeManager, foodManager, { startingLevel }); + const gameManager = useGameStatus(boardManager, snakeManager, foodManager, { startingLevel, foodsPerLevel }); const { gameStatus, setGameStatus, startGame, quitGame, togglePause, score, level } = gameManager; useEffect(() => { diff --git a/src/useGameStatus/useGameStatus.ts b/src/useGameStatus/useGameStatus.ts index 9b09cad..c62a9c6 100755 --- a/src/useGameStatus/useGameStatus.ts +++ b/src/useGameStatus/useGameStatus.ts @@ -4,10 +4,9 @@ import type { UseBoardType } from "../useBoard"; import type { UseCursorType } from "../useCursor"; import type { UseFoodType } from "../useGame/useFood"; -const FOODS_PER_LEVEL = 2; - export type UseGameStatusParams = { startingLevel?: number; + foodsPerLevel?: number; }; export function useGameStatus( @@ -17,6 +16,7 @@ export function useGameStatus( params?: UseGameStatusParams ) { const startingLevel = params?.startingLevel ?? 1; + const foodsPerLevel = params?.foodsPerLevel ?? 10; const { containerRef } = boardManager; const { initSnake, clearSnake, snakeBodyRef } = snakeManager; const { spawnFood, clearFood } = foodManager; @@ -91,7 +91,7 @@ export function useGameStatus( shouldGrow = true; // Check BEFORE incrementing if we're about to level up - const willLevelUp = (foodsEatenRef.current + 1) >= FOODS_PER_LEVEL; + const willLevelUp = (foodsEatenRef.current + 1) >= foodsPerLevel; foodsEatenRef.current += 1;