diff options
author | the lemons <citrons@mondecitronne.com> | 2023-03-24 04:16:38 -0500 |
---|---|---|
committer | the lemons <citrons@mondecitronne.com> | 2023-03-24 04:16:38 -0500 |
commit | 1c167b5d959491301144f68d80edcaee35647330 (patch) | |
tree | e14024b9e16c10b320d39976c16285b5f1424630 | |
parent | 88e53b6f21d1183ec86f6234e2108ee331367920 (diff) |
L heaven
-rw-r--r-- | main.lua | 2 | ||||
-rw-r--r-- | polyomino.lua | 37 |
2 files changed, 37 insertions, 2 deletions
@@ -73,6 +73,8 @@ function love.keypressed(key) piece:move(0, 1) elseif key == "down" then piece:move(-1, 0) + elseif key == "up" then + piece:rotate() end end end diff --git a/polyomino.lua b/polyomino.lua index 00f20bc..2462629 100644 --- a/polyomino.lua +++ b/polyomino.lua @@ -21,6 +21,8 @@ function M.def(name, shape) table.insert(new.cells, 1, line) end + if name == "tetr.O" then print(#new.cells) end + for line = 1, new.size do for column = 1, new.size do if new.cells[line][column] then @@ -50,7 +52,28 @@ function M:drop(field) return new end +local rotations = { + {1, 0, 0, 1}, + {0, 1, -1, 0}, + {-1, 0, 0, -1}, + {0, -1, 1, 0}, +} + +local function rotate(line, column, rotation, size) + local center = size / 2 - 0.5 + line = line - center + column = column - center + local rotation = rotations[rotation or rotation] + local l = line * rotation[1] + column * rotation[2] + local c = line * rotation[3] + column * rotation[4] + line = l + center + column = c + center + return line, column +end + function piece:get_cell(line, column, rotation) + line, column = rotate( + line, column, rotation or self.rotation, self.poly.size) return self.poly.cells[line + 1][column + 1] end @@ -59,7 +82,7 @@ function piece:can_occupy(line, column, rotation) column = column or self.column for l = 0, self.poly.size - 1 do for c = 0, self.poly.size - 1 do - if self:get_cell(l, c) + if self:get_cell(l, c, rotation) and self.field:cell_full(line + l, column + c) then return false end @@ -80,7 +103,17 @@ function piece:place() end function piece:rotate(ccw) - return rotated + local rotation = self.rotation + (ccw and -1 or 1) + if rotation > 4 then + rotation = 1 + elseif rotation < 1 then + rotation = 4 + end + if not self:can_occupy(nil, nil, rotation) then + return false + end + self.rotation = rotation + return true end function piece:move(lines, columns) |