-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.lua
More file actions
125 lines (98 loc) · 2.65 KB
/
block.lua
File metadata and controls
125 lines (98 loc) · 2.65 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
local Block = {}
Block.__index = Block
function Block:create(...)
local block = {}
setmetatable(block, Block)
block.type = BLOCK_TYPES[love.math.random(7)]
block.grid = self.grid
block.rotation = 1
block.offsetX = 3
block.offsetY = 0
return block
end
function Block:draw()
local shape = BLOCK_SHAPES[self.type][self.rotation]
for rowIndex = 1, 4 do
for columnIndex = 1, 4 do
local cell = shape[rowIndex][columnIndex]
local x = columnIndex + self.offsetX
local y = rowIndex + self.offsetY
if cell ~= ' ' then
self.grid:drawCell(cell, x, y)
end
end
end
end
function Block:rotateRight()
local rotation = self.rotation + 1
if rotation > #BLOCK_SHAPES[self.type] then
rotation = 1
end
if self:canMove { rotation=rotation } then
self.rotation = rotation
end
end
function Block:rotateLeft()
local rotation = self.rotation - 1
if rotation < 1 then
rotation = #BLOCK_SHAPES[self.type]
end
if self:canMove { rotation=rotation } then
self.rotation = rotation
end
end
function Block:moveLeft()
local offsetX = self.offsetX - 1
if self:canMove { offsetX=offsetX } then
self.offsetX = offsetX
end
end
function Block:moveRight()
local offsetX = self.offsetX + 1
if self:canMove { offsetX=offsetX } then
self.offsetX = offsetX
end
end
function Block:moveDown()
local offsetY = self.offsetY + 1
if self:canMove { offsetY=offsetY } then
self.offsetY = offsetY
else
self:rest()
self.grid:next()
end
end
function Block:canMove(args)
local args = args or {} -- for when no args are given.
local offsetX = args.offsetX or self.offsetX
local offsetY = args.offsetY or self.offsetY
local rotation = args.rotation or self.rotation
local shape = BLOCK_SHAPES[self.type][rotation]
for rowIndex = 1, 4 do
for columnIndex = 1, 4 do
local testBlockX = offsetX + rowIndex
local testBlockY = offsetY + columnIndex
if shape[columnIndex][rowIndex] ~= ' ' and (
testBlockX < 1 -- left border
or testBlockX > self.grid.columns -- right border
or testBlockY > self.grid.rows -- bottom border
or self.grid.inert[testBlockY][testBlockX] ~= ' ' -- already filled
) then
return false
end
end
end
return true
end
function Block:rest()
local shape = BLOCK_SHAPES[self.type][self.rotation]
for rowIndex = 1, 4 do
for columnIndex = 1, 4 do
local cell = shape[rowIndex][columnIndex]
if cell ~= ' ' then
self.grid.inert[self.offsetY + rowIndex][self.offsetX + columnIndex] = cell
end
end
end
end
return Block