Fixed jank regarding the area above the field

- The game now discards blocks locked over y = 1 instead of panicking and crashing
This commit is contained in:
Oshisaure 2020-10-07 20:40:43 +01:00
parent b7ef7d1976
commit a4984fd687

View File

@ -3,6 +3,7 @@ local Object = require 'libs.classic'
local Grid = Object:extend() local Grid = Object:extend()
local empty = { skin = "", colour = "" } local empty = { skin = "", colour = "" }
local oob = { skin = "", colour = "" }
function Grid:new() function Grid:new()
self.grid = {} self.grid = {}
@ -26,8 +27,15 @@ function Grid:clear()
end end
end end
function Grid:getCell(x, y)
if x < 1 or x > 10 or y > 24 then return oob
elseif y < 1 then return empty
else return self.grid[y][x]
end
end
function Grid:isOccupied(x, y) function Grid:isOccupied(x, y)
return self.grid[y+1][x+1] ~= empty return self:getCell(x+1, y+1) ~= empty
end end
function Grid:isRowFull(row) function Grid:isRowFull(row)
@ -46,7 +54,7 @@ function Grid:canPlacePiece(piece)
for index, offset in pairs(offsets) do for index, offset in pairs(offsets) do
local x = piece.position.x + offset.x local x = piece.position.x + offset.x
local y = piece.position.y + offset.y local y = piece.position.y + offset.y
if x >= 10 or x < 0 or y >= 24 or y < 0 or self.grid[y+1][x+1] ~= empty then if self:isOccupied(x, y) then
return false return false
end end
end end
@ -58,13 +66,12 @@ function Grid:canPlaceBigPiece(piece)
for index, offset in pairs(offsets) do for index, offset in pairs(offsets) do
local x = piece.position.x + offset.x local x = piece.position.x + offset.x
local y = piece.position.y + offset.y local y = piece.position.y + offset.y
if y < 0 then return true if (
elseif x >= 5 or x < 0 or y >= 12 or self:isOccupied(x * 2 + 0, y * 2 + 0)
self.grid[y * 2 + 1][x * 2 + 1] ~= empty or or self:isOccupied(x * 2 + 1, y * 2 + 0)
self.grid[y * 2 + 1][x * 2 + 2] ~= empty or or self:isOccupied(x * 2 + 0, y * 2 + 1)
self.grid[y * 2 + 2][x * 2 + 1] ~= empty or or self:isOccupied(x * 2 + 1, y * 2 + 1)
self.grid[y * 2 + 2][x * 2 + 2] ~= empty ) then
then
return false return false
end end
end end
@ -81,7 +88,7 @@ function Grid:canPlacePieceInVisibleGrid(piece)
for index, offset in pairs(offsets) do for index, offset in pairs(offsets) do
local x = piece.position.x + offset.x local x = piece.position.x + offset.x
local y = piece.position.y + offset.y local y = piece.position.y + offset.y
if x >= 10 or x < 0 or y >= 24 or y < 4 or self.grid[y+1][x+1] ~= empty then if y < 4 or self:isOccupied(x, y) ~= empty then
return false return false
end end
end end
@ -167,10 +174,12 @@ function Grid:applyBigPiece(piece)
y = piece.position.y + offset.y y = piece.position.y + offset.y
for a = 1, 2 do for a = 1, 2 do
for b = 1, 2 do for b = 1, 2 do
self.grid[y*2+a][x*2+b] = { if y*2+a > 0 then
skin = piece.skin, self.grid[y*2+a][x*2+b] = {
colour = piece.shape skin = piece.skin,
} colour = piece.shape
}
end
end end
end end
end end