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
This commit is contained in:
@@ -11,6 +11,7 @@ export * from './useScore/types';
|
|||||||
export type GameOptions = {
|
export type GameOptions = {
|
||||||
rows: number;
|
rows: number;
|
||||||
cols: number;
|
cols: number;
|
||||||
|
timeLimit?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type KeyLogEntry = { key: string; timestamp: number };
|
export type KeyLogEntry = { key: string; timestamp: number };
|
||||||
|
|||||||
+2
-2
@@ -6,7 +6,7 @@ import { useHeroRender } from './useCursor';
|
|||||||
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 } = options;
|
const { cols, rows, timeLimit = 600 } = options;
|
||||||
const board = useBoard(cols, rows);
|
const board = useBoard(cols, rows);
|
||||||
const { containerRef, renderBoard } = board;
|
const { containerRef, renderBoard } = board;
|
||||||
const gameStatusManager = useGameStatus();
|
const gameStatusManager = useGameStatus();
|
||||||
@@ -14,7 +14,7 @@ export function useGame(options: GameOptions, platformHook?: unknown): GameManag
|
|||||||
|
|
||||||
const hero = cursor.hero;
|
const hero = cursor.hero;
|
||||||
const renderer = useHeroRender({ gameStatus: gameStatusManager.gameStatus, board, 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 = {
|
const gameManager: GameManager = {
|
||||||
containerRef,
|
containerRef,
|
||||||
|
|||||||
@@ -13,11 +13,12 @@ type UseScoreParams = {
|
|||||||
keyManager?: { keyLog: any[] };
|
keyManager?: { keyLog: any[] };
|
||||||
rows: number;
|
rows: number;
|
||||||
cols: 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 timer = useTimer();
|
||||||
const { gameStatus } = gameStatusManager;
|
const { gameStatus, setGameStatus } = gameStatusManager;
|
||||||
const { timeValue, startTimer, stopTimer, resetTimer } = timer;
|
const { timeValue, startTimer, stopTimer, resetTimer } = timer;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -35,6 +36,16 @@ export function useScore({ board, hero, gameStatusManager, keyManager, rows, col
|
|||||||
|
|
||||||
useScoreTime({ gameStatus, timer });
|
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(() => {
|
useEffect(() => {
|
||||||
if (gameStatus !== "game-won") return;
|
if (gameStatus !== "game-won") return;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user