From 0f0b9d1b4e1b1b14b07bfd5e4df7cf31b40a1e5c Mon Sep 17 00:00:00 2001 From: ctcpip Date: Tue, 27 Sep 2022 18:33:08 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20issue=20where=20drop=20did?= =?UTF-8?q?=20not=20lock=20shape=20right=20away?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- board.js | 29 +++++++++++++++++++++++------ netrisse.js | 2 +- shape.js | 28 ++++++++++------------------ 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/board.js b/board.js index 7489187..95b7ce0 100644 --- a/board.js +++ b/board.js @@ -173,7 +173,7 @@ module.exports = class Board { this.nextShapeType = this.algorithm.next().value; this.currentShape = Shape.createNewShape(this.screen, this, newShapeType); - this.currentTimeout = setTimeout(this.moveShapeAutomatically.bind(this), this.game.interval); + this.resetAutoMoveTimer(); this.drawHeldShape(this.nextShapeType, false, false); this.screen.render(); @@ -257,9 +257,17 @@ module.exports = class Board { } - lockShape(gameOver) { + lockShape() { + + let gameOver = false; this.occupiedPoints.push(...this.currentShape.currentPoints); + + // check if game over. if lowest y value (highest point of shape) is outside of top border, it's curtains! (probably) + if (Math.min(...this.currentShape.currentPoints.map(p => p[1])) <= this.top) { // eslint-disable-line max-depth + gameOver = true; + } + this.clearLines(gameOver); if (gameOver) { @@ -280,12 +288,12 @@ module.exports = class Board { const txtPaused = 'Game paused by you'; if (this.game.paused) { - clearTimeout(this.currentTimeout); + this.stopAutoMoveTimer(); this.screen.d(24, 21, txtPaused); } else { - this.currentTimeout = setTimeout(this.moveShapeAutomatically.bind(this), this.game.interval); + this.resetAutoMoveTimer(); for (let i = 0; i < txtPaused.length; i++) { this.screen.d(24 + i, 21, ' '); @@ -304,7 +312,7 @@ module.exports = class Board { return; } - clearTimeout(this.currentTimeout); + this.stopAutoMoveTimer(); let copyHeldShape; @@ -336,7 +344,7 @@ module.exports = class Board { this.screen.render(); - this.currentTimeout = setTimeout(this.moveShapeAutomatically.bind(this), this.game.interval); + this.resetAutoMoveTimer(); } else { @@ -414,5 +422,14 @@ module.exports = class Board { } + stopAutoMoveTimer() { + clearTimeout(this.currentTimeout); + } + + resetAutoMoveTimer() { + this.stopAutoMoveTimer(); + this.currentTimeout = setTimeout(this.moveShapeAutomatically.bind(this), this.game.interval); + } + }; diff --git a/netrisse.js b/netrisse.js index 845ef7c..ca97fdc 100644 --- a/netrisse.js +++ b/netrisse.js @@ -16,7 +16,7 @@ const board = new Board(2, 21, 23, 0, screen, game); game.boards.push(board); function quit() { - clearTimeout(board.currentTimeout); + board.stopAutoMoveTimer(); clearTimeout(screen.timeDisplayTimeout); screen.term.grabInput(false); screen.term.moveTo(board.left + 1, board.bottom + 1); diff --git a/shape.js b/shape.js index 82105f8..2561d96 100644 --- a/shape.js +++ b/shape.js @@ -158,7 +158,6 @@ module.exports = class Shape { let lockShape = false; let canMove = true; - let gameOver = false; const offset = [0, 0]; @@ -179,24 +178,17 @@ module.exports = class Shape { } if (canMove) { - if (direction === directions.DROP) { offset[1] = downDropPositionData.dropGhostOffsetY; } else { offset[1] = downDropPositionData.offsetY; } - } - else if (direction === directions.AUTO || direction === directions.DROP) { + + if ((direction === directions.AUTO && !canMove) || direction === directions.DROP) { // eslint-disable-line no-extra-parens // only lock when auto-moved or dropped lockShape = true; - - // check if game over. if lowest y value (highest point of shape) is outside of top border, it's curtains! (probably) - if (Math.min(...this.currentPoints.map(p => p[1])) <= this.board.top) { // eslint-disable-line max-depth - gameOver = true; - } - } } @@ -267,10 +259,8 @@ module.exports = class Shape { throw new Error('like a rolling stone!'); } - if (lockShape) { - this.board.lockShape(gameOver); - } - else if (canMove) { + if (canMove) { + this.drawGhost(true); this.draw(true); this.currentPoints = newShapePoints; @@ -281,15 +271,13 @@ module.exports = class Shape { this.screen.render(); switch (direction) { + case directions.AUTO: case directions.DOWN: // if shape was manually moved down, reset the automatic down timer. // this prevents automatically locking the shape immediately after // moving down to touch the bottom or another shape, giving the player // the normal amount of time to rotate or move the piece before it locks - clearTimeout(this.board.currentTimeout); - // fall through to start a new timeout - case directions.AUTO: - this.board.currentTimeout = setTimeout(this.board.moveShapeAutomatically.bind(this.board), this.board.game.interval); + this.board.resetAutoMoveTimer(); break; default: break; @@ -297,6 +285,10 @@ module.exports = class Shape { } + if (lockShape) { + this.board.lockShape(); + } + this.board.concurrentExecutions -= 1; }