fix rotation

This commit is contained in:
Chris Wanstrath 2025-09-29 10:47:50 -07:00
parent 7d5062afab
commit f7bf91f085

View File

@ -174,9 +174,7 @@ export function update(_delta: number, input: InputState) {
const keys = input.pressed const keys = input.pressed
if (input.justPressed.has(" ")) { if (input.justPressed.has(" ")) {
player.rotation += 1 rotateShape()
if (player.rotation === SHAPES[player.shape]!.length)
player.rotation = 0
} }
// tetris animation // tetris animation
@ -456,3 +454,25 @@ function drawPreview(game: GameContext) {
} }
} }
} }
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
}
}