From ec954ccbb4245d267448ee0bd6ffde3a90f5608f Mon Sep 17 00:00:00 2001 From: Andre Padez Date: Sun, 19 Oct 2025 03:34:43 +0100 Subject: [PATCH] simplification --- src/useCursor/useCursor.ts | 11 ++++------- src/useGame.ts | 2 +- src/useKeyBindings/useKeyBindings.ts | 25 +++++-------------------- 3 files changed, 10 insertions(+), 28 deletions(-) diff --git a/src/useCursor/useCursor.ts b/src/useCursor/useCursor.ts index a8e96f2..4e576fb 100644 --- a/src/useCursor/useCursor.ts +++ b/src/useCursor/useCursor.ts @@ -239,13 +239,10 @@ export function useCursor(board: BoardManager, gameStatus: GameStatus) { getLastKey, } - if (gameStatus) { - return { - ...cursorAPI, - hero, - }; - } - return cursorAPI; + return { + ...cursorAPI, + hero, + }; } diff --git a/src/useGame.ts b/src/useGame.ts index 2cbf2cb..8a61951 100644 --- a/src/useGame.ts +++ b/src/useGame.ts @@ -14,7 +14,7 @@ export function useGame(options: GameOptions, platformHook?: unknown): GameManag const cursor = useCursor(board, gameStatus); - const keyManager = useKeyBindings({ cursor, hero: cursor.hero, gameStatus, setGameStatus }); + const keyManager = useKeyBindings({ cursor, gameStatus, setGameStatus }); const gameManager = { containerRef, diff --git a/src/useKeyBindings/useKeyBindings.ts b/src/useKeyBindings/useKeyBindings.ts index c50090b..dd46641 100644 --- a/src/useKeyBindings/useKeyBindings.ts +++ b/src/useKeyBindings/useKeyBindings.ts @@ -1,25 +1,9 @@ -import type { GameStatus, KeyLogEntry, UseHeroType } from "../types"; +import type { GameStatus, KeyLogEntry, UseHeroType, Cursor } from "../types"; import { useEffect, useRef, useState } from "react"; -export type CursorBindingContext = { - moveLeft: (count?: number) => void; - moveRight: (count?: number) => void; - moveUp: (count?: number) => void; - moveDown: (count?: number) => void; - moveToStart: () => void; - moveToEnd: () => void; - moveToTop: () => void; - moveToBottom: () => void; - repeatLastMotion: () => void; - resetCount: () => void; - getCount: () => string; - setCount: (digit: string) => void; - setLastKey: (key: string) => void; - getLastKey: () => string; -}; type UseKeyBindingsParams = { - cursor: CursorBindingContext; + cursor: Cursor; hero: UseHeroType; gameStatus: GameStatus; setGameStatus: (status: GameStatus) => void; @@ -30,10 +14,10 @@ const RELEVANT_KEYS = new Set([ "1", "2", "3", "4", "5", "6", "7", "8", "9" ]); -export const useKeyBindings = ({ cursor, hero, gameStatus, setGameStatus }: UseKeyBindingsParams) => { - +export const useKeyBindings = ({ cursor, gameStatus, setGameStatus }: UseKeyBindingsParams) => { const [keyLog, setKeyLog] = useState([]); const logRef = useRef([]); + const { hero } = cursor; const clearLog = () => { logRef.current = []; @@ -112,6 +96,7 @@ export const useKeyBindings = ({ cursor, hero, gameStatus, setGameStatus }: UseK const count = Math.max(1, parseInt(cursor.getCount() || "1", 10)); cursor.resetCount(); + if (!hero) return; switch (ev.key) { case "h": case "H": hero.moveHero(0, -1, count, gameStatus, setGameStatus); break;