33import * as React from 'react' ;
44import { useEffect , useRef , useMemo } from 'react' ;
55import * 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_';
2727const 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+
3050function 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