-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.lua
512 lines (450 loc) · 10.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
--[[
Squaricles
Copyright (C) 2017 ITR
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
]]--
require ".helperfunctions"
require ".rules"
require ".board"
require ".input"
Game = {}
function Game:new(canvas, input, options)
local pieces = {}
for i=0,#PREVIEWS do
pieces[i] = randomSquindice(DEFAULT_AMOUNT_OF_COLORS)
end
pieces[0].y = pieces[0].y + 1
local o = {
canvas = canvas,
input = input,
pieces = pieces,
board = Board:new(),
width = canvas:getWidth(),
height = canvas:getHeight(),
colors = DEFAULT_AMOUNT_OF_COLORS,
gravity = DEFAULT_GRAVITY * math.pow(0.8, 0),
level = 0,
timer = 4,
restTimer = 0,
placedPiece = false,
squares = {timer = 0},
mode = options['mode'],
draw_symbol = options['draw_symbol'],
hd_cooldown = HD_COOLDOWN_TIME,
pause = false,
pauseTimer = 0.,
clears = 0,
score = 0,
combo = 0,
comboscore = 0,
special = 0,
lost = false,
lostAnim = 0.,
}
for k,v in pairs(clone(self)) do o[k] = v end
o:checkGhost()
Stats:add("Times Played")
Stats:add("Times Played "..options.mode)
return o
end
function Game:draw()
love.graphics.setCanvas(self.canvas)
local w = self.width/self.board.width
local h = self.height/self.board.height
love.graphics.clear(COLORS["background"])
if not (self.pause or self.pauseTimer > 0) then
self:drawBoard(w,h)
self:drawSquares(w,h)
if self.ghost then self:drawPiece(self.pieces[0],w,h,true) end
self:drawPiece(self.pieces[0],w,h)
end
love.graphics.setCanvas()
end
function Game:drawBoard(w,h)
for i=1,self.board.height do
local y = (i-1)*h + 4
for j=1,self.board.width do
local x = (j-1)*w + 4
if self.lostAnim > i then
setColor('locked')
love.graphics.rectangle('fill',x,y,w-8,h-8)
else
setColor(self.board[i][j])
love.graphics.rectangle('fill',x,y,w-8,h-8)
if self.board[i][j]>0 and self.draw_symbol then
setColor(self.board[i][j],false,true)
love.graphics.polygon(
'fill',
polyregular(
self.board[i][j]%3+3,
x+w/2,y+h/2,
w*1/3,h*1/3
)
)
end
end
end
end
end
function Game:drawSquares(w,h)
local alpha = 255*self.squares.timer/SQUARE_REMOVAL_TIME
for k=1,#self.squares do
love.graphics.setColor(255,255,255,alpha)
local square = self.squares[k]
local x0,y0 = square.x,square.y
local dx,dy = 0,square.size
for j=1,4 do
local x,y = (x0+dx-1)*w+4,(y0+dy-1)*h+4
love.graphics.rectangle('fill',x,y,w-8,h-8)
dx,dy = dy,square.size-dx
end
end
end
function Game:drawPiece(piece, w,h, ghost)
local x0,y0 = piece.x,piece.y
if y0<1 then return end
for j=1,2 do
local y0 = y0
if ghost then
while self.board[y0+1][x0-j+1] == 0 do y0 = y0+1 end
if piece.shape[1][j] ==0 then
y0 = y0+1
end
end
for i=1,2 do
local x,y = (x0-j)*w,(y0-i)*h
local c = piece.shape[i][j]
if c ~= 0 then
setColor("outline",ghost)
love.graphics.rectangle(
'fill',
x-4,y-4,
w+8,h+8
)
setColor(c,ghost)
love.graphics.rectangle(
'fill',
x+4,y+4,
w-8,h-8
)
if self.draw_symbol then
setColor(c,ghost,true)
love.graphics.polygon(
'fill',
polyregular(
c%3+3,
x+w/2,y+h/2,
w*1/3,h*1/3
)
)
end
end
end
end
local x,y = (x0-1)*w,(y0-1)*h
if not ghost then
love.graphics.setColor(0,0,0,255)
love.graphics.rectangle('fill',x-16,y-4,32,8)
love.graphics.rectangle('fill',x-4,y-16,8,32)
end
end
function Game:update(dt)
if self.lost then
self.lostAnim = self.lostAnim + dt*LOSE_ANIM_SPEED;
return
end
if self.pause or self.pauseTimer > 0 then
self.pauseTimer = self.pauseTimer - dt
self.input:useInput(function(key)
self:togglePause()
end)
return
end
if #self.squares ~= 0 then
self:squareRemovalUpdate(dt)
else
self:endCombo()
self:playUpdate(dt)
end
end
function Game:squareRemovalUpdate(dt)
self.input:useInput(function(key)
if key~=UP and self:parseInput(key) then
self.restTimer = 0
end
end)
self.squares.timer = self.squares.timer + dt
if self.squares.timer >= SQUARE_REMOVAL_TIME then
self:addScore(self.squares)
local newTime = self.squares.timer - SQUARE_REMOVAL_TIME
self.board:removeSquares(self.squares)
self.board:gravBoard()
self.squares = self.board:findSquares()
self:checkSpecial(self.squares)
self.squares.timer = newTime
end
end
function Game:playUpdate(dt)
self:checkGravity(dt)
self.hd_cooldown = self.hd_cooldown - dt
self.input:useInput(function(key)
if self.hd_cooldown > 0 then
if key == UP then
return
else
self.hd_cooldown = 0
end
end
if self:parseInput(key) then
self.restTimer = 0
end
end)
if self.placedPiece then
self.placedPiece = false
self.board:gravBoard()
self.squares = self.board:findSquares()
self:checkSpecial(self.squares)
if #self.squares>0 then
self:addClear()
end
self.squares.timer = 0
end
end
function Game:checkGravity(dt)
if self.pieces[0]:isBlocked(0,1,self.board) then
self.restTimer = self.restTimer + dt
if self.restTimer >= REST_TIME then
self:place()
end
else
if self.gravity>0 then
self.timer = self.timer - dt
while self.timer <= 0 do
self:movePiece(0,1)
self.timer = self.timer + self.gravity
end
else
while self:movePiece(0,1) do end
end
end
end
function Game:checkSpecial(squares)
for i = 1,#self.squares do
local l = #self.squares
self.special = self.special
+ REMOVE_COLOR_INC[self.squares[i].size]
if self.special >= REMOVE_COLOR_COST then
self.special = self.special - REMOVE_COLOR_COST
local found = self.board:findColor(
self.squares[i].color
)
for j = 1,#found do
self.squares[j+l] = found[j]
end
end
end
end
function Game:place()
local piece = self.pieces[0]
local x0,y0 = piece.x,piece.y
self.lost = true
for i=1,2 do
for j=1,2 do
local c = piece.shape[i][j]
if c ~= 0 then
self.board[y0-i+1][x0-j+1] = c
if y0-i+1>0 then
self.lost = false
elseif self.board[1][x0-j+1] == 0 then
self.lost = false
end
end
end
end
self.placedPiece = true
self.hd_cooldown = HD_COOLDOWN_TIME
for i=1,#self.pieces do
self.pieces[i-1] = self.pieces[i]
end
self.pieces[#self.pieces] = randomSquindice(self.colors)
self.restTimer = 0
self.timer = IDLE_TIME
self:checkGhost()
end
function Game:parseInput(key)
actions = {
[UP ] = function()
return self:hardDrop()
end,
[DOWN ] = function()
return self:movePiece(0,1)
end,
[LEFT ] = function()
return self:movePiece(-1,0)
end,
[RIGHT] = function()
return self:movePiece(1,0)
end,
[A ] = function()
return self:rotate(true)
end,
[B ] = function()
return self:rotate(false)
end,
[START] = function()
return self:togglePause()
end,
}
return actions[key]()
end
function Game:hardDrop()
while self:movePiece(0,1) do end
self:place()
return true
end
function Game:movePiece(dx, dy)
local piece = self.pieces[0]
if piece:isBlocked(dx,dy,self.board) then
local shape = piece.shape
if dy == 1 and shape[1][1]==0 and shape[1][2]==0 then
shape[1], shape[2] = shape[2], shape[1]
piece.y = piece.y-1
end
if dx == -1 and shape[1][1]==0 and shape[2][1]==0 then
shape[1][1], shape[1][2] = shape[1][2], shape[1][1]
shape[2][1], shape[2][2] = shape[2][2], shape[2][1]
piece.x = piece.x-1
elseif dx == 1 and shape[1][2]==0 and shape[2][2]==0 then
shape[1][1], shape[1][2] = shape[1][2], shape[1][1]
shape[2][1], shape[2][2] = shape[2][2], shape[2][1]
piece.x = piece.x+1
end
return false
end
piece.x = piece.x + dx
piece.y = piece.y + dy
self:checkGhost()
return true
end
function Game:addScore(squares)
local score = 0
local clears = 0
for i=1,#squares do
if squares[i].size>0 then
clears = clears + 1
score = score + squares[i].size*100
Stats:max("Biggest Square",squares[i].size)
else
score = score + 10
end
end
self.comboscore = self.comboscore+score
self.combo = self.combo + clears
self.score = self.score + score
Stats:max("Biggest Clear",clears)
Stats:max("Longest Combo",self.combo)
Stats:max("Biggest Combo",self.comboscore)
end
function Game:endCombo()
if self.combo>2 then
self.score = self.score + (self.combo-2)*50
elseif self.combo==2 then
self.score = self.score + 25
end
self.combo = 0
self.comboscore = 0
end
local lLimit = { -- Levels needed to reach next step
{ 8,10, 4}, -- 80 32
{ 16, 5, 2}, -- 40 16
{ 23, 2, 1}, -- 14 07
{ 32,10, 6}, -- 90 54
{ 44, 7, 3}, -- 84 26
{ 55, 4, 1.5}, -- 44 16.5
{ 75,10, 6}, -- 200 100
{ 95, 6, 3}, -- 120 60
{105,10,08}, -- 100 80
}
function Game:addClear()
self.clears = self.clears + 1
Stats:max("Max Clears",self.clears)
Stats:add("Total Clears")
local levelUp = self.clears%20
for i=1,#lLimit do
if self.level<lLimit[i][1] then
levelUp = false
if self.mode=="TRIPLE" then
if self.clears>=lLimit[i][3] then
levelUp = true
self.clears = self.clears - lLimit[i][3]
end
else
if self.clears>=lLimit[i][2] then
levelUp = true
self.clears = self.clears - lLimit[i][2]
end
end
break
end
end
if levelUp then
self.level = self.level + 1
local danger = self.level
if danger > 160 then
danger = -1
self.colors = 6
elseif danger > 110 then
danger = danger-110
danger = danger/2
self.colors = 6
elseif danger > 90 then
danger = -1
self.colors = 5
elseif danger > 70 then
danger = danger-70
self.colors = 5
elseif danger > 50 then
danger = -1
self.colors = 4
elseif danger > 40 then
danger = danger-40
danger = danger*2
self.colors = 4
elseif danger > 25 then
danger = -1
self.colors = 3
else
self.colors = 3
end
if danger < 0 then
self.gravity = 0
else
self.gravity = DEFAULT_GRAVITY * math.pow(0.8, danger)
end
end
end
function Game:rotate(clockwise)
local success = self.pieces[0]:doRotate(clockwise,self.board)
self:checkGhost()
return success
end
function Game:checkGhost()
if self.pieces[0]:isBlocked(0,2,self.board) then
self.ghost = false
else
self.ghost = true
end
end
function Game:togglePause()
self.pause = not self.pause
self.pauseTimer = 0
end