|
| 1 | +// useMazeStore.ts |
| 2 | +import create from 'zustand'; |
| 3 | +import { Cell } from '../algorithms/utils/PathfindingUtils'; |
| 4 | + |
| 5 | +interface MazeState { |
| 6 | + maze: Cell[][]; |
| 7 | + startNode: [number, number]; |
| 8 | + endNode: [number, number]; |
| 9 | + setMaze: (newMaze: Cell[][]) => void; |
| 10 | + setCell: (row: number, col: number, updates: Partial<Cell>) => void; |
| 11 | + generateMaze: (width: number, height: number) => void; |
| 12 | + clearAttribute: (attribute: keyof Cell) => void; |
| 13 | + clearLatestRun: () => void; |
| 14 | + populateOptimalPath: (optimalPath: Cell[] | null) => void; |
| 15 | + setStartPosition: (flatHeight: number, flatWidth: number) => void; |
| 16 | + setEndPosition: (flatHeight: number, flatWidth: number) => void; |
| 17 | + drawCell: (row: number, col: number, value: boolean) => void; |
| 18 | + moveNode: (row: number, col: number, nodeType: 'start' | 'end') => void; |
| 19 | +} |
| 20 | + |
| 21 | +export const useMazeStore = create<MazeState>((set) => ({ |
| 22 | + maze: [], |
| 23 | + startNode: [0, 0], |
| 24 | + endNode: [1,1], |
| 25 | + setMaze: (newMaze) => set({ maze: newMaze }), |
| 26 | + setCell: (row, col, updates) => { |
| 27 | + set((state) => { |
| 28 | + const newMaze = state.maze.map((r, i) => |
| 29 | + i === row ? r.map((c, j) => (j === col ? { ...c, ...updates } : c)) : r |
| 30 | + ); |
| 31 | + return { maze: newMaze }; |
| 32 | + }); |
| 33 | + }, |
| 34 | + generateMaze: (width, height) => { |
| 35 | + let flatHeight = Math.floor((height - (26 + 64)) / 26); |
| 36 | + if (flatHeight % 2 === 0) flatHeight += 1; |
| 37 | + let flatWidth = Math.floor((width - 326) / 26); |
| 38 | + if (flatWidth % 2 === 0) flatWidth += 1; |
| 39 | + |
| 40 | + const newMaze = []; |
| 41 | + for (let i = 0; i < flatHeight; i++) { |
| 42 | + const row = []; |
| 43 | + for (let j = 0; j < flatWidth; j++) { |
| 44 | + row.push(new Cell(i, j)); |
| 45 | + } |
| 46 | + newMaze.push(row); |
| 47 | + } |
| 48 | + set({ maze: newMaze }); |
| 49 | + }, |
| 50 | + clearAttribute: (attribute) => { |
| 51 | + set((state) => ({ |
| 52 | + maze: state.maze.map((row) => |
| 53 | + row.map((cell) => ({ ...cell, [attribute]: false })) |
| 54 | + ), |
| 55 | + })); |
| 56 | + }, |
| 57 | + clearLatestRun: () => { |
| 58 | + set((state) => { |
| 59 | + const clearedMaze = state.maze.map((row) => |
| 60 | + row.map((cell) => ({ ...cell, visited: false, finalPath: false })) |
| 61 | + ); |
| 62 | + return { maze: clearedMaze }; |
| 63 | + }); |
| 64 | + }, |
| 65 | + populateOptimalPath: (optimalPath) => { |
| 66 | + if (!optimalPath) { |
| 67 | + return; |
| 68 | + } |
| 69 | + const delay = 50; // Delay in milliseconds between each update |
| 70 | + const updateCell = (index: number) => { |
| 71 | + if (index >= optimalPath.length) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + set((state) => { |
| 76 | + const newMaze = state.maze.map((row) => |
| 77 | + row.map((cell) => ({ ...cell })) |
| 78 | + ); |
| 79 | + newMaze[optimalPath[index].row][optimalPath[index].col].finalPath = true; |
| 80 | + return { maze: newMaze }; |
| 81 | + }); |
| 82 | + |
| 83 | + // Schedule the next update with delay |
| 84 | + requestAnimationFrame(() => |
| 85 | + setTimeout(() => updateCell(index + 1), delay) |
| 86 | + ); |
| 87 | + }; |
| 88 | + |
| 89 | + updateCell(0); |
| 90 | + }, |
| 91 | + setStartPosition: (flatHeight, flatWidth) => { |
| 92 | + set((state) => { |
| 93 | + const rowIndex = Math.floor(flatHeight / 2); |
| 94 | + const colIndex = Math.floor(flatWidth / 4); |
| 95 | + const newMaze = state.maze.map((row, i) => |
| 96 | + i === rowIndex ? row.map((cell, j) => (j === colIndex ? { ...cell, start: true } : cell)) : row |
| 97 | + ); |
| 98 | + return { maze: newMaze, startNode: [rowIndex, colIndex] }; |
| 99 | + }); |
| 100 | + }, |
| 101 | + setEndPosition: (flatHeight, flatWidth) => { |
| 102 | + set((state) => { |
| 103 | + const rowIndex = Math.floor(flatHeight / 2); |
| 104 | + const colIndex = Math.floor(flatWidth * 3 / 4); |
| 105 | + const newMaze = state.maze.map((row, i) => |
| 106 | + i === rowIndex ? row.map((cell, j) => (j === colIndex ? { ...cell, end: true } : cell)) : row |
| 107 | + ); |
| 108 | + return { maze: newMaze, endNode: [rowIndex, colIndex] }; |
| 109 | + }); |
| 110 | + }, |
| 111 | + drawCell: (row, col, value) => { |
| 112 | + set((state) => { |
| 113 | + const newMaze = state.maze.map((r, i) => |
| 114 | + i === row ? r.map((c, j) => (j === col ? { ...c, wall: value } : c)) : r |
| 115 | + ); |
| 116 | + return { maze: newMaze }; |
| 117 | + }); |
| 118 | + }, |
| 119 | + moveNode: (row, col, nodeType) => { |
| 120 | + set((state) => { |
| 121 | + const newMaze = state.maze.map((r, i) => |
| 122 | + r.map((c) => { |
| 123 | + if (nodeType === 'start' && c.start) { |
| 124 | + return { ...c, start: false }; |
| 125 | + } |
| 126 | + if (nodeType === 'end' && c.end) { |
| 127 | + return { ...c, end: false }; |
| 128 | + } |
| 129 | + if (i === row && c.col === col) { |
| 130 | + return { ...c, [nodeType]: true }; |
| 131 | + } |
| 132 | + return c; |
| 133 | + }) |
| 134 | + ); |
| 135 | + return nodeType === 'start' |
| 136 | + ? { maze: newMaze, startNode: [row, col] } |
| 137 | + : { maze: newMaze, endNode: [row, col] }; |
| 138 | + }); |
| 139 | + }, |
| 140 | +})); |
0 commit comments