-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstage.lua
More file actions
732 lines (635 loc) · 20.4 KB
/
stage.lua
File metadata and controls
732 lines (635 loc) · 20.4 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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
local stage = {
maxLevel = 10,
levelStrings = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10" },
speeds = { 1000, 793, 618, 473, 355, 262, 190, 135, 94, 64 },
create = function(startMusic, nextSong, stopMusic)
local s = {
startMusic = startMusic,
nextSong = nextSong,
stopMusic = stopMusic,
width = 10,
height = 22,
visibleHeight = 20,
tetromino = nil,
enablePreview = true,
preview = nil,
enableHold = true,
hold = nil,
enableGhost = true,
ghost = nil,
tiles = {},
bag = {},
highScore = 0,
score = 0,
scoreDisplay = 0,
combo = -1,
linesCleared = 0,
fourLinesCleared = 0,
level = 1,
speed = 1000,
lockDelay = 500,
lockTimer = nil,
tickTimer = nil,
holdDelay = 500,
isHardDropping = false,
isWaitingForHoldLock = false,
lastRowCleared = 1,
isGameOver = false,
fillSpeed = 1,
fillTimer = nil,
fillRow = 22,
fillCol = 1,
mode = "dynamic",
levelLabelWidth = Gfx.getTextSize("LEVEL"),
holdLabelWidth = Gfx.getTextSize("HOLD"),
lineClearAnimation = Gfx.imagetable.new('images/clear.gif'),
lineClearAnimationTimer = nil,
lineClearIndex = 1,
bigLineClearAnimation = Gfx.imagetable.new('images/clearbig.gif'),
bigLineClearAnimationTimer = nil,
bigLineClearIndex = 1,
highScoreAnimation = Gfx.imagetable.new('images/highscore.gif'),
highScoreAnimationTimer = nil,
highScoreIndex = 1,
hasReachedHighScore = false,
soundEffects = {
shift = playdate.sound.sampleplayer.new("sounds/shift"),
rotate = playdate.sound.sampleplayer.new("sounds/rotate"),
hold = playdate.sound.sampleplayer.new("sounds/hold"),
softDrop = playdate.sound.sampleplayer.new("sounds/softDrop"),
hardDrop = playdate.sound.sampleplayer.new("sounds/hardDrop"),
collide = playdate.sound.sampleplayer.new("sounds/collide"),
land = playdate.sound.sampleplayer.new("sounds/land"),
lock = playdate.sound.sampleplayer.new("sounds/lock"),
lineClear1 = playdate.sound.sampleplayer.new("sounds/lineClear1"),
lineClear2 = playdate.sound.sampleplayer.new("sounds/lineClear2"),
lineClear3 = playdate.sound.sampleplayer.new("sounds/lineClear3"),
lineClear4 = playdate.sound.sampleplayer.new("sounds/lineClear4"),
gameOver = playdate.sound.sampleplayer.new("sounds/gameOver")
},
setup = function(self)
self.isGameOver = false
self.score = 0
self.scoreDisplay = 0
self.combo = -1
self.linesCleared = 0
self.fourLinesCleared = 0
self.hasReachedHighScore = false
self.tetromino = nil
self.hold = nil
self.preview = nil
self.ghost = nil
if self.mode ~= "chill" then
self:setLevel(1, true)
end
self.tiles = {}
for row = 1, self.height do
self.tiles[row] = {}
for col = 1, self.width do
self.tiles[row][col] = 0
end
end
self:resetBag()
self:startMusic()
self.tickTimer = playdate.timer.new(self.speed, function() self:tick() end)
self.tickTimer.repeats = true
end,
gameOver = function(self)
self.isGameOver = true
self.soundEffects.gameOver:play()
self:stopMusic()
self.tickTimer:remove()
self.tetromino = nil
self.fillRow = 22
self.fillCol = 1
self.fillTimer = playdate.timer.new(self.fillSpeed, function() self:fill() end)
self.fillTimer.repeats = true
end,
setMode = function(self, mode)
if self.mode ~= mode then
self.mode = mode
ModeOption:setValue(mode)
end
end,
setLevel = function(self, level, setManually)
if level ~= self.level then
if self.mode == "chill" and not setManually then
level = self.level
end
if level > Stage.maxLevel then
level = Stage.maxLevel
end
self.level = level
self.speed = Stage.speeds[level]
if self.tickTimer then
self.tickTimer.duration = self.speed
end
if not setManually then
LevelOption:setValue(Stage.levelStrings[level])
end
end
end,
resetBag = function(self)
self.bag = {}
for i = 1, #Tetromino.types do
table.insert(self.bag, Tetromino.types[i])
end
for i = #self.bag, 2, -1 do
local j = math.random(i)
self.bag[i], self.bag[j] = self.bag[j], self.bag[i]
end
end,
chooseFromBag = function(self)
local type = self.bag[1]
table.remove(self.bag, 1)
if #self.bag == 0 then
self:resetBag()
end
return type
end,
spawnTetromino = function(self)
local type = self:chooseFromBag()
local x = 4
local y = 2
if type == "I" then
y = 1
end
self.tetromino = Tetromino.create(type, x, y)
if self.tetromino:checkCollision(self, 0, 0) then
self:gameOver()
else
if self.enableGhost then
self.ghost = Tetromino.create(type, x, y)
self.ghost.isGhost = true
end
if self.enablePreview then
self.preview = Tetromino.create(self.bag[1], 0, 0)
end
self:tick()
end
end,
shiftLeft = function(self)
if self.tetromino then
local success = self.tetromino:moveLeft(self)
if success then
self.soundEffects.shift:play()
else
self.soundEffects.collide:play()
end
end
end,
shiftRight = function(self)
if self.tetromino then
local success = self.tetromino:moveRight(self)
if success then
self.soundEffects.shift:play()
else
self.soundEffects.collide:play()
end
end
end,
rotateClockwise = function(self)
if self.tetromino then
local success = self.tetromino:rotateClockwise(self)
if success then
self.soundEffects.rotate:play()
else
self.soundEffects.collide:play()
end
end
end,
rotateCounterClockwise = function(self)
if self.tetromino then
local success = self.tetromino:rotateCounterClockwise(self)
if success then
self.soundEffects.rotate:play()
else
self.soundEffects.collide:play()
end
end
end,
startSoftDrop = function(self)
if self.tetromino then
self.soundEffects.softDrop:play(0)
self.tickTimer.duration = math.floor(self.speed / 20)
end
end,
stopSoftDrop = function(self)
if self.tetromino then
self.soundEffects.softDrop:stop()
self.tickTimer.duration = self.speed
end
end,
hardDrop = function(self)
if self.tetromino then
self.isHardDropping = true
self:tick()
end
end,
switchHold = function(self)
if self.enableHold and not self.isWaitingForHoldLock and self.tetromino ~= nil then
self.isHardDropping = false
self.soundEffects.hold:play()
if self.hold then
table.insert(self.bag, 1, self.hold.type)
self.isWaitingForHoldLock = true
end
self.hold = self.tetromino
self.hold.rotationIndex = 1
self:spawnTetromino()
end
end,
lock = function(self)
if self.lockTimer then
self.lockTimer:remove()
self.lockTimer = nil
end
if self.tetromino and self.tetromino:checkCollision(self, 0, 1) then
self:tetrominoToTiles()
self.tetromino = nil
local lineCount = self:checkForLines()
self:calculateScore(lineCount)
if self:checkForLockOut() then
self:gameOver()
else
self:checkForSlowDown()
if lineCount == 0 then
if self.isHardDropping then
self.soundEffects.hardDrop:play()
else
self.soundEffects.lock:play()
end
elseif 1 <= lineCount and lineCount <= 4 then
self.soundEffects["lineClear" .. lineCount]:play()
end
end
end
self.isWaitingForHoldLock = false
self.isHardDropping = false
end,
countAirBubbles = function(self)
local airBubbles = 0
local lastAirBubbleRow = 0
local tilesLookedAt = {}
for row = 1, self.height do
local rowTiles = {}
for col = 1, self.width do
table.insert(rowTiles, false)
end
table.insert(tilesLookedAt, rowTiles)
end
local lookAtTile
lookAtTile = function(row, col)
if self.tiles[row][col] == 0 and not tilesLookedAt[row][col] then
tilesLookedAt[row][col] = true
if row - 1 >= 1 then
lookAtTile(row - 1, col)
end
if row + 1 <= self.height then
lookAtTile(row + 1, col)
end
if col - 1 >= 1 then
lookAtTile(row, col - 1)
end
if col + 1 <= self.width then
lookAtTile(row, col + 1)
end
end
end
for row = 1, self.height do
for col = 1, self.width do
if self.tiles[row][col] == 0 and not tilesLookedAt[row][col] then
if row > lastAirBubbleRow then
airBubbles = airBubbles + 1
lastAirBubbleRow = row
end
lookAtTile(row, col)
end
end
end
return airBubbles - 1
end,
checkForSlowDown = function(self)
if self.mode == "dynamic" and self.level > 1 then
local airBubbles = self:countAirBubbles()
local newMaxLevel = Stage.maxLevel - (airBubbles - 1)
if self.level > newMaxLevel then
self:setLevel(self.level - 1)
end
end
end,
checkForLockOut = function(self)
local tilesAboveVisibleStage = false
for row = 1, 2 do
for col = 1, self.width do
if self.tiles[row][col] > 0 then
tilesAboveVisibleStage = true
break
end
end
end
return tilesAboveVisibleStage
end,
checkForLines = function(self)
local lineCount = 0
self.lastRowCleared = 1
for row = 3, self.height do
local isLine = true
for col = 1, self.width do
if self.tiles[row][col] == 0 then
isLine = false
end
end
if isLine then
self:clearLine(row)
lineCount = lineCount + 1
if row > self.lastRowCleared then
self.lastRowCleared = row
end
end
end
return lineCount
end,
clearLine = function(self, y)
for row = y, 2, -1 do
for col = 1, self.width do
self.tiles[row][col] = self.tiles[row - 1][col]
end
end
end,
tetrominoToTiles = function(self)
local pattern = Tetromino.pattern[self.tetromino.type]
local rotation = pattern[self.tetromino.rotationIndex]
for row = 1, #rotation do
for col = 1, #rotation[row] do
if rotation[row][col] > 0 then
local x = self.tetromino.x + col - 1
local y = self.tetromino.y + row - 1
self.tiles[y][x] = rotation[row][col]
end
end
end
end,
calculateScore = function(self, lineCount)
local score = 0
if lineCount == 1 then
score = 100 * self.level
elseif lineCount == 2 then
score = 300 * self.level
elseif lineCount == 3 then
score = 500 * self.level
elseif lineCount >= 4 then
score = 800 * self.level
end
if lineCount >= 1 then
self.combo = self.combo + 1
score = score + (50 * self.combo * self.level)
else
self.combo = -1
end
self.linesCleared = self.linesCleared + lineCount
if self.linesCleared >= 10 then
self.linesCleared = 0
self:setLevel(self.level + 1)
end
if lineCount >= 4 then
self.fourLinesCleared = self.fourLinesCleared + 1
if self.fourLinesCleared >= 2 then
self.fourLinesCleared = 0
self:nextSong()
end
end
if lineCount >= 1 then
self.lineClearIndex = 1
self.lineClearAnimationTimer = playdate.timer.new(25, function() self:nextLineClearFrame() end)
self.lineClearAnimationTimer.repeats = true
end
if lineCount >= 4 then
self.bigLineClearIndex = 1
self.bigLineClearAnimationTimer = playdate.timer.new(30, function() self:nextBigLineClearFrame() end)
self.bigLineClearAnimationTimer.repeats = true
end
self.score = self.score + score
if self.score > self.highScore then
if self.highScoreAnimationTimer == nil and not self.hasReachedHighScore then
self.highScoreIndex = 1
self.highScoreAnimationTimer = playdate.timer.new(30, function() self:nextHighScoreFrame() end)
self.highScoreAnimationTimer.repeats = true
end
self.highScore = self.score
self.hasReachedHighScore = true
end
end,
tick = function(self)
if self.tetromino then
local success = false
if self.isHardDropping then
while self.tetromino:moveDown(self) do
-- nothing, we're hard-dropping until we hit the bottom
end
else
success = self.tetromino:moveDown(self)
end
if success then
if self.lockTimer then
self.lockTimer:remove()
self.lockTimer = nil
end
elseif not self.lockTimer then
local lockDelay = self.lockDelay
if self.isHardDropping then
lockDelay = 1
else
self.soundEffects.land:play()
end
self.lockTimer = playdate.timer.new(lockDelay, function() self:lock() end)
end
else
self:spawnTetromino()
end
end,
fill = function(self)
self.tiles[self.fillRow][self.fillCol] = math.random(1, 7)
if self.fillRow % 2 == 0 then
self.fillCol = self.fillCol + 1
else
self.fillCol = self.fillCol - 1
end
if self.fillCol < 1 or self.fillCol > self.width then
self.fillRow = self.fillRow - 1
if self.fillRow % 2 == 0 then
self.fillCol = 1
else
self.fillCol = self.width
end
if self.fillRow > 2 then
self.soundEffects.collide:play()
end
end
if self.fillRow <= 2 then
self.soundEffects.land:play()
self.fillTimer:remove()
self.fillTimer = nil
end
end,
drawTile = function(self, tx, ty, blockIndex)
local x = tx * Tetromino.minoSize
local y = ty * Tetromino.minoSize
Tetromino.images[blockIndex]:draw(x, y)
end,
draw = function(self)
local displayWidth = self.width * Tetromino.minoSize
local displayHeight = self.visibleHeight * Tetromino.minoSize
if self.score > self.scoreDisplay then
if self.score - self.scoreDisplay >= 1000 then
self.scoreDisplay = math.min(self.score, self.scoreDisplay + 100)
elseif self.score - self.scoreDisplay >= 100 then
self.scoreDisplay = math.min(self.score, self.scoreDisplay + 20)
else
self.scoreDisplay = math.min(self.score, self.scoreDisplay + 10)
end
end
Gfx.drawText("SCORE", displayWidth + 10, displayHeight - 26)
Gfx.drawText(self.scoreDisplay, displayWidth + 10, displayHeight - 10)
Gfx.drawText("LEVEL", -self.levelLabelWidth - 10, displayHeight - 26)
self.levelTextWidth = Gfx.getTextSize(self.level)
Gfx.drawText(self.level, -self.levelTextWidth - 10, displayHeight - 10)
if self.enablePreview then
playdate.graphics.setImageDrawMode(playdate.graphics.kDrawModeFillWhite)
Gfx.drawText("NEXT", displayWidth + 9, -1)
Gfx.drawText("NEXT", displayWidth + 9, 0)
Gfx.drawText("NEXT", displayWidth + 9, 1)
Gfx.drawText("NEXT", displayWidth + 11, -1)
Gfx.drawText("NEXT", displayWidth + 11, 0)
Gfx.drawText("NEXT", displayWidth + 11, 1)
Gfx.drawText("NEXT", displayWidth + 10, -1)
Gfx.drawText("NEXT", displayWidth + 10, 1)
playdate.graphics.setImageDrawMode(playdate.graphics.kDrawModeCopy)
Gfx.drawText("NEXT", displayWidth + 10, 0)
end
if self.enableHold then
playdate.graphics.setImageDrawMode(playdate.graphics.kDrawModeFillWhite)
Gfx.drawText("HOLD", -self.holdLabelWidth - 9, -1)
Gfx.drawText("HOLD", -self.holdLabelWidth - 9, 0)
Gfx.drawText("HOLD", -self.holdLabelWidth - 9, 1)
Gfx.drawText("HOLD", -self.holdLabelWidth - 11, -1)
Gfx.drawText("HOLD", -self.holdLabelWidth - 11, 0)
Gfx.drawText("HOLD", -self.holdLabelWidth - 11, 1)
Gfx.drawText("HOLD", -self.holdLabelWidth - 10, -1)
Gfx.drawText("HOLD", -self.holdLabelWidth - 10, 1)
playdate.graphics.setImageDrawMode(playdate.graphics.kDrawModeCopy)
Gfx.drawText("HOLD", -self.holdLabelWidth - 10, 0)
end
if self.enablePreview and self.preview then
self.preview.x = self.width + 2
self.preview.y = 5
self.preview:draw(true)
end
if self.enableHold and self.hold then
self.hold.x = -Tetromino.width[self.hold.type]
self.hold.y = 5
self.hold:draw(true)
end
Gfx.setColor(Gfx.kColorWhite)
Gfx.fillRect(-3, -3, displayWidth + 5, displayHeight + 5)
Gfx.setColor(Gfx.kColorBlack)
Gfx.drawRect(-2, -2, displayWidth + 3, displayHeight + 3)
Gfx.setClipRect(0, 0, displayWidth, displayHeight)
if self.tetromino then
if self.ghost and self.enableGhost then
self.ghost.x = self.tetromino.x
self.ghost.y = self.tetromino.y
self.ghost.rotationIndex = self.tetromino.rotationIndex
while self.ghost:moveDown(self) do end
self.ghost:draw()
end
self.tetromino:draw()
end
for row = 1, self.height do
for col = 1, self.width do
if self.tiles[row][col] > 0 then
local x = col - 1
local y = row - 3
self:drawTile(x, y, self.tiles[row][col])
end
end
end
playdate.graphics.clearClipRect()
if self.lineClearAnimationTimer ~= nil then
local lineClearImage = self.lineClearAnimation:getImage(self.lineClearIndex)
local lineClearY = (self.lastRowCleared - 3) * Tetromino.minoSize - 5
lineClearImage:draw(-13, lineClearY)
end
if self.bigLineClearAnimationTimer ~= nil then
local bigLineClearImage = self.bigLineClearAnimation:getImage(self.bigLineClearIndex)
local bigLineClearY = (self.lastRowCleared - 3) * Tetromino.minoSize - 10
bigLineClearImage:draw(-13, bigLineClearY)
end
if self.highScoreAnimationTimer ~= nil then
local highScoreImage = self.highScoreAnimation:getImage(self.highScoreIndex)
local scoreWidth = playdate.graphics.getTextSize(self.score)
highScoreImage:draw(displayWidth + scoreWidth + 12, displayHeight - 17)
elseif self.hasReachedHighScore then
local highScoreImage = self.highScoreAnimation:getImage(4)
local scoreWidth = playdate.graphics.getTextSize(self.score)
highScoreImage:draw(displayWidth + scoreWidth + 12, displayHeight - 17)
end
if self.isGameOver then
local highScoreText = "HIGH SCORE\n" .. self.highScore
local highScoreW, highScoreH = playdate.graphics.getTextSize(highScoreText, nil, 4)
local highScoreX = displayWidth / 2 - math.floor(highScoreW / 2)
local highScoreY = displayHeight / 2 - math.floor(highScoreH / 2)
playdate.graphics.setColor(playdate.graphics.kColorWhite)
playdate.graphics.fillRoundRect(highScoreX - 4, highScoreY - 4, highScoreW + 8, highScoreH + 8, 4)
Gfx.drawTextAligned(highScoreText, displayWidth / 2, highScoreY, kTextAlignment.center, 4)
end
end,
nextLineClearFrame = function(self)
self.lineClearIndex = self.lineClearIndex + 1
if self.lineClearIndex >= self.lineClearAnimation:getLength() then
self.lineClearAnimationTimer:remove()
self.lineClearAnimationTimer = nil
end
end,
nextBigLineClearFrame = function(self)
self.bigLineClearIndex = self.bigLineClearIndex + 1
if self.bigLineClearIndex >= self.bigLineClearAnimation:getLength() then
self.bigLineClearAnimationTimer:remove()
self.bigLineClearAnimationTimer = nil
end
end,
nextHighScoreFrame = function(self)
self.highScoreIndex = self.highScoreIndex + 1
if self.highScoreIndex >= self.highScoreAnimation:getLength() then
self.highScoreAnimationTimer:remove()
self.highScoreAnimationTimer = nil
end
end,
saveData = function(self)
playdate.datastore.write({
highScore = self.highScore,
mode = self.mode,
level = self.level
})
end,
loadData = function(self)
local data = playdate.datastore.read()
if data ~= nil then
if data.highScore ~= nil then
self.highScore = data.highScore
end
if data.mode ~= nil then
self:setMode(data.mode)
end
if data.level ~= nil and data.mode == "chill" then
self:setLevel(data.level, true)
LevelOption:setValue(Stage.levelStrings[data.level])
end
end
end,
}
s:setup()
return s
end
}
return stage