-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.lua
345 lines (293 loc) · 8.82 KB
/
game.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
--[[
welcome to game.lua!
this is where the games algorithims are stored in
this is where most of the errors and bugs happen
make sure to use the console!
]]
game = {}
local tilemap = require 'tilemap'
local Gravity = require 'Gravity'
game.cp = {} --this stores the players current piece (a matrix)
game.cpX = 0
game.cpY = 0
game.colors = {{255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {0, 255, 255}, {255,211,0}} --table of piece colors
game.moveTimer = 20 --timer that when reaches 0, it moves the piece down and resets
game.cpDir = 0 --0=down,1=left,2=right,3=up
game.gamestate = 0 --0=title,1=options,2=game,3=pause,4=game end
game.score = 0
--size of the pieces
local pw = 2
local ph = 1
local t1
local t2
local cpDirTable = {{{}}}
pop = love.audio.newSource("pop.wav", "stream")
game.newPiece = function()
game.cpX = 8
game.cpY = 0
game.cpDir = 2
t1 = {type=2, color=game.colors[love.math.random(1, #game.colors)]}
t2 = {type=2, color=game.colors[love.math.random(1, #game.colors)]}
cpDirTable[0] = {{t2},{t1}} --down
cpDirTable[1] = {{t1, t2}} --left
cpDirTable[2] = {{t1},{t2}} --right
cpDirTable[3] = {{t2, t1}} --up
game.cpDir = 2
game.cp = cpDirTable[game.cpDir] --set it to right for start
if #game.cp == 0 then error("oh no") end
end
function game.clearMatches()
local x
local y
local sn = 0
for x=0, tilemap.width - 1, 1 do
for y=0, tilemap.height-1, 1 do
sn = sn + checkAndRemoveMatches(x, y)
end
end
return sn
end
game.initGame = function()
game.newPiece()
end
game.movePiece = function(x, y) --move piece by x and y + check collision
tilemapSetPiece(true)
if gameOver() then return end
--we check collision here and change vars accordingly
cr = moveAndCheckCollision(x, y)
game.cpX = cr.x
game.cpY = cr.y
if cr.cp then
t1.type = 1
t2.type = 1
end
tilemapSetPiece(t1, t2)
if cr.cp then
if game.clearMatches() > 0 then love.audio.play(pop) end
Gravity.fall()
game.newPiece()
end
end
game.rotatePiece = function(ccw) --rotates a piece. set 'ccw' to True if rotating ccw
tilemapSetPiece(true)
local origDir = game.cpDir
local t2x
local t2y
if ccw then
if game.cpDir == 0 then
game.cpDir = 2
t2x = 1
t2y = 0
elseif game.cpDir == 1 then
game.cpDir = 0
t2x = 0
t2y = -1
elseif game.cpDir == 2 then
game.cpDir = 3
t2x = 0
t2y = 1
elseif game.cpDir == 3 then
game.cpDir = 1
t2x = -1
t2y = 0
end
else
if game.cpDir == 0 then
game.cpDir = 1
t2x = -1
t2y = 0
elseif game.cpDir == 1 then
game.cpDir = 3
t2x = 0
t2y = 1
elseif game.cpDir == 2 then
game.cpDir = 0
t2x = 0
t2y = -1
elseif game.cpDir == 3 then
game.cpDir = 2
t2x = 1
t2y = 0
end
end
if (tilemap.get(game.cpX + t2x, game.cpY + t2y).type ~= 0) or (game.cpX + t2x < 0) or (game.cpX + t2x > tilemap.width - 1) then game.cpDir = origDir end
game.cp = cpDirTable[game.cpDir]
tilemapSetPiece(t1, t2)
end
function moveAndCheckCollision(cbmx, cbmy)
--[[
returns coords if the piece cant be inside another tile or out of bounds
]]
local canPlace = false
local go
local wo
local t2x --offsets
local t2y
local wo2 = function() if game.cpDir == 1 then return 1 else return 0 end end
if game.cpDir == 0 then
go = 0
wo = 0
t2x = 0
t2y = -1
elseif game.cpDir == 1 then
go = 0
wo = 0
t2x = -1
t2y = 0
elseif game.cpDir == 2 then
go = 0
wo = -1
t2x = 1
t2y = 0
elseif game.cpDir == 3 then
go = -1
wo = 0
t2x = 0
t2y = 1
end
if (game.cpY == tilemap.height - 1 + go) or (tilemap.get(game.cpX, game.cpY + 1).type == 1) or (tilemap.get(game.cpX + t2x, game.cpY + t2y + 1).type == 1) then
canPlace = true
cbmx = 0
cbmy = 0
end
if cbmx < 0 and ((game.cpX == 0 + wo2()) or (tilemap.get(game.cpX - 1, game.cpY).type == 1) or (tilemap.get(game.cpX + t2x - 1, game.cpY + t2y).type == 1)) then --left movement
cbmx = 0
end
if cbmx > 0 and ((game.cpX == tilemap.width - 1 + wo) or (tilemap.get(game.cpX + 1, game.cpY).type == 1) or (tilemap.get(game.cpX + t2x + 1, game.cpY + t2y).type == 1)) then --right movement
cbmx = 0
end
return {x=game.cpX+cbmx, y=game.cpY+cbmy, cp=canPlace}
end
function tilemapSetPiece(tp1, tp2)
if tp1 == true then
tp1 = {type=0, color={0, 0, 0}}
tp2 = {type=0, color={0, 0, 0}}
end
if game.cpDir == 0 then
tilemap.set(game.cpX, game.cpY, tp1)
tilemap.set(game.cpX, game.cpY-1, tp2)
elseif game.cpDir == 1 then
tilemap.set(game.cpX, game.cpY, tp1)
tilemap.set(game.cpX-1, game.cpY, tp2)
elseif game.cpDir == 2 then
tilemap.set(game.cpX, game.cpY, tp1)
tilemap.set(game.cpX+1, game.cpY, tp2)
elseif game.cpDir == 3 then
tilemap.set(game.cpX, game.cpY, tp1)
tilemap.set(game.cpX, game.cpY+1, tp2)
end
end
function gameOver()
if game.cpY == 0 and tilemap.get(game.cpX, 1).type == 1 then
for i=0,tilemap.width - 1 do
for j=0,tilemap.height - 1 do
tilemap.set(i, j, {type=0, color={0, 0, 0}})
end
end
game.score = 0
return true
else return false end
end
--------------------------------------------tile check functions----------------------------------------------------------------------------------------------------------
function checkAndRemoveMatches(tx, ty)
local del = {x={}, y={}}
--up
local ctx = tx
local cty = ty
local same = true
local sameCounter = 0
local tempDel = {x={}, y={}}
repeat
if tilemap.get(ctx, cty).color == tilemap.get(tx, ty).color then
table.insert(tempDel.x, ctx)
table.insert(tempDel.y, cty)
sameCounter = sameCounter + 1
same = true
cty = cty - 1
else
same = false
end
until (not same) or cty < 0
if sameCounter > 2 then
for k,v in pairs(tempDel.x) do del.x[#del.x + k] = v end
for k,v in pairs(tempDel.y) do del.y[#del.y + k] = v end
end
--down
ctx = tx
cty = ty
same = true
sameCounter = 0
tempDel = {x={}, y={}}
repeat
if tilemap.get(ctx, cty).color == tilemap.get(tx, ty).color then
table.insert(tempDel.x, ctx)
table.insert(tempDel.y, cty)
sameCounter = sameCounter + 1
same = true
cty = cty + 1
else
same = false
end
until (not same) or cty > tilemap.height - 1
if sameCounter > 2 then
for k,v in pairs(tempDel.x) do del.x[#del.x + k] = v end
for k,v in pairs(tempDel.y) do del.y[#del.y + k] = v end
end
--right
ctx = tx
cty = ty
same = true
sameCounter = 0
tempDel = {x={}, y={}}
repeat
if tilemap.get(ctx, cty).color == tilemap.get(tx, ty).color then
table.insert(tempDel.x, ctx)
table.insert(tempDel.y, cty)
sameCounter = sameCounter + 1
same = true
ctx = ctx + 1
else
same = false
end
until (not same) or ctx > tilemap.width - 1
if sameCounter > 2 then
for k,v in pairs(tempDel.x) do del.x[#del.x + k] = v end
for k,v in pairs(tempDel.y) do del.y[#del.y + k] = v end
end
--right
ctx = tx
cty = ty
same = true
sameCounter = 0
tempDel = {x={}, y={}}
repeat
if tilemap.get(ctx, cty).color == tilemap.get(tx, ty).color then
table.insert(tempDel.x, ctx)
table.insert(tempDel.y, cty)
sameCounter = sameCounter + 1
same = true
ctx = ctx - 1
else
same = false
end
until (not same) or ctx < 0
if sameCounter > 2 then
for k,v in pairs(tempDel.x) do del.x[#del.x + k] = v end
for k,v in pairs(tempDel.y) do del.y[#del.y + k] = v end
end
clearMatching(del)--do at end of func
if #del.x > 0 then
return 1
else return 0 end
end
function clearMatching(del)
for i, x in pairs(del.x) do
for j, y in pairs(del.y) do
if tilemap.get(x, y).type ~= 0 then
tilemap.set(x, y, {type=0, color={0, 0, 0}})
game.score = game.score + 1
end
end
end
end
return game