Skip to content

Commit 33c6687

Browse files
author
Shawn Toubeau
committed
Fixes wall selecting functionality
1 parent d31a700 commit 33c6687

File tree

12 files changed

+222
-200
lines changed

12 files changed

+222
-200
lines changed

src/actions/mazeActions.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const MAKE_WALL = 'MAKE_WALL';
2+
export const MAKE_EMPTY = 'MAKE_EMPTY';
3+
4+
export const makeWall = (coord: { x: number; y: number }) => {
5+
return {
6+
type: MAKE_WALL,
7+
payload: coord
8+
};
9+
};
10+
11+
export const makeEmpty = (coord: { x: number; y: number }) => {
12+
return {
13+
type: MAKE_EMPTY,
14+
payload: coord
15+
};
16+
};

src/components/Maze/Maze.tsx

+40-40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect } from 'react';
2-
import {Canvas, useThree} from 'react-three-fiber';
2+
import { Canvas, useThree } from 'react-three-fiber';
33
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
44

55
// Components
@@ -10,51 +10,50 @@ import { MazeState } from '../../models/maze';
1010
import { SpaceState } from '../../models/space';
1111

1212
import './Maze.scss';
13-
import { makeWall, makeEmpty } from '../../actions/mazeActions';
1413

1514
const CameraController = () => {
1615
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]);
3733
return null;
3834
};
3935

4036
const populateMaze = (
4137
mazeSize: { x: number; y: number },
4238
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
4541
) => {
46-
console.log(mazeInfo);
4742
let list = [];
4843
let key = 0;
4944

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++) {
5247
key++;
53-
console.log('visited: ' + mazeInfo[j][i].visited);
5448
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 })}
5857
/>
5958
);
6059
}
@@ -63,24 +62,25 @@ const populateMaze = (
6362
return list;
6463
};
6564

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;
6969
}
7070

