Add ghost piece preview showing where block will land

- Add getGhostY() method to TetrisGame class (game.js)
- Add ghostY to getState() output
- Add getGhostY() helper to server (server/index.js)
- Include ghostY in getStates() broadcast
- Add drawGhostPiece() and drawGhostCell() methods (renderer.js)
- Ghost renders as semi-transparent outline at landing position
This commit is contained in:
2026-03-20 09:37:41 -07:00
parent 708b1466d6
commit 164ed790ed
4 changed files with 62 additions and 6 deletions
+12
View File
@@ -280,6 +280,16 @@ class TetrisGame {
this.dropInterval = Math.max(100, 1000 - (this.level - 1) * 100);
}
getGhostY() {
if (!this.currentPiece) return null;
let ghostY = this.currentPiece.y;
while (this.isValidPosition(this.currentPiece.x, ghostY + 1, this.currentPiece.shape)) {
ghostY++;
}
return ghostY;
}
receiveGarbage(rows) {
if (this.gameOver) return;
@@ -328,10 +338,12 @@ class TetrisGame {
}
getState() {
const ghostY = this.currentPiece ? this.getGhostY() : null;
return {
playerId: this.playerId,
board: JSON.parse(JSON.stringify(this.board)),
currentPiece: this.currentPiece ? JSON.parse(JSON.stringify(this.currentPiece)) : null,
ghostY: ghostY,
nextPiece: this.nextPiece ? JSON.parse(JSON.stringify(this.nextPiece)) : null,
holdPiece: this.holdPiece ? JSON.parse(JSON.stringify(this.holdPiece)) : null,
canHold: this.canHold,