-
Notifications
You must be signed in to change notification settings - Fork 0
/
TetriminoManager.lua
299 lines (265 loc) · 6.55 KB
/
TetriminoManager.lua
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
local tetriminoData = {
{
{
{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{163, 239, 239}
},
{
{
{1, 0, 0},
{1, 1, 1},
{0, 0, 0},
},
{112, 112, 239}
},
{
{
{0, 0, 1},
{1, 1, 1},
{0, 0, 0}
},
{255, 210, 96}
},
{
{
{1, 1},
{1, 1}
},
{255, 247, 135}
},
{
{
{0, 1, 1},
{1, 1, 0},
{0, 0, 0}
},
{148, 239, 148}
},
{
{
{1, 1, 0},
{0, 1, 1},
{0, 0, 0}
},
{239, 112, 143}
},
{
{
{0, 1, 0},
{1, 1, 1},
{0, 0, 0}
},
{179, 122, 239}
}
}
-- 0 is starting position
-- 1 is 90 clockwise
-- 2 is full 180
-- 3 is 270 clockwise
local kickTests = { -- all clockwise by default
basic = {
{ -- 0>>1
{0, 0},
{-1, 0},
{-1, 1},
{0, -2},
{-1, -2}
},
{ -- 1>>2
{0, 0},
{1, 0},
{1, -1},
{0, 2},
{1, 2}
},
{ -- 2>>3
{0, 0},
{1, 0},
{1, 1},
{0, -2},
{1, -2}
},
{ -- 3>>0
{0, 0},
{-1, 0},
{-1, -1},
{0, 2},
{-1, 2}
}
},
ITetrimino = {
{ -- 0>>1
{0, 0},
{-2, 0},
{1, 0},
{-2, -1},
{1, 2}
},
{ -- 1>>2
{0, 0},
{-1, 0},
{2, 0},
{-1, 2},
{2, -1}
},
{ -- 2>>3
{0, 0},
{2, 0},
{-1, 0},
{2, 1},
{-1, -2}
},
{ -- 3>>0
{0, 0},
{1, 0},
{-2, 0},
{1, -2},
{-2, 1}
}
}
}
local Tetrimino = {}
Tetrimino.__index = Tetrimino
--[[
local function loopThroughTableElements(tbl, func)
for i, element in ipairs(tbl) do
func(i, element)
end
end ]]
local function loopThroughMatrixElements(matrix, func)
for row, elements in ipairs(matrix) do
for column, element in ipairs(elements) do
func(row, column, element)
end
end
end
local function findFirstInTable(tbl, valueToFind)
for i, value in ipairs(tbl) do
if value == valueToFind then
return i
end
end
end
local function changeIfAlready(oldValue, newValue, expectedOldValue)
if oldValue == expectedOldValue or oldValue == nil then
return newValue
else
return oldValue
end
end
local function tableSum(tbl)
local sum = 0
for _, value in ipairs(tbl) do
sum = sum + value
end
return sum
end
function Tetrimino.new(tileMatrix, color, isIPiece)
local self = setmetatable({}, Tetrimino)
self.tileCoordinates = {}
local tileMatrixRows = {}
local tileMatrixColumns = {}
loopThroughMatrixElements(tileMatrix, function(row, column, tileValue)
tileMatrixRows[row] = changeIfAlready(
tileMatrixRows[row], tileValue, 0
)
tileMatrixColumns[column] = changeIfAlready(
tileMatrixColumns[column], tileValue, 0
)
if tileValue == 1 then
table.insert(self.tileCoordinates, {column-1, row-1})
end
end)
self.displayRectOffset = {
x = findFirstInTable(tileMatrixColumns, 1) - 1,
y = findFirstInTable(tileMatrixRows, 1) - 1
}
self.displayDimensions = {
width = tableSum(tileMatrixColumns),
height = tableSum(tileMatrixRows)
}
self.rect = {
width = #tileMatrix[1],
height = #tileMatrix
}
self.color = color
self.isIPiece = isIPiece
return self
end
function Tetrimino:getColor() return self.color end
function Tetrimino:getKickTests()
if self.isIPiece then
return kickTests.ITetrimino
end
return kickTests.basic
end
function Tetrimino:getTileCoordinates()
return self.tileCoordinates
end
function Tetrimino:getRect()
return self.rect.width, self.rect.height
end
function Tetrimino:getDisplayDimensions()
return self.displayDimensions.width, self.displayDimensions.height
end
function Tetrimino:getDisplayRectOffset()
return self.displayRectOffset.x, self.displayRectOffset.y
end
local TetriminoManager = {}
TetriminoManager.__index = TetriminoManager
function TetriminoManager.new()
local self = setmetatable({}, TetriminoManager)
self.tetriminos = {}
for i, params in ipairs(tetriminoData) do
local tileMap, rgbColors = unpack(params)
local colors = {}
for _, channel in ipairs(rgbColors) do table.insert(colors, channel/255) end
table.insert(self.tetriminos, Tetrimino.new(tileMap, colors, i==1))
end
self.tetriminoSequence = {}
self.tetriminoBag = {}
self.lastTetriminoIndex = 0
self.heldTetriminoIndex = 0
return self
end
function TetriminoManager:nextTetrimino()
if #self.tetriminoBag <= 0 then
for i=1, #self.tetriminos do
table.insert(self.tetriminoBag, i)
--io.write(i .. ' ')
end
--print()
end
while #self.tetriminoSequence <= 3 do
local tetriminoBagIndex = math.random(1, #self.tetriminoBag)
local tetriminoIndex = table.remove(self.tetriminoBag, tetriminoBagIndex)
--print(tetriminoIndex)
table.insert(self.tetriminoSequence, tetriminoIndex)
end
self.lastTetriminoIndex = table.remove(self.tetriminoSequence, 1)
return self.tetriminos[self.lastTetriminoIndex]
end
function TetriminoManager:switchHeldTetrimino()
local originalHeldTetriminoIndex = self.heldTetriminoIndex
self.heldTetriminoIndex = self.lastTetriminoIndex
self.lastTetriminoIndex = originalHeldTetriminoIndex
if originalHeldTetriminoIndex == 0 then
return self:nextTetrimino()
end
return self.tetriminos[self.lastTetriminoIndex]
end
function TetriminoManager:getUpcomingTetriminos()
local upcomingTetriminos = {}
for _, tetriminoIndex in ipairs(self.tetriminoSequence) do
table.insert(upcomingTetriminos, self.tetriminos[tetriminoIndex])
end
return upcomingTetriminos
end
function TetriminoManager:getHeldTetrimino()
return self.tetriminos[self.heldTetriminoIndex]
end
return TetriminoManager