Skip to content

Commit d8d5b42

Browse files
committed
Maze finally generating, but refreshes with every render
1 parent 7cf837a commit d8d5b42

6 files changed

Lines changed: 174 additions & 93 deletions

File tree

‎src/components/Algorithms/Display.js‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import {Strategy} from '../Strategy/Strategy';
1818
*/
1919
export default function Display() {
2020
const [layoutType, setLayout] = useState('standard');
21-
const [mazeType, setMazeType] = useState("_BinaryTree_");
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 = '_wall_', visited: boolean = false) => ({id, type, visited}));
27+
(i, id, type: string = "_wall_", visited: boolean = false) => ({id, type, visited}));
2828

2929
const boardRef = useRef(); // Mutable(Persistant) board reference object.
3030

@@ -66,14 +66,14 @@ export default function Display() {
6666
onClick = {() => setLayout('standard')}
6767
className = {layoutType === 'standard' ? 'active' : undefined}
6868
>
69-
Grid
70-
</button>
71-
<button
72-
onClick = {() => setLayout('circular')}
73-
className = {layoutType === 'circular' ? 'active' : undefined}
74-
>
75-
Circular
69+
Standard Grid
7670
</button>
71+
{/* <button */}
72+
{/* onClick = {() => setLayout('circular')} */}
73+
{/* className = {layoutType === 'circular' ? 'active' : undefined} */}
74+
{/* > */}
75+
{/* Circular */}
76+
{/* </button> */}
7777

7878
<button className = "reset-button" onClick = {handleResetCamera}>
7979
View Reset
@@ -89,7 +89,7 @@ export default function Display() {
8989
setMazeType('_BinaryTree_');
9090
}
9191
}}
92-
className = {`maze-toggle + ${mazeType !== 'none' ? 'active' : undefined}`}
92+
className = {`maze-toggle ${mazeType !== 'none' ? 'active' : undefined}`}
9393
>
9494
ON
9595
</button>

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import {directions} from './Directions';
22
import {_FindCellVisitState, _FindCellTypeState, _FindCell} from './Tools';
3-
import {useEffect} from 'react';
43

54

65

76

