1
1
import React , { useEffect } from 'react' ;
2
- import { Canvas , useThree } from 'react-three-fiber' ;
2
+ import { Canvas , useThree } from 'react-three-fiber' ;
3
3
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js' ;
4
4
5
5
// Components
@@ -10,51 +10,50 @@ import { MazeState } from '../../models/maze';
10
10
import { SpaceState } from '../../models/space' ;
11
11
12
12
import './Maze.scss' ;
13
- import { makeWall , makeEmpty } from '../../actions/mazeActions' ;
14
13
15
14
const CameraController = ( ) => {
16
15
const { camera, gl } = useThree ( ) ;
17
- useEffect (
18
- ( ) => {
19
- const controls = new OrbitControls ( camera , gl . domElement ) ;
20
-
21
- controls . minDistance = 5 ;
22
- controls . maxDistance = 30 ;
23
- controls . keyPanSpeed = 10 ;
24
-
25
- //uncomment to limit orbit rotation
26
- // controls.maxAzimuthAngle = (Math.PI / 360) * angle;
27
- // controls.minAzimuthAngle = (Math.PI / 360) * -angle;
28
-
29
- controls . maxPolarAngle = ( Math . PI / 360 ) * 180 ;
30
-
31
- return ( ) => {
32
- controls . dispose ( ) ;
33
- } ;
34
- } ,
35
- [ camera , gl ]
36
- ) ;
16
+ useEffect ( ( ) => {
17
+ const controls = new OrbitControls ( camera , gl . domElement ) ;
18
+
19
+ controls . minDistance = 5 ;
20
+ controls . maxDistance = 30 ;
21
+ controls . keyPanSpeed = 10 ;
22
+
23
+ //uncomment to limit orbit rotation
24
+ // controls.maxAzimuthAngle = (Math.PI / 360) * angle;
25
+ // controls.minAzimuthAngle = (Math.PI / 360) * -angle;
26
+
27
+ controls . maxPolarAngle = ( Math . PI / 360 ) * 180 ;
28
+
29
+ return ( ) => {
30
+ controls . dispose ( ) ;
31
+ } ;
32
+ } , [ camera , gl ] ) ;
37
33
return null ;
38
34
} ;
39
35
40
36
const populateMaze = (
41
37
mazeSize : { x : number ; y : number } ,
42
38
mazeInfo : { [ key : number ] : SpaceState [ ] } ,
43
- makeWall : ( data : { x : number ; y : number } , type : string ) => void ,
44
- makeEmpty : ( data : { x : number ; y : number } , type : string ) => void
39
+ makeWall : ( coord : { x : number ; y : number } ) => void ,
40
+ makeEmpty : ( coord : { x : number ; y : number } ) => void
45
41
) => {
46
- console . log ( mazeInfo ) ;
47
42
let list = [ ] ;
48
43
let key = 0 ;
49
44
50
- for ( let j = 0 ; j < mazeSize . y ; j ++ ) {
51
- for ( let i = 0 ; i < mazeSize . x ; i ++ ) {
45
+ for ( let y = 0 ; y < mazeSize . y ; y ++ ) {
46
+ for ( let x = 0 ; x < mazeSize . x ; x ++ ) {
52
47
key ++ ;
53
- console . log ( 'visited: ' + mazeInfo [ j ] [ i ] . visited ) ;
54
48
list . push (
55
- < Space type = { mazeInfo [ j ] [ i ] . type } visited = { mazeInfo [ j ] [ i ] . visited } path = { mazeInfo [ i ] [ j ] . path } position = { [ i , 0 , j ] } key = { key }
56
- onSetWall = { ( ) => makeWall ( { x :i , y :j } , "empty" ) }
57
- onSetEmpty = { ( ) => makeEmpty ( { x :i , y :j } , "wall" ) }
49
+ < Space
50
+ type = { mazeInfo [ y ] [ x ] . type }
51
+ visited = { mazeInfo [ y ] [ x ] . visited }
52
+ path = { mazeInfo [ y ] [ x ] . path }
53
+ position = { [ x , 0 , y ] }
54
+ key = { key }
55
+ onSetWall = { ( ) => makeWall ( { x, y } ) }
56
+ onSetEmpty = { ( ) => makeEmpty ( { x, y } ) }
58
57
/>
59
58
) ;
60
59
}
@@ -63,24 +62,25 @@ const populateMaze = (
63
62
return list ;
64
63
} ;
65
64
66
- interface Props extends MazeState {
67
- makeWall : any ;
68
- makeEmpty : any ;
65
+ interface Props {
66
+ makeWall : ( coord : { x : number ; y : number } ) => void ;
67
+ makeEmpty : ( coord : { x : number ; y : number } ) => void ;
68
+ maze : MazeState ;
69
69
}
70
70
71
71
const Maze : React . FC < Props > = props => {
72
- const { mazeInfo } = props ;
72
+ const { mazeInfo } = props . maze ;
73
+
73
74
const mazeSize = {
74
75
x : mazeInfo [ 0 ] . length ,
75
76
y : Object . keys ( mazeInfo ) . length
76
77
} ;
77
78
78
-
79
79
return (
80
80
< Canvas
81
81
className = "Maze"
82
82
//uncomment for isomentric mode ;)
83
- // orthographic
83
+ // orthographic
84
84
// camera = {{
85
85
// position: new Vector3(0,0,0),
86
86
// left: 100,
@@ -93,9 +93,9 @@ const Maze: React.FC<Props> = props => {
93
93
< ambientLight />
94
94
95
95
{ /* background grid */ }
96
- < mesh rotation = { [ ( Math . PI / 2 ) , 0 , 0 ] } position = { [ 0.5 , - 0.5 , 0.5 ] } >
97
- < planeBufferGeometry attach = "geometry" args = { [ 100 , 100 , 100 , 100 ] } />
98
- < meshPhongMaterial attach = "material" wireframe = { true } color = { 'grey' } />
96
+ < mesh rotation = { [ Math . PI / 2 , 0 , 0 ] } position = { [ 0.5 , - 0.5 , 0.5 ] } >
97
+ < planeBufferGeometry attach = "geometry" args = { [ 100 , 100 , 100 , 100 ] } />
98
+ < meshPhongMaterial attach = "material" wireframe = { true } color = { 'grey' } />
99
99
</ mesh >
100
100
{ populateMaze ( mazeSize , mazeInfo , props . makeWall , props . makeEmpty ) }
101
101
</ Canvas >
0 commit comments