7171
const Maze: React.FC<Props> = props => {
72-
const { mazeInfo } = props;
72+
const { mazeInfo } = props.maze;
73+
7374
const mazeSize = {
7475
x: mazeInfo[0].length,
7576
y: Object.keys(mazeInfo).length
7677
};
7778

78-
7979
return (
8080
<Canvas
8181
className="Maze"
8282
//uncomment for isomentric mode ;)
83-
// orthographic
83+
// orthographic
8484
// camera = {{
8585
// position: new Vector3(0,0,0),
8686
// left: 100,
@@ -93,9 +93,9 @@ const Maze: React.FC<Props> = props => {
9393
<ambientLight />
9494

9595
{/* 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'} />
9999
</mesh>
100100
{populateMaze(mazeSize, mazeInfo, props.makeWall, props.makeEmpty)}
101101
</Canvas>

src/components/Space/Space.tsx

+45-43
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,87 @@
11
import * as THREE from 'three';
2-
import React, { useRef, useState, MutableRefObject, Suspense, useEffect } from 'react';
2+
import React, {
3+
useRef,
4+
useState,
5+
MutableRefObject,
6+
Suspense,
7+
useEffect
8+
} from 'react';
39
import { useLoader, useFrame, stateContext } from 'react-three-fiber';
410
import { Mesh, Vector3 } from 'three';
511

6-
// Interfaces
7-
import { SpaceState } from '../../models/space';
8-
912
const Wall: React.FC<BoxProps> = props => {
10-
11-
const texture = useLoader(
13+
const texture = useLoader(
1214
THREE.TextureLoader,
1315
`${process.env.PUBLIC_URL}${props.type}.png`
1416
);
1517

16-
return(
18+
return (
1719
<mesh>
1820
<boxBufferGeometry attach="geometry" args={[1, 1, 1]} />
1921
<meshPhongMaterial attach="material" map={texture} />
2022
</mesh>
2123
);
22-
}
24+
};
2325

2426
const Empty: React.FC<BoxProps> = props => {
25-
26-
return(
27-
<mesh position={new Vector3(0,-0.5,0)} rotation={[-(Math.PI/2),0,0]} >
28-
<planeBufferGeometry attach="geometry" args={[1, 1, 1, 1]}/>
29-
<meshPhongMaterial attach="material"color={ props.visited ? props.path ? 'red': 'yellow' : 'blue' }/>
30-
</mesh>
27+
return (
28+
<mesh position={new Vector3(0, -0.5, 0)} rotation={[-(Math.PI / 2), 0, 0]}>
29+
<planeBufferGeometry attach="geometry" args={[1, 1, 1, 1]} />
30+
<meshPhongMaterial
31+
attach="material"
32+
color={props.visited ? (props.path ? 'red' : 'yellow') : 'blue'}
33+
/>
34+
</mesh>
3135
);
32-
}
36+
};
3337

3438
const GenericSpace: React.FC<BoxProps> = props => {
35-
3639
const mesh: MutableRefObject<Mesh | undefined> = useRef();
3740

3841
if (props.type === 'empty') {
39-
return(
40-
<Empty visited={props.visited} path={props.path}/>
41-
);
42-
}
43-
else{
42+
return <Empty visited={props.visited} path={props.path} />;
43+
} else {
4444
return (
45-
<Suspense fallback="none">
46-
<Wall type={props.type}/>
45+
<Suspense fallback="none">
46+
<Wall type={props.type} />
4747
</Suspense>
4848
);
4949
}
5050
};
5151

5252
const Space: React.FC<SpaceProps> = props => {
53-
5453
const [hovered, setHover] = useState(false);
5554

5655
const spaceClicked = () => {
57-
if (props.type === "wall") {
58-
console.log('wall space clicked');
59-
props.onSetEmpty()
56+
if (props.type === 'wall') {
57+
props.onSetEmpty();
6058
}
61-
if (props.type === "empty") {
62-
console.log('empty space clicked');
63-
props.onSetWall()
59+
if (props.type === 'empty') {
60+
props.onSetWall();
6461
}
65-
6662
};
67-
63+
6864
return (
6965
<mesh
7066
position={props.position}
7167
onClick={e => spaceClicked()}
7268
onPointerOver={e => setHover(true)}
7369
onPointerOut={e => setHover(false)}
7470
>
75-
<GenericSpace type={props.type} visited={props.visited} path={props.path}/>
76-
{
77-
hovered &&
78-
<mesh>
79-
<boxBufferGeometry attach="geometry" args={[1.0001, 1.0001, 1.0001]} />
80-
<meshPhongMaterial attach="material" wireframe={true}/>
81-
</mesh>
82-
}
71+
<GenericSpace
72+
type={props.type}
73+
visited={props.visited}
74+
path={props.path}
75+
/>
76+
{hovered && (
77+
<mesh>
78+
<boxBufferGeometry
79+
attach="geometry"
80+
args={[1.0001, 1.0001, 1.0001]}
81+
/>
82+
<meshPhongMaterial attach="material" wireframe={true} />
83+
</mesh>
84+
)}
8385
</mesh>
8486
);
8587
};
@@ -91,9 +93,9 @@ export interface BoxProps {
9193
path?: Boolean;
9294
}
9395

94-
export interface SpaceProps extends BoxProps{
95-
onSetWall: () => void,
96-
onSetEmpty: () => void
96+
export interface SpaceProps extends BoxProps {
97+
onSetWall: () => void;
98+
onSetEmpty: () => void;
9799
}
98100

99101
export default Space;
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { connect } from 'react-redux';
22
import { bindActionCreators } from 'redux';
3-
import {makeWall, makeEmpty} from '../../actions/mazeActions';
4-
import {Dispatch} from 'redux';
5-
import Grid from '../../components/Maze/Maze';
3+
import { makeWall, makeEmpty } from '../../actions/mazeActions';
4+
import { Dispatch } from 'redux';
5+
import Maze from '../../components/Maze/Maze';
66

77
const mapStateToProps = (state: any) => ({
8-
mazeInfo: state.maze.mazeInfo
8+
maze: state.maze
99
});
1010

1111
const mapDispatchToProps = (dispatch: Dispatch) => {
1212
return bindActionCreators({ makeWall, makeEmpty }, dispatch);
1313
};
1414

15-
export default connect(mapStateToProps, mapDispatchToProps) (Grid);
15+
export default connect(mapStateToProps, mapDispatchToProps)(Maze);

src/index.tsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import * as serviceWorker from "./serviceWorker";
2-
import { Provider } from "react-redux";
3-
import ReactDOM from "react-dom";
4-
import * as React from "react";
1+
import * as serviceWorker from './serviceWorker';
2+
import { Provider } from 'react-redux';
3+
import ReactDOM from 'react-dom';
4+
import * as React from 'react';
55

6-
import { configureStore } from "./store";
7-
import "./assets/sass/main.scss";
8-
import App from "./views";
6+
import { configureStore } from './store';
7+
import './assets/sass/main.scss';
8+
import App from './views';
99

1010
export const store = configureStore();
1111

1212
ReactDOM.render(
1313
<Provider store={store}>
1414
<App />
1515
</Provider>,
16-
document.querySelector("#root")
16+
document.querySelector('#root')
1717
);
1818

1919
serviceWorker.register();

src/models/maze/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ export interface MazeState {
44
mazeInfo: {
55
[key: number]: SpaceState[];
66
};
7+
clearMaze: {
8+
[key: number]: SpaceState[];
9+
};
710
}

0 commit comments

Comments
 (0)