feat: add maxLevel to GameOptions with default of 25
- New option: maxLevel (defaults to 25)
- Once a player reaches maxLevel, levels no longer progress
- Prevents unlimited level scaling and FPS increases
- Example: useGame({ maxLevel: 10 })
This commit is contained in:
@@ -34,6 +34,7 @@ export type GameOptions = {
|
||||
timeLimit?: number;
|
||||
startingLevel?: number;
|
||||
foodsPerLevel?: number;
|
||||
maxLevel?: number;
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
|
||||
@@ -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, foodsPerLevel } = options ?? {};
|
||||
const { cols = 30, rows = 20, startingLevel, foodsPerLevel, maxLevel } = 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, foodsPerLevel });
|
||||
const gameManager = useGameStatus(boardManager, snakeManager, foodManager, { startingLevel, foodsPerLevel, maxLevel });
|
||||
const { gameStatus, setGameStatus, startGame, quitGame, togglePause, score, level } = gameManager;
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -7,6 +7,7 @@ import type { UseFoodType } from "../useGame/useFood";
|
||||
export type UseGameStatusParams = {
|
||||
startingLevel?: number;
|
||||
foodsPerLevel?: number;
|
||||
maxLevel?: number;
|
||||
};
|
||||
|
||||
export function useGameStatus(
|
||||
@@ -17,6 +18,7 @@ export function useGameStatus(
|
||||
) {
|
||||
const startingLevel = params?.startingLevel ?? 1;
|
||||
const foodsPerLevel = params?.foodsPerLevel ?? 10;
|
||||
const maxLevel = params?.maxLevel ?? 25;
|
||||
const { containerRef } = boardManager;
|
||||
const { initSnake, clearSnake, snakeBodyRef } = snakeManager;
|
||||
const { spawnFood, clearFood } = foodManager;
|
||||
@@ -99,7 +101,7 @@ export function useGameStatus(
|
||||
const pointsToAdd = levelRef.current;
|
||||
|
||||
// Update level OUTSIDE of setScore to avoid multiple triggers
|
||||
if (willLevelUp) {
|
||||
if (willLevelUp && levelRef.current < maxLevel) {
|
||||
const newLevel = levelRef.current + 1;
|
||||
levelRef.current = newLevel;
|
||||
setLevel(newLevel);
|
||||
|
||||
Reference in New Issue
Block a user