From 7800acaf56f0cf984b060eedd659f71d4fe3521a Mon Sep 17 00:00:00 2001 From: Andre Padez Date: Wed, 22 Oct 2025 22:07:59 +0100 Subject: [PATCH] feat: add timeLimit option and time-based game-over logic - Add timeLimit to GameOptions (default 600 seconds) - Game-over triggers when time limit exceeded - Remove all efficiency-based game-over logic - Pass timeLimit through to useScore - Check time limit during started and has-key states --- src/types.ts | 1 + src/useGame.ts | 4 ++-- src/useScore/useScore.ts | 15 +++++++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/types.ts b/src/types.ts index 4439d32..0e5f4ad 100644 --- a/src/types.ts +++ b/src/types.ts @@ -11,6 +11,7 @@ export * from './useScore/types'; export type GameOptions = { rows: number; cols: number; + timeLimit?: number; }; export type KeyLogEntry = { key: string; timestamp: number }; diff --git a/src/useGame.ts b/src/useGame.ts index 9f3f547..c93cb45 100644 --- a/src/useGame.ts +++ b/src/useGame.ts @@ -6,7 +6,7 @@ import { useHeroRender } from './useCursor'; import { useScore } from './useScore'; export function useGame(options: GameOptions, platformHook?: unknown): GameManager { - const { cols, rows } = options; + const { cols, rows, timeLimit = 600 } = options; const board = useBoard(cols, rows); const { containerRef, renderBoard } = board; const gameStatusManager = useGameStatus(); @@ -14,7 +14,7 @@ export function useGame(options: GameOptions, platformHook?: unknown): GameManag const hero = cursor.hero; const renderer = useHeroRender({ gameStatus: gameStatusManager.gameStatus, board, hero }); - const scoreManager = useScore({ board, hero, gameStatusManager, keyManager, rows, cols }); + const scoreManager = useScore({ board, hero, gameStatusManager, keyManager, rows, cols, timeLimit }); const gameManager: GameManager = { containerRef, diff --git a/src/useScore/useScore.ts b/src/useScore/useScore.ts index ded67f1..aac5c37 100644 --- a/src/useScore/useScore.ts +++ b/src/useScore/useScore.ts @@ -13,11 +13,12 @@ type UseScoreParams = { keyManager?: { keyLog: any[] }; rows: number; cols: number; + timeLimit: number; }; -export function useScore({ board, hero, gameStatusManager, keyManager, rows, cols }: UseScoreParams) { +export function useScore({ board, hero, gameStatusManager, keyManager, rows, cols, timeLimit }: UseScoreParams) { const timer = useTimer(); - const { gameStatus } = gameStatusManager; + const { gameStatus, setGameStatus } = gameStatusManager; const { timeValue, startTimer, stopTimer, resetTimer } = timer; const { @@ -35,6 +36,16 @@ export function useScore({ board, hero, gameStatusManager, keyManager, rows, col useScoreTime({ gameStatus, timer }); + useEffect(() => { + if (gameStatus !== "started" && gameStatus !== "has-key") return; + + const timeLimitMs = timeLimit * 1000; + if (timeValue >= timeLimitMs) { + setGameStatus("game-over"); + return; + } + }, [gameStatus, timeValue, timeLimit, setGameStatus]); + useEffect(() => { if (gameStatus !== "game-won") return;