diff --git a/src/lib/tetris.js b/src/lib/tetris.js index a54d906..e0dcd5c 100644 --- a/src/lib/tetris.js +++ b/src/lib/tetris.js @@ -80,7 +80,7 @@ function rotatePoints(type, points) { return rotated } -function eliminateLines(tetromino, tetrominoes, setTetrominoes) { +function eliminateLines(tetromino, tetrominoes) { const { y, points } = tetromino const candidates = [] @@ -118,10 +118,6 @@ function eliminateLines(tetromino, tetrominoes, setTetrominoes) { } }) - if (eliminatedLines > 0) { - setTetrominoes([...tetrominoes]) - } - return eliminatedLines } @@ -201,7 +197,7 @@ export function rotateTetromino(tetromino, tetrominoes) { return collised ? tetromino : rotated } -export function moveDownTetromino(tetromino, tetrominoes, onCollise, onGameOver, setTetrominoes) { +export function moveDownTetromino(tetromino, tetrominoes, onCollise, onGameOver) { const moved = { ...tetromino, y: tetromino.y + 1 } const { collised, reachTop } = detectCollision(moved, DOWN, tetrominoes) @@ -210,7 +206,7 @@ export function moveDownTetromino(tetromino, tetrominoes, onCollise, onGameOver, return moved } - const eliminatedLines = eliminateLines(tetromino, tetrominoes, setTetrominoes) + const eliminatedLines = eliminateLines(tetromino, tetrominoes) if (eliminatedLines == 0 && reachTop) { onGameOver() } else { @@ -236,7 +232,7 @@ export function moveRightTetromino(tetromino, tetrominoes) { return collised ? tetromino : moved } -export function fallDownTetromino(tetromino, tetrominoes, onCollise, onGameOver, setTetrominoes) { +export function fallDownTetromino(tetromino, tetrominoes, onCollise, onGameOver) { let result const moved = { ...tetromino } do { @@ -246,7 +242,7 @@ export function fallDownTetromino(tetromino, tetrominoes, onCollise, onGameOver, tetromino.y = moved.y - 1 - const eliminatedLines = eliminateLines(tetromino, tetrominoes, setTetrominoes) + const eliminatedLines = eliminateLines(tetromino, tetrominoes) if (eliminatedLines == 0 && result.reachTop) { onGameOver() } else { @@ -255,14 +251,12 @@ export function fallDownTetromino(tetromino, tetrominoes, onCollise, onGameOver, } export function predictTetromino(tetromino, tetrominoes) { - if (tetromino) { - let result - const predicted = { ...tetromino } - do { - predicted.y++ - result = detectCollision(predicted, DOWN, tetrominoes) - } while (!result.collised) - predicted.y-- - return predicted - } + let result + const predicted = { ...tetromino } + do { + predicted.y++ + result = detectCollision(predicted, DOWN, tetrominoes) + } while (!result.collised) + predicted.y-- + return predicted } \ No newline at end of file diff --git a/src/pages/index.js b/src/pages/index.js index 58e5665..d9a4988 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -14,7 +14,8 @@ var audio export default function Home() { const [tetrominoes, setTetrominoes] = useState([]) const [currentTetromino, setCurrentTetromino] = useState() - const predictedTetromino = predictTetromino(currentTetromino, tetrominoes) + const currentTetrominoValid = currentTetromino && currentTetromino.points.length > 0 + const predictedTetromino = currentTetrominoValid ? predictTetromino(currentTetromino, tetrominoes) : null const [nextTetromino, setNextTetromino] = useState() const [score, setScore] = useState(0) const [eliminatedLines, setEliminatedLines] = useState(0) @@ -83,23 +84,15 @@ export default function Home() { } } - const frozen = () => { + const next = () => { if (currentTetromino) { tetrominoes.push(currentTetromino) setTetrominoes([...tetrominoes.filter(({ points }) => points.length > 0)]) } - } - - const next = () => { - frozen() setCurrentTetromino(nextTetromino) setNextTetromino(randomTetromino()) } - const rotate = () => { - setCurrentTetromino(rotateTetromino(currentTetromino, tetrominoes)) - } - const onCollise = (elimiLines) => { if (elimiLines > 0) { setEliminatedLines(eliminatedLines + elimiLines) @@ -109,33 +102,43 @@ export default function Home() { queueMicrotask(next) } + const rotate = () => { + if (!gameOver && currentTetrominoValid) { + setCurrentTetromino(rotateTetromino(currentTetromino, tetrominoes)) + } + } + const down = () => { - setCurrentTetromino( - moveDownTetromino( - currentTetromino, - tetrominoes, - onCollise, - () => setGameOver(true), - setTetrominoes) - ) + if (!gameOver && currentTetrominoValid) { + setCurrentTetromino( + moveDownTetromino( + currentTetromino, + tetrominoes, + onCollise, + () => setGameOver(true)) + ) + } } const left = () => { - setCurrentTetromino(moveLeftTetromino(currentTetromino, tetrominoes)) + if (!gameOver && currentTetrominoValid) { + setCurrentTetromino(moveLeftTetromino(currentTetromino, tetrominoes)) + } } const right = () => { - setCurrentTetromino(moveRightTetromino(currentTetromino, tetrominoes)) + if (!gameOver && currentTetrominoValid) { + setCurrentTetromino(moveRightTetromino(currentTetromino, tetrominoes)) + } } const fallDown = () => { - if (!gameOver && currentTetromino) { + if (!gameOver && currentTetrominoValid) { fallDownTetromino( currentTetromino, tetrominoes, onCollise, - () => setGameOver(true), - setTetrominoes) + () => setGameOver(true)) } }