-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from shibisuriya/feat/replay
feat: added the ability to pause + revamped the grid
- Loading branch information
Showing
12 changed files
with
336 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React, { Fragment, useState } from 'react'; | ||
import { defaultDirections } from './constants'; | ||
import { initialSnakesState, initialFoodState } from './computed'; | ||
import { useDirection, useFood, useTicks, useSnakes, useInput, useSocket } from './hooks'; | ||
import Grid from './Grid'; | ||
import styles from './app.module.css'; | ||
|
||
// function App() { | ||
// const [mounted, setMounted] = useState(true); | ||
// return ( | ||
// <div> | ||
// <div> | ||
// <button onClick={() => setMounted((prev) => !prev)}>Mount / Unmount</button> | ||
// </div> | ||
// <div>{mounted && <Game />}</div> | ||
// </div> | ||
// ); | ||
// } | ||
|
||
function Game(props) { | ||
const { showCellId, isGamePaused } = props; | ||
const [snakeId, setSnakeId] = useState(1); | ||
|
||
// Keep the direction of the snakes inside useRef since we don't | ||
// want to force rerender of the component when the user changes | ||
// the direction. | ||
const { getDirection, onLeft, onRight, onUp, onDown, setDirection } = useDirection(defaultDirections, 1); | ||
|
||
useInput({ snakes, onUp, onDown, onLeft, onRight, snakeId }); | ||
|
||
const getSnakeCells = () => allSnakeCells(); | ||
|
||
const { food, getFood, removeFood, spawnFood, isFood, setFood } = useFood({ initialFoodState, getSnakeCells }); | ||
|
||
const getTracks = () => { | ||
return { addSnakeToTrack, removeSnakeFromTracks, resetSnakeTrack }; | ||
}; | ||
|
||
// Don't keep direction of the snakes inside of useState()... | ||
const { | ||
snakes, | ||
moveForward, | ||
getSnakeCells: allSnakeCells, | ||
getAllSnakeIds, | ||
} = useSnakes({ | ||
initialSnakesState, | ||
getDirection, | ||
getFood, | ||
removeFood, | ||
setDirection, | ||
isFood, | ||
setFood, | ||
getTracks, | ||
}); | ||
|
||
const { addSnakeToTrack, removeSnakeFromTracks, resetSnakeTrack } = useTicks({ | ||
moveForward, | ||
spawnFood, | ||
getAllSnakeIds, | ||
isGamePaused, | ||
}); | ||
|
||
return ( | ||
<div className={styles.game}> | ||
{/* <select value={snakeId} onChange={(e) => setSnakeId(e.target.value)}> | ||
{Object.keys(snakes).map((snakeId, index) => ( | ||
<option value={snakeId} key={index}> | ||
{snakeId} | ||
</option> | ||
))} | ||
</select> */} | ||
<Grid snakes={snakes} food={food} showCellId={showCellId} /> | ||
</div> | ||
); | ||
} | ||
|
||
export default Game; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.