Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created Nov 2, 2020
Embed
What would you like to do?
mainLoop() {
if (!this.canPlay) {
return;
}
// If figure can move down, move down
if (this.figureCanMoveDown()) {
this.globalY++;
} else {
// If figure cannot, then we start a timeout because
// player can move figure to keep it going down
// for example when the figure collapses with another points but there's remaining
// space at the left or right and the player moves there so the figure can keep going down
if (this.timeoutFlag) return;
this.timeoutFlag = true;
setTimeout(() => {
this.timeoutFlag = false;
// If the time expires, we re-check if figure cannot keep going down. If it can
// (because player moved it) then we return and keep the loop
if (this.figureCanMoveDown()) {
return;
}
// At this point, we know that the figure collapsed either with the floor
// or with another point. So we move all the figure to the existing pieces array
this.sounds.tap.currentTime = 0;
this.sounds.tap.play();
this.moveFigurePointsToExistingPieces();
if (this.playerLoses()) {
Swal.fire("Juego terminado", "Inténtalo de nuevo");
this.sounds.background.pause();
this.canPlay = false;
this.resetGame();
return;
}
this.verifyAndDeleteFullRows();
this.chooseRandomFigure();
this.syncExistingPiecesWithBoard()
}, Game.TIMEOUT_LOCK_PUT_NEXT_PIECE);
}
this.syncExistingPiecesWithBoard();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment