Skip to content

Commit d31a700

Browse files
committed
thanks for the help shawn :)
1 parent 93f2fba commit d31a700

File tree

5 files changed

+71
-7
lines changed

5 files changed

+71
-7
lines changed

src/actions/navbarActions.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DropdownProps, ButtonProps } from "semantic-ui-react";
2+
import { emptyStatement } from "@babel/types";
23

34
export const CHANGE_ALGO = "CHANGE_ALGO";
45
export const START_VISUALIZATION = "START_VISUALIZATION";
@@ -55,3 +56,5 @@ export const handleClearGrid = (
5556
payload: null
5657
};
5758
};
59+
60+

src/components/Maze/Maze.tsx

+15-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { MazeState } from '../../models/maze';
1010
import { SpaceState } from '../../models/space';
1111

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

1415
const CameraController = () => {
1516
const { camera, gl } = useThree();
@@ -38,7 +39,9 @@ const CameraController = () => {
3839

3940
const populateMaze = (
4041
mazeSize: { x: number; y: number },
41-
mazeInfo: { [key: number]: SpaceState[] }
42+
mazeInfo: { [key: number]: SpaceState[] },
43+
makeWall: (data: {x: number; y: number}, type: string) => void,
44+
makeEmpty: (data: {x: number; y: number}, type: string) => void
4245
) => {
4346
console.log(mazeInfo);
4447
let list = [];
@@ -49,15 +52,23 @@ const populateMaze = (
4952
key++;
5053
console.log('visited: ' + mazeInfo[j][i].visited);
5154
list.push(
52-
<Space type={mazeInfo[j][i].type} visited={mazeInfo[j][i].visited} path={mazeInfo[i][j].path} position={[i, 0, j]} key={key} />
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" )}
58+
/>
5359
);
5460
}
5561
}
5662

5763
return list;
5864
};
5965

60-
const Maze: React.FC<MazeState> = props => {
66+
interface Props extends MazeState {
67+
makeWall: any;
68+
makeEmpty: any;
69+
}
70+
71+
const Maze: React.FC<Props> = props => {
6172
const { mazeInfo } = props;
6273
const mazeSize = {
6374
x: mazeInfo[0].length,
@@ -86,7 +97,7 @@ const Maze: React.FC<MazeState> = props => {
8697
<planeBufferGeometry attach="geometry" args={[100, 100, 100, 100]}/>
8798
<meshPhongMaterial attach="material" wireframe={true} color={'grey'}/>
8899
</mesh>
89-
{populateMaze(mazeSize, mazeInfo)}
100+
{populateMaze(mazeSize, mazeInfo, props.makeWall, props.makeEmpty)}
90101
</Canvas>
91102
);
92103
};

src/components/Space/Space.tsx

+15-2
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,20 @@ const GenericSpace: React.FC<BoxProps> = props => {
4949
}
5050
};
5151

52-
const Space: React.FC<BoxProps> = props => {
52+
const Space: React.FC<SpaceProps> = props => {
5353

5454
const [hovered, setHover] = useState(false);
5555

5656
const spaceClicked = () => {
57-
console.log('space clicked');
57+
if (props.type === "wall") {
58+
console.log('wall space clicked');
59+
props.onSetEmpty()
60+
}
61+
if (props.type === "empty") {
62+
console.log('empty space clicked');
63+
props.onSetWall()
64+
}
65+
5866
};
5967

6068
return (
@@ -83,4 +91,9 @@ export interface BoxProps {
8391
path?: Boolean;
8492
}
8593

94+
export interface SpaceProps extends BoxProps{
95+
onSetWall: () => void,
96+
onSetEmpty: () => void
97+
}
98+
8699
export default Space;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import { connect } from 'react-redux';
2+
import { bindActionCreators } from 'redux';
3+
import {makeWall, makeEmpty} from '../../actions/mazeActions';
4+
import {Dispatch} from 'redux';
25
import Grid from '../../components/Maze/Maze';
36

47
const mapStateToProps = (state: any) => ({
58
mazeInfo: state.maze.mazeInfo
69
});
710

8-
export default connect(mapStateToProps, null)(Grid as any);
11+
const mapDispatchToProps = (dispatch: Dispatch) => {
12+
return bindActionCreators({ makeWall, makeEmpty }, dispatch);
13+
};
14+
15+
export default connect(mapStateToProps, mapDispatchToProps) (Grid);

src/reducers/mazeReducer.ts

+30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
import { MazeState } from "../models/maze";
22
import { initialState, clearMaze } from "../models/maze/initialState";
33
import { CLEAR_GRID } from "../actions/navbarActions";
4+
import {MAKE_EMPTY, MAKE_WALL} from "../actions/mazeActions"
5+
6+
export interface SpaceState {
7+
path: Boolean;
8+
type: string;
9+
visited: Boolean;
10+
}
11+
12+
13+
const changeSpaceType = (
14+
mazeInfo: {
15+
[key: number]: SpaceState[];
16+
},
17+
data: { data: {x:number, y:number},type: string}
18+
) =>{
19+
console.log(data)
20+
let copy = mazeInfo;
21+
copy[data.data.x][data.data.y].type = data.type;
22+
return copy;
23+
}
424

525
export const mazeReducer = (
626
state: MazeState = initialState,
@@ -12,6 +32,16 @@ export const mazeReducer = (
1232
...state,
1333
mazeInfo: clearMaze
1434
};
35+
case MAKE_WALL:
36+
return{
37+
...state,
38+
mazeInfo: changeSpaceType(state.mazeInfo , payload )
39+
};
40+
case MAKE_EMPTY:
41+
return{
42+
...state,
43+
mazeInfo: changeSpaceType(state.mazeInfo , payload )
44+
};
1545
default:
1646
return state;
1747
}

0 commit comments

Comments
 (0)