Skip to content

Commit 8474eca

Browse files
committed
Board maze switch functioning correctly, but maze not correctly updating board. Might be adding cells.
1 parent 9610532 commit 8474eca

File tree

5 files changed

+200
-141
lines changed

5 files changed

+200
-141
lines changed

src/components/Algorithms/Display.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ import {Strategy} from '../Strategy/Strategy';
1818
*/
1919
export default function Display() {
2020
const [layoutType, setLayout] = useState('standard');
21-
const [mazeType, setMazeType] = useState("none");
21+
const [mazeType, setMazeType] = useState("_none_");
2222
const [selectedPoint, setSelectedPoint] = useState(null);
2323
const [selectedGoalPoint, setSelectedGoalPoint] = useState(null);
2424
const [selectedStartPoint, setSelectedStartPoint] = useState(null);
2525
// const board = new Array(10000).fill(0).map(
2626
const board = new Array(3844).fill(0).map(
27-
// (i, id, type: String = '_floor_', visited: Boolean = false, x: Number, y: Number ) => ({id, type, visited}));
28-
// (i, id, type: String = '_floor_', visited: Boolean = false, x: Number, y: Number ) => ({x, y} ({id, type, visited})));
29-
(i, id, type: String = '_floor_', visited: Boolean = false, x: Number, y: Number ) => ({x, y}));
27+
// (i, id, type: string = '_floor_', visited: Boolean = false, x: Number, y: Number ) => ({id, type, visited}));
28+
// (i, id, type: string = '_floor_', visited: Boolean = false, x: Number, y: Number ) => ({x, y} ({id, type, visited})));
29+
(i, id: number, type: String = '_wall_', sourceType: String = '_wall_', targetType: String = '_wall_', visited: Boolean = false, x: number, y: number ) => ({x, y, id, type, visited, sourceType, targetType}));
3030

3131
const boardRef = useRef(); // Mutable(Persistant) board reference object.
3232

