fix rotation and placement

This commit is contained in:
Jeremy Penner 2022-04-24 21:47:13 -04:00
parent c8a42fd631
commit 370d5ebc7e
2 changed files with 12 additions and 3 deletions

View file

@ -140,7 +140,16 @@
(fn Rules.rotate [di] (fn Rules.rotate [di]
(when state.current (when state.current
(set state.current.irotation (+ state.current.irotation di)))) (var settled false)
(let [irotation (+ state.current.irotation di)
tetromino (Rules.tetromino state.current.piece irotation)]
(each [_ dx (ipairs [0 1 -1]) :until settled]
(let [x (+ state.current.x dx)
y state.current.y]
(when (not (Rules.collides? tetromino x y))
(set settled true)
(set state.current.x x)
(set state.current.irotation irotation)))))))
(set Rules.commands (set Rules.commands
{:move Rules.try-move-piece {:move Rules.try-move-piece

View file

@ -21,8 +21,8 @@
(fn set-itile-at [x y tilemap itile] (fn set-itile-at [x y tilemap itile]
(let [row (or (. tilemap (+ y 1)) "") (let [row (or (. tilemap (+ y 1)) "")
row (.. row (string.rep " " (math.max 0 (- x (length row))))) row (.. row (string.rep " " (math.max 0 (- x (length row)))))
pre (row:sub 1 (- x 1)) pre (row:sub 1 x)
post (row:sub (+ x 1)) post (row:sub (+ x 2))
row (.. pre itile post)] row (.. pre itile post)]
(tset tilemap (+ y 1) row))) (tset tilemap (+ y 1) row)))