proper anchor invalidation
This commit is contained in:
@@ -26,35 +26,43 @@ export function useMazeNavigation(maze: MazeCell[][]): MazeNavigator {
|
||||
if (direction === 'left' || direction === 'right') {
|
||||
const step = direction === 'left' ? -1 : 1;
|
||||
let cc = c;
|
||||
let lastValidPos = { row: r, col: c };
|
||||
|
||||
while (true) {
|
||||
const next = cc + step;
|
||||
if (next < 0 || next >= colsCount) break;
|
||||
if (maze[r][next].includes('wall')) break;
|
||||
if (!maze[r][next].includes('wall')) {
|
||||
lastValidPos = { row: r, col: next };
|
||||
if (next < 0 || next >= colsCount) return null;
|
||||
if (maze[r][next].includes('wall')) {
|
||||
const wallIdx = next;
|
||||
const expectedWallIdx = direction === 'left' ? 0 : colsCount - 1;
|
||||
const expectedCellIdx = direction === 'left' ? 1 : colsCount - 2;
|
||||
|
||||
if (wallIdx !== expectedWallIdx) return null;
|
||||
if (cc !== expectedCellIdx) return null;
|
||||
if (cc === c) return null;
|
||||
|
||||
return { row: r, col: cc };
|
||||
}
|
||||
cc = next;
|
||||
}
|
||||
|
||||
return lastValidPos.col !== c ? lastValidPos : null;
|
||||
} else {
|
||||
const step = direction === 'top' ? -1 : 1;
|
||||
let rr = r;
|
||||
let lastValidPos = { row: r, col: c };
|
||||
|
||||
while (true) {
|
||||
const next = rr + step;
|
||||
if (next < 0 || next >= rowsCount) break;
|
||||
if (maze[next][c].includes('wall')) break;
|
||||
if (!maze[next][c].includes('wall')) {
|
||||
lastValidPos = { row: next, col: c };
|
||||
if (next < 0 || next >= rowsCount) return null;
|
||||
if (maze[next][c].includes('wall')) {
|
||||
const wallIdx = next;
|
||||
const expectedWallIdx = direction === 'top' ? 0 : rowsCount - 1;
|
||||
const expectedCellIdx = direction === 'top' ? 1 : rowsCount - 2;
|
||||
|
||||
if (wallIdx !== expectedWallIdx) return null;
|
||||
if (rr !== expectedCellIdx) return null;
|
||||
if (rr === r) return null;
|
||||
|
||||
return { row: rr, col: c };
|
||||
}
|
||||
rr = next;
|
||||
}
|
||||
|
||||
return lastValidPos.row !== r ? lastValidPos : null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,25 @@ import { useGameKeys } from './useGameKeys';
|
||||
import { useMazeNavigation } from '../useBoard';
|
||||
import { useVimMotions } from './useVimMotions';
|
||||
|
||||
function validateAnchorMove(direction: 'start' | 'end' | 'top' | 'bottom', currentPos: Coord, board: BoardManager): boolean {
|
||||
const navDirection = direction === 'start' ? 'left' :
|
||||
direction === 'end' ? 'right' :
|
||||
direction === 'top' ? 'top' : 'bottom';
|
||||
|
||||
const navigator = useMazeNavigation(board.mazeRef.current);
|
||||
const target = navigator.findAnchorTarget(currentPos, navDirection);
|
||||
|
||||
if (!target) return false;
|
||||
|
||||
const dr = target.row > currentPos.row ? 1 :
|
||||
target.row < currentPos.row ? -1 : 0;
|
||||
const dc = target.col > currentPos.col ? 1 :
|
||||
target.col < currentPos.col ? -1 : 0;
|
||||
const steps = Math.abs(target.row - currentPos.row) + Math.abs(target.col - currentPos.col);
|
||||
|
||||
return navigator.validatePath(currentPos, { row: dr, col: dc }, steps);
|
||||
}
|
||||
|
||||
export function useCursor(board: BoardManager, gameStatusManager: GameStatusManager) {
|
||||
const position = useRef<Coord>({ row: 0, col: 0 })
|
||||
const mode = useRef<CursorMode>('normal')
|
||||
@@ -66,10 +85,7 @@ export function useCursor(board: BoardManager, gameStatusManager: GameStatusMana
|
||||
target === 'top' ? 'top' : 'bottom';
|
||||
|
||||
const anchorTarget = navigator.findAnchorTarget(currentPos, direction);
|
||||
if (!anchorTarget) {
|
||||
window.dispatchEvent(new Event("maze-invalid"));
|
||||
return;
|
||||
}
|
||||
if (!anchorTarget) return;
|
||||
|
||||
const dr = anchorTarget.row > currentPos.row ? 1 :
|
||||
anchorTarget.row < currentPos.row ? -1 : 0;
|
||||
@@ -87,18 +103,34 @@ export function useCursor(board: BoardManager, gameStatusManager: GameStatusMana
|
||||
}
|
||||
|
||||
function moveToStart(): void {
|
||||
if (!validateAnchorMove('start', position.current, board)) {
|
||||
window.dispatchEvent(new Event("maze-invalid"));
|
||||
return;
|
||||
}
|
||||
moveToAnchor('start');
|
||||
}
|
||||
|
||||
function moveToEnd(): void {
|
||||
if (!validateAnchorMove('end', position.current, board)) {
|
||||
window.dispatchEvent(new Event("maze-invalid"));
|
||||
return;
|
||||
}
|
||||
moveToAnchor('end');
|
||||
}
|
||||
|
||||
function moveToTop(): void {
|
||||
if (!validateAnchorMove('top', position.current, board)) {
|
||||
window.dispatchEvent(new Event("maze-invalid"));
|
||||
return;
|
||||
}
|
||||
moveToAnchor('top');
|
||||
}
|
||||
|
||||
function moveToBottom(): void {
|
||||
if (!validateAnchorMove('bottom', position.current, board)) {
|
||||
window.dispatchEvent(new Event("maze-invalid"));
|
||||
return;
|
||||
}
|
||||
moveToAnchor('bottom');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user