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