player stopped moving

This commit is contained in:
2025-10-19 03:39:37 +01:00
parent ec954ccbb4
commit 81489557c3
3 changed files with 128 additions and 129 deletions
+2
View File
@@ -89,6 +89,7 @@ import { MazeRenderer } from './MazeRenderer';
- **Destructuring**: Use object destructuring for parameters and returns - **Destructuring**: Use object destructuring for parameters and returns
- **Arrow functions**: Prefer arrow functions for consistency - **Arrow functions**: Prefer arrow functions for consistency
- **No useCallback/useMemo**: React 19 optimization makes these hooks unnecessary - **No useCallback/useMemo**: React 19 optimization makes these hooks unnecessary
- **useEffect dependencies**: Only use values, never functions as dependencies
### Error Handling ### Error Handling
- **Early returns**: Guard clauses at function start - **Early returns**: Guard clauses at function start
@@ -178,6 +179,7 @@ export function useGame(options: GameOptions, platformHook?: unknown): GameManag
- Write overly complex implementations - Write overly complex implementations
- Add unnecessary features - Add unnecessary features
- Use `useCallback` or `useMemo` anywhere in the codebase - Use `useCallback` or `useMemo` anywhere in the codebase
- Use functions as useEffect dependencies
### Always DO ### Always DO
- Follow existing code patterns exactly - Follow existing code patterns exactly
+11 -14
View File
@@ -1,4 +1,4 @@
import { useRef, useState, useCallback, useEffect } from "react"; import { useRef, useState, useEffect } from "react";
import type { BoardManager, GameStatus, Coord } from "../types"; import type { BoardManager, GameStatus, Coord } from "../types";
import { useHeroRender } from "./useHeroRender"; import { useHeroRender } from "./useHeroRender";
@@ -8,35 +8,35 @@ export function useHero(board: BoardManager, gameStatus: GameStatus) {
const animatingRef = useRef(false); const animatingRef = useRef(false);
const getCellEl = useCallback((r: number, c: number): HTMLElement | null => { function getCellEl(r: number, c: number): HTMLElement | null {
const container = containerRef.current; const container = containerRef.current;
if (!container) return null; if (!container) return null;
const mazeDiv = container.querySelector("#maze"); const mazeDiv = container.querySelector("#maze");
if (!mazeDiv) return null; if (!mazeDiv) return null;
const selector = `.maze-row > div[data-r="${r}"][data-c="${c}"]`; const selector = `.maze-row > div[data-r="${r}"][data-c="${c}"]`;
return mazeDiv.querySelector(selector) as HTMLElement | null; return mazeDiv.querySelector(selector) as HTMLElement | null;
}, [containerRef]); }
const leaveTrail = useCallback((r: number, c: number) => { function leaveTrail(r: number, c: number) {
const el = getCellEl(r, c); const el = getCellEl(r, c);
if (!el) return; if (!el) return;
el.classList.add("trail"); el.classList.add("trail");
setTimeout(() => el.classList.remove("trail"), 220); setTimeout(() => el.classList.remove("trail"), 220);
}, [getCellEl]); }
const flashInvalid = useCallback(() => { function flashInvalid() {
if (!heroPos) return; if (!heroPos) return;
const el = getCellEl(heroPos.row, heroPos.col); const el = getCellEl(heroPos.row, heroPos.col);
if (!el) return; if (!el) return;
el.classList.add("invalid-move"); el.classList.add("invalid-move");
setTimeout(() => el.classList.remove("invalid-move"), 180); setTimeout(() => el.classList.remove("invalid-move"), 180);
}, [heroPos, getCellEl]); }
useEffect(() => { useEffect(() => {
const onInvalid = () => flashInvalid(); const onInvalid = () => flashInvalid();
window.addEventListener("maze-invalid", onInvalid); window.addEventListener("maze-invalid", onInvalid);
return () => window.removeEventListener("maze-invalid", onInvalid); return () => window.removeEventListener("maze-invalid", onInvalid);
}, [flashInvalid]); }, []);
useEffect(() => { useEffect(() => {
if (gameStatus === 'started' && !heroPos) { if (gameStatus === 'started' && !heroPos) {
@@ -61,14 +61,13 @@ export function useHero(board: BoardManager, gameStatus: GameStatus) {
} }
}, [gameStatus, containerRef]); }, [gameStatus, containerRef]);
const moveHero = useCallback( function moveHero(
(
dr: number, dr: number,
dc: number, dc: number,
steps: number | undefined, steps: number | undefined,
gameStatus: GameStatus, gameStatus: GameStatus,
setGameStatus: (s: GameStatus) => void setGameStatus: (s: GameStatus) => void
) => { ) {
const m = mazeRef.current; const m = mazeRef.current;
if (!heroPos || !m.length) return; if (!heroPos || !m.length) return;
if (gameStatus !== "started" && gameStatus !== "hasKey") return; if (gameStatus !== "started" && gameStatus !== "hasKey") return;
@@ -194,9 +193,7 @@ export function useHero(board: BoardManager, gameStatus: GameStatus) {
} }
setHeroPos({ row: newR, col: newC }); setHeroPos({ row: newR, col: newC });
}, }
[heroPos, mazeRef, containerRef, flashInvalid, leaveTrail]
);
const hero = { const hero = {
heroPos, heroPos,
+1 -1
View File
@@ -112,7 +112,7 @@ export const useKeyBindings = ({ cursor, gameStatus, setGameStatus }: UseKeyBind
window.addEventListener("keydown", handler); window.addEventListener("keydown", handler);
return () => window.removeEventListener("keydown", handler); return () => window.removeEventListener("keydown", handler);
}, [cursor, gameStatus]); }, [gameStatus]);
return { return {
keyLog, keyLog,