Skip to content

Commit

Permalink
🐛 fix issue where drop did not lock shape right away
Browse files Browse the repository at this point in the history
  • Loading branch information
ctcpip committed Sep 27, 2022
1 parent 4bf3228 commit 0f0b9d1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
29 changes: 23 additions & 6 deletions board.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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, ' ');
Expand All @@ -304,7 +312,7 @@ module.exports = class Board {
return;
}

clearTimeout(this.currentTimeout);
this.stopAutoMoveTimer();

let copyHeldShape;

Expand Down Expand Up @@ -336,7 +344,7 @@ module.exports = class Board {

this.screen.render();

this.currentTimeout = setTimeout(this.moveShapeAutomatically.bind(this), this.game.interval);
this.resetAutoMoveTimer();

}
else {
Expand Down Expand Up @@ -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);
}

};

2 changes: 1 addition & 1 deletion netrisse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 10 additions & 18 deletions shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ module.exports = class Shape {

let lockShape = false;
let canMove = true;
let gameOver = false;

const offset = [0, 0];

Expand All @@ -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;
}

}

}
Expand Down Expand Up @@ -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;
Expand All @@ -281,22 +271,24 @@ 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;
}

}

if (lockShape) {
this.board.lockShape();
}

this.board.concurrentExecutions -= 1;

}
Expand Down

0 comments on commit 0f0b9d1

Please sign in to comment.