8-
export function useBinaryTreeCreation({board, layoutType}) {
9-
useEffect(() => {
7+
export function BinaryTreeCreation(board) {
108
for (var i = 0; i < board.length; i++) {
119
let direction = Math.floor(Math.random() * 4);
1210
// console.log(board[i]);
@@ -17,19 +15,17 @@ export function useBinaryTreeCreation({board, layoutType}) {
1715
let xcord = board[i].x + directions[direction].x;
1816
let ycord = board[i].y + directions[direction].y;
1917
const cell = board.find(item => Math.floor(item.x) === Math.floor(xcord) && Math.floor(item.y) === Math.floor(ycord));
20-
18+
// console.log(cell);
2119

2220
if (cell) {
2321
if (!cell.visited) {
2422
// console.log(cell);
2523
board[cell.id].visited = true;
26-
board[cell.id].type = '_wall_';
24+
board[cell.id].type = "_wall_";
2725
}
2826
}
2927
}
3028
console.log('BinaryTreeCreation: ' + board.length);
31-
return board;
32-
}, [board, layoutType === 'binaryTree' || layoutType === 'standard']);
3329
}
3430

3531

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
export const _FindCellVisitState = (board, x, y) => {
55
for (let i = 0; i < board.length; i++) {
66
if (Math.floor(board[i].x) === Math.floor(x) && Math.floor(board[i].y) === Math.floor(y)) {
7-
board[i].type = '_test_';
87
return board[i].visited;
98
}
109
}
@@ -14,7 +13,6 @@ export const _FindCellVisitState = (board, x, y) => {
1413
export const _FindCellTypeState = (board, x, y) => {
1514
for (let i = 0; i < board.length; i++) {
1615
if (Math.floor(board[i].x) === Math.floor(x) && Math.floor(board[i].y) === Math.floor(y)) {
17-
board[i].type = '_test_';
1816
return board[i].visited;
1917
}
2018
}
@@ -26,7 +24,6 @@ export function _FindCell(board, x, y) {
2624
console.log(Math.floor(board[i].x), Math.floor(x));
2725

2826
if (Math.floor(board[i].x) === Math.floor(x) && Math.floor(board[i].y) === Math.floor(y)) {
29-
board[i].type = '_test_';
3027
return board[i];
3128
}
3229
}

‎src/components/BoardCanvas/Board.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Cell from './Cells';
55
import {useImperativeHandle, useRef, forwardRef} from 'react';
66
import {TrackballControls} from 'three/examples/jsm/controls/TrackballControls';
77
import OrbitControls from './OrbitControls';
8-
import {useGenerateMaze} from './layouts';
98
// import create from 'zustand';
109

1110

@@ -33,7 +32,7 @@ export const Board = ({board, layoutType, selectedPoint, onSelectPoint, mazeType
3332
{(layoutType === 'standard') ?
3433
<boxBufferGeometry attach = "geometry" args = {[110, 110, -10]}/>
3534
:
36-
<boxBufferGeometry attach = "geometry" args = {[180, 180, -10]}/>
35+
<boxBufferGeometry attach = "geometry" args = {[150, 150, -10]}/>
3736
}
3837
<meshBasicMaterial attach = "material" color = "black"/>
3938
</mesh>

‎src/components/BoardCanvas/Cells.js‎

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import * as React from 'react';
44
import {useEffect, useRef, useMemo} from 'react';
55
import * as THREE from 'three';
6-
import {a} from 'react-spring/three';
7-
import {useAnimationHook, useGenerateMaze} from './layouts';
6+
import {a} from 'react-spring/three';
7+
import {useAnimationHook, useGeneratedMazeHook} from './layouts';
88

99

1010

@@ -27,6 +27,26 @@ const TEST_TYPE = '_test_';
2727
const tempOBJ = new THREE.Object3D();
2828

2929

30+
function getColor(type) {
31+
switch (type) {
32+
case FLOOR_TYPE:
33+
return FLOOR_COLOR;
34+
case WALL_TYPE:
35+
return WALL_COLOR;
36+
case PATH_TYPE:
37+
return PATH_COLOR;
38+
case GOAL_TYPE:
39+
return GOAL_COLOR;
40+
case START_TYPE:
41+
return START_COLOR;
42+
case TEST_TYPE:
43+
return TEST_COLOR;
44+
default:
45+
return '#ffffff';
46+
}
47+
}
48+
49+
3050
function updateInstancedMeshMatrices({mesh, board}) {
3151
if (!mesh) {
3252
return;
@@ -54,44 +74,42 @@ const usePointColorsHook = ({board, selectedPoint}) => {
5474
const colorArray = useMemo(() => new Float32Array(numPoints * 3), [
5575
numPoints,
5676
]);
57-
useEffect((prev) => {
77+
useEffect(() => {
5878
for (let i = 0; i < board.length; ++i) {
59-
if (board[i].type === WALL_TYPE.toString()) {
60-
tempCOLOR.set(
61-
selectedPoint === board[i] ? WALL_COLOR : FLOOR_COLOR,
62-
);
63-
}
64-
else if (board[i].type === PATH_TYPE.toString()) {
65-
tempCOLOR.set(
66-
selectedPoint === board[i] ? PATH_COLOR : FLOOR_COLOR,
67-
);
68-
}
69-
else if (board[i].type === GOAL_TYPE.toString()) {
70-
tempCOLOR.set(
71-
selectedPoint === board[i] ? GOAL_COLOR : FLOOR_COLOR,
72-
);
73-
}
74-
else if (board[i].type === START_TYPE.toString()) {
75-
tempCOLOR.set(
76-
selectedPoint === board[i] ? START_COLOR : FLOOR_COLOR,
77-
);
78-
}
79-
else if (board[i].type === TEST_TYPE.toString()) {
80-
tempCOLOR.set(
81-
selectedPoint === board[i] ? TEST_COLOR : FLOOR_COLOR,
82-
);
83-
}
84-
else if (board[i].type === FLOOR_TYPE.toString()) {
85-
tempCOLOR.set(
86-
selectedPoint === board[i] ? FLOOR_COLOR : FLOOR_COLOR,
87-
);
88-
tempCOLOR.set(FLOOR_COLOR);
89-
}
90-
else if (board[i].type === null) {
91-
tempCOLOR.set(FLOOR_COLOR);
92-
}
93-
else {
94-
tempCOLOR.set(FLOOR_COLOR);
79+
switch (board[i].type) {
80+
case WALL_TYPE:
81+
tempCOLOR.set(
82+
selectedPoint === board[i] ? WALL_COLOR : FLOOR_COLOR,
83+
);
84+
break;
85+
case PATH_TYPE:
86+
tempCOLOR.set(
87+
selectedPoint === board[i] ? PATH_COLOR : FLOOR_COLOR,
88+
);
89+
break;
90+
case GOAL_TYPE:
91+
tempCOLOR.set(
92+
selectedPoint === board[i] ? GOAL_COLOR : WALL_COLOR,
93+
);
94+
break;
95+
case START_TYPE:
96+
tempCOLOR.set(
97+
selectedPoint === board[i] ? START_COLOR : FLOOR_COLOR,
98+
);
99+
break;
100+
case TEST_TYPE:
101+
tempCOLOR.set(
102+
selectedPoint === board[i] ? TEST_COLOR : FLOOR_COLOR,
103+
);
104+
break;
105+
case FLOOR_TYPE:
106+
tempCOLOR.set(
107+
selectedPoint === board[i] ? FLOOR_COLOR : FLOOR_COLOR,
108+
);
109+
break;
110+
default:
111+
tempCOLOR.set(WALL_COLOR);
112+
break;
95113
}
96114
tempCOLOR.toArray(colorArray, i * 3);
97115
}
@@ -145,12 +163,6 @@ const _mouseClickHook = ({board, selectedPoint, onSelectPoint/* , useDrag */})
145163
}
146164
};
147165

148-
// const onMouseMove = (e) => {
149-
// const dx = e.clientX - originalPosition.clientX
150-
// const dy = e.clientY - originalPosition.clientY
151-
// onMouseMoveHandler.current(e, { dx, dy })
152-
// }
153-
154166
return {setDownPointerCoord, getClickTarget};
155167
};
156168

@@ -170,9 +182,9 @@ const Cells = ({board, layoutType, mazeType, selectedPoint, onSelectPoint /*, u
170182
* Update the mesh matrixes based on the board state
171183
* with the goal of constructing a solvable maze.
172184
*/
173-
useEffect(() => {
174-
175-
})
185+
// useEffect(() => {
186+
//
187+
// })
176188

177189

178190
/*
@@ -190,9 +202,20 @@ const Cells = ({board, layoutType, mazeType, selectedPoint, onSelectPoint /*, u
190202
};
191203

192204

205+
206+
207+
const {generationInProgress} = useGeneratedMazeHook({
208+
board,
209+
mazeType,
210+
onFrame: () => {
211+
updateInstancedMeshMatrices({mesh: meshRef.current, board});
212+
},
213+
});
214+
193215
const {animationInProgress} = useAnimationHook({
194216
board,
195217
layoutType,
218+
mazeType,
196219
onFrame: () => {
197220
updateInstancedMeshMatrices({mesh: meshRef.current, board});
198221
},

0 commit comments

Comments
 (0)