From f7bf91f085fa12ca1751c7a7cc72fe0ae11f5ae5 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Mon, 29 Sep 2025 10:47:50 -0700 Subject: [PATCH] fix rotation --- app/nose/bin/tetris.ts | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/app/nose/bin/tetris.ts b/app/nose/bin/tetris.ts index 31e6bac..455db0f 100644 --- a/app/nose/bin/tetris.ts +++ b/app/nose/bin/tetris.ts @@ -174,9 +174,7 @@ export function update(_delta: number, input: InputState) { const keys = input.pressed if (input.justPressed.has(" ")) { - player.rotation += 1 - if (player.rotation === SHAPES[player.shape]!.length) - player.rotation = 0 + rotateShape() } // tetris animation @@ -455,4 +453,26 @@ function drawPreview(game: GameContext) { drawBlock(game, previewX + col + offsetX, previewY + row + offsetY, COLORS[nextShape.shape]!) } } -} \ No newline at end of file +} + +function rotateShape() { + const oldRot = player.rotation + const newRot = (oldRot + 1) % SHAPES[player.shape]!.length + + if (!collision(player.x, player.y, newRot)) { + player.rotation = newRot + return + } + + if (!collision(player.x + 1, player.y, newRot)) { + player.x += 1 + player.rotation = newRot + return + } + + if (!collision(player.x - 1, player.y, newRot)) { + player.x -= 1 + player.rotation = newRot + return + } +}