player moving

This commit is contained in:
2025-10-19 03:07:52 +01:00
parent 6108640267
commit 544581401b
3 changed files with 26 additions and 9 deletions
+8 -6
View File
@@ -1,4 +1,4 @@
import type { GameStatus, KeyLogEntry } from "../types";
import type { GameStatus, KeyLogEntry, UseHeroType } from "../types";
import { useEffect, useRef, useState } from "react";
export type CursorBindingContext = {
@@ -20,7 +20,9 @@ export type CursorBindingContext = {
type UseKeyBindingsParams = {
cursor: CursorBindingContext;
hero: UseHeroType;
gameStatus: GameStatus;
setGameStatus: (status: GameStatus) => void;
};
const RELEVANT_KEYS = new Set([
@@ -28,7 +30,7 @@ const RELEVANT_KEYS = new Set([
"1", "2", "3", "4", "5", "6", "7", "8", "9"
]);
export const useKeyBindings = ({ cursor, gameStatus }: UseKeyBindingsParams) => {
export const useKeyBindings = ({ cursor, hero, gameStatus, setGameStatus }: UseKeyBindingsParams) => {
const [keyLog, setKeyLog] = useState<KeyLogEntry[]>([]);
const logRef = useRef<KeyLogEntry[]>([]);
@@ -112,13 +114,13 @@ export const useKeyBindings = ({ cursor, gameStatus }: UseKeyBindingsParams) =>
switch (ev.key) {
case "h":
case "H": cursor.moveLeft(count); break;
case "H": hero.moveHero(0, -1, count, gameStatus, setGameStatus); break;
case "j":
case "J": cursor.moveDown(count); break;
case "J": hero.moveHero(1, 0, count, gameStatus, setGameStatus); break;
case "k":
case "K": cursor.moveUp(count); break;
case "K": hero.moveHero(-1, 0, count, gameStatus, setGameStatus); break;
case "l":
case "L": cursor.moveRight(count); break;
case "L": hero.moveHero(0, 1, count, gameStatus, setGameStatus); break;
default: return;
}
};