@@ -87,18 +87,18 @@ export default function Display() {
8787
<div className = "maze-toggle-group" >
8888
<button
8989
onClick = {() => {
90-
if (mazeType === 'none') {
90+
if (mazeType === '_none_') {
9191
setMazeType('_BinaryTree_');
9292
}
9393
}}
94-
className = {`maze-toggle ${mazeType !== 'none' ? 'active' : undefined}`}
94+
className = {`maze-toggle ${mazeType !== '_none_' ? 'active' : undefined}`}
9595
>
9696
ON
9797
</button>
9898

9999
<button
100-
onClick = {() => setMazeType('none')}
101-
className = {`maze-toggle ${mazeType === 'none' ? 'active' : undefined}`}
100+
onClick = {() => setMazeType('_none_')}
101+
className = {`maze-toggle ${mazeType === '_none_' ? 'active' : undefined}`}
102102
>
103103
OFF
104104
</button>

src/components/Algorithms/Maze/Generation/BinaryTreeCreation.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import {_FindCellVisitState, _FindCellTypeState, _FindCell} from './Tools';
44

55

66

7-
export function BinaryTreeCreation(board) {
8-
for (var i = 0; i < board.length; i++) {
7+
export function BinaryTreeCreation({board}) {
8+
for (var i = 0; i < board.i.length; i++) {
99
let direction = Math.random() * 4;
1010
// console.log(board[i]);
1111

1212
// console.log(direction);
1313
// console.log(directions[direction].x);
1414
// console.log(board[i].x + directions[direction].x);
15-
let xcord = board[i].x + directions[direction].x;
16-
let ycord = board[i].y + directions[direction].y;
15+
let xcord = board[i].i.x + directions[direction].x;
16+
let ycord = board[i].i.y + directions[direction].y;
1717
const cell = board.find(item => Math.floor(item.x) === Math.floor(xcord) && Math.floor(item.y) === Math.floor(ycord));
1818
// console.log(cell);
1919

src/components/Algorithms/Maze/Generation/Tools.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ export const _FindCellTypeState = (board, x, y) => {
2121

2222
export function _FindCell(board, x, y) {
2323
for (let i = 0; i < board.length; i++) {
24-
console.log(Math.floor(board[i].x), Math.floor(x));
24+
// console.log(board[i].x, x, board[i].y, y);
25+
26+
if (board[i].x === x && board[i].y === y) {
27+
console.log(board[i].x, x, board[i].y, y);
2528

26-
if (Math.floor(board[i].x) === Math.floor(x) && Math.floor(board[i].y) === Math.floor(y)) {
2729
return board[i];
2830
}
2931
}

src/components/BoardCanvas/Cells.js

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ function updateInstancedMeshMatrices({mesh, board}) {
5252
return;
5353

5454
}
55+
5556
// transform mesh to world space
5657
for (let i = 0; i < board.length; ++i) {
5758
const {x, y, z} = board[i];
5859
tempOBJ.position.set(x, y, z);
5960
tempOBJ.rotation.set(0.5 * Math.PI, 0, 0); // Look at the origin
61+
tempCOLOR.set(getColor(board[i].type));
6062
tempOBJ.updateMatrix();
61-
mesh.setMatrixAt(i, tempOBJ.matrix);
62-
63+
mesh.setMatrixAt(i, tempOBJ.matrix, tempCOLOR);
6364
}
6465
mesh.instanceMatrix.needsUpdate = true;
6566

@@ -68,7 +69,7 @@ function updateInstancedMeshMatrices({mesh, board}) {
6869

6970
const tempCOLOR = new THREE.Color();
7071

71-
const usePointColorsHook = ({board, selectedPoint}) => {
72+
const usePointColorsHook = ({board}) => {
7273
const numPoints = board.length;
7374
const colorAttrib = useRef();
7475
const colorArray = useMemo(() => new Float32Array(numPoints * 3), [
@@ -100,7 +101,7 @@ const usePointColorsHook = ({board, selectedPoint}) => {
100101
tempCOLOR.toArray(colorArray, i * 3);
101102
}
102103
colorAttrib.current.needsUpdate = true;
103-
}, [board, selectedPoint, colorArray]);
104+
}, [board, colorArray]);
104105

105106
return {colorAttrib, colorArray};
106107
};
@@ -139,6 +140,7 @@ const _mouseClickHook = ({board, selectedPoint, onSelectPoint/* , useDrag */})
139140
}
140141

141142
const point = board[instanceId];
143+
console.log(board[instanceId]);
142144
console.log('Point clicked: ', point);
143145
// toggle the point
144146
if (point === selectedPoint) {
@@ -164,40 +166,6 @@ const Cells = ({board, layoutType, mazeType, selectedPoint, onSelectPoint /*, u
164166
board[1].type = '_goal_';
165167

166168

167-
/*
168-
* Update the mesh matrixes based on the board state
169-
* with the goal of constructing a solvable maze.
170-
*/
171-
// useEffect(() => {
172-
//
173-
// })
174-
175-
176-
/*
177-
* Recursive backtracking algorithm to generate a solvable maze.
178-
*/
179-
const backtrack = (board, start, goal) => {
180-
let current = start;
181-
let path = [];
182-
while (current !== goal) {
183-
path.push(current);
184-
current = board[current.parent];
185-
}
186-
path.push(current);
187-
return path;
188-
};
189-
190-
191-
192-
193-
const {generationInProgress} = useGeneratedMazeHook({
194-
board,
195-
mazeType,
196-
onFrame: () => {
197-
updateInstancedMeshMatrices({mesh: meshRef.current, board});
198-
},
199-
});
200-
201169
const {animationInProgress} = useAnimationHook({
202170
board,
203171
layoutType,
@@ -209,7 +177,7 @@ const Cells = ({board, layoutType, mazeType, selectedPoint, onSelectPoint /*, u
209177

210178
useEffect(() => {
211179
updateInstancedMeshMatrices({mesh: meshRef.current, board});
212-
}, [board, layoutType]);
180+
}, [board, layoutType, mazeType]);
213181

214182
const {getClickTarget, setDownPointerCoord} = _mouseClickHook({
215183
board,
@@ -219,7 +187,7 @@ const Cells = ({board, layoutType, mazeType, selectedPoint, onSelectPoint /*, u
219187
});
220188

221189

222-
const {colorAttrib, colorArray} = usePointColorsHook({board, selectedPoint});
190+
const {colorAttrib, colorArray} = usePointColorsHook({board});
223191

224192

225193
return (
@@ -231,14 +199,14 @@ const Cells = ({board, layoutType, mazeType, selectedPoint, onSelectPoint /*, u
231199
onClick = {getClickTarget}
232200
onPointerDown = {setDownPointerCoord}
233201
>
234-
<boxBufferGeometry attach = "geometry" args = {[1, 0.5, 1, 18]}>
202+
<boxBufferGeometry attach = "geometry" args = {[0.6, 0.3, 0.6, 18]}>
235203
<instancedBufferAttribute
236204
ref = {colorAttrib}
237205
attachObject = {['attributes', 'color']}
238206
args = {[colorArray, 3]}
239207
/>
240208
</boxBufferGeometry>
241-
<boxBufferGeometry
209+
{/* <boxBufferGeometry
242210
attach = "geometry"
243211
args = {[.9, .4, 0.9, 18]}
244212
color = "black"
@@ -247,13 +215,15 @@ const Cells = ({board, layoutType, mazeType, selectedPoint, onSelectPoint /*, u
247215
attachObject = {['attributes', 'color']}
248216
args = {[colorArray, 3]}
249217
/>
250-
</boxBufferGeometry>
218+
</boxBufferGeometry> */}
251219
<meshStandardMaterial
252220
attach = "material"
253221
vertexColors = {THREE.VertexColors}
254222
/>
255223
</instancedMesh>
256-
224+
{/* {generationInProgress && <a.group */}
225+
{/* position = {generationInProgress.interpolate((x, y, z) => [x, y, z])} */}
226+
{/* />} */}
257227
{selectedPoint && (
258228
<a.group
259229
position = {animationInProgress.interpolate(() => [

0 commit comments

Comments
 (0)