simplification

This commit is contained in:
2025-10-19 03:34:43 +01:00
parent 970bf22a6a
commit ec954ccbb4
3 changed files with 10 additions and 28 deletions
+4 -7
View File
@@ -239,13 +239,10 @@ export function useCursor(board: BoardManager, gameStatus: GameStatus) {
getLastKey, getLastKey,
} }
if (gameStatus) {
return {
...cursorAPI,
hero,
};
}
return cursorAPI; return {
...cursorAPI,
hero,
};
} }
+1 -1
View File
@@ -14,7 +14,7 @@ export function useGame(options: GameOptions, platformHook?: unknown): GameManag
const cursor = useCursor(board, gameStatus); const cursor = useCursor(board, gameStatus);
const keyManager = useKeyBindings({ cursor, hero: cursor.hero, gameStatus, setGameStatus }); const keyManager = useKeyBindings({ cursor, gameStatus, setGameStatus });
const gameManager = { const gameManager = {
containerRef, containerRef,
+5 -20
View File
@@ -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"; 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 = { type UseKeyBindingsParams = {
cursor: CursorBindingContext; cursor: Cursor;
hero: UseHeroType; hero: UseHeroType;
gameStatus: GameStatus; gameStatus: GameStatus;
setGameStatus: (status: GameStatus) => void; setGameStatus: (status: GameStatus) => void;
@@ -30,10 +14,10 @@ const RELEVANT_KEYS = new Set([
"1", "2", "3", "4", "5", "6", "7", "8", "9" "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<KeyLogEntry[]>([]); const [keyLog, setKeyLog] = useState<KeyLogEntry[]>([]);
const logRef = useRef<KeyLogEntry[]>([]); const logRef = useRef<KeyLogEntry[]>([]);
const { hero } = cursor;
const clearLog = () => { const clearLog = () => {
logRef.current = []; logRef.current = [];
@@ -112,6 +96,7 @@ export const useKeyBindings = ({ cursor, hero, gameStatus, setGameStatus }: UseK
const count = Math.max(1, parseInt(cursor.getCount() || "1", 10)); const count = Math.max(1, parseInt(cursor.getCount() || "1", 10));
cursor.resetCount(); cursor.resetCount();
if (!hero) return;
switch (ev.key) { switch (ev.key) {
case "h": case "h":
case "H": hero.moveHero(0, -1, count, gameStatus, setGameStatus); break; case "H": hero.moveHero(0, -1, count, gameStatus, setGameStatus); break;