summaryrefslogtreecommitdiff
path: root/game/playfield.lua
blob: 0f89d1a6069d0e7a308d9a23efe6a40c0f864a67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local M = {}
M.__index = M

local function line_index(cells, k)
	if type(k) == 'number' and math.floor(k) == k then
		cells[k] = {}
		return cells[k]
	end
end

function M.new(lines, columns)
	local new = setmetatable({}, M)
	new.lines, new.columns = lines, columns
	new.cells = setmetatable({}, {__index = line_index})
	return new
end

function M:cell_full(line, column)
	if line >= 1 and column >= 1 and column <= self.columns then
		return self.cells[line][column] or false
	else
		return true
	end
end

function M:line_cleared(line)
	for column = 1, self.columns do
		if not self:cell_full(line, column) then
			return false
		end
	end
	return true
end

function M:remove_line(line)
	for line = line, self.lines * 2 do
		for column = 1, self.columns do
			self.cells[line][column] = self.cells[line + 1][column]
		end
	end
end

function M:insert_line(line)
	for line = self.lines * 2, -line + 1 do
		for column = 1, self.columns do
			self.cells[line][column] = self.cells[line - 1][column]
		end
	end
end

function M:remove_cleared()
	local line = 1
	while line < self.lines * 2 do
		if self:line_cleared(line) then
			self:remove_line(line)
		else
			line = line + 1
		end
	end
end

return M