-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.lua
More file actions
163 lines (132 loc) · 3.68 KB
/
main.lua
File metadata and controls
163 lines (132 loc) · 3.68 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
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"
Gfx = playdate.graphics
Tetromino = import "tetromino"
Stage = import "stage"
Scene = import "scene"
local currentStage
local buttonRepeatTimerLeft = nil
local buttonRepeatTimerRight = nil
local upHoldTimer = nil
local defaultFont
local scene = {}
local songs = {}
local songIndex = 1
local songNames = {
"something_in_the_air",
"glad_to_be_stuck_inside",
"mundane",
"pretty_little_lies",
"shut_up_or_shut_in",
"yesterday",
"whatever",
}
local startMusic = function()
songIndex = 1
songs[songIndex]:play(0)
end
local nextSong = function()
songs[songIndex]:stop()
songIndex = songIndex + 1
if songIndex > #songs then
songIndex = 1
end
songs[songIndex]:play(0)
end
local stopMusic = function()
songs[songIndex]:stop()
end
local setup = function()
math.randomseed(playdate.getSecondsSinceEpoch())
defaultFont = Gfx.font.new("fonts/krull")
Gfx.setFont(defaultFont)
scene = Scene.create()
scene:setup()
for i, songName in pairs(songNames) do
songs[i] = playdate.sound.fileplayer.new("music/" .. songName)
songs[i]:setStopOnUnderrun(false)
songs[i]:setVolume(0.5)
end
Tetromino:loadImages()
currentStage = Stage.create(startMusic, nextSong, stopMusic)
local width, height = playdate.display.getSize()
local stageWidth = (currentStage.width * Tetromino.minoSize)
local stageHeight = (currentStage.visibleHeight * Tetromino.minoSize)
local offsetX = math.floor((width - stageWidth) / 2)
local offsetY = math.floor((height - stageHeight) / 2)
Gfx.setDrawOffset(offsetX, offsetY)
local menu = playdate.getSystemMenu()
ModeOption = menu:addOptionsMenuItem("mode", { "regular", "dynamic", "chill" }, currentStage.mode, function(option)
currentStage:setMode(option)
end)
LevelOption = menu:addOptionsMenuItem("level", Stage.levelStrings, Stage.levelStrings[1], function(option)
currentStage:setLevel(math.tointeger(option), true)
end)
GhostOption = menu:addCheckmarkMenuItem("ghost", currentStage.enableGhost, function(value)
currentStage.enableGhost = value
end)
end
setup()
currentStage:loadData()
playdate.AButtonDown = function()
if currentStage.isGameOver and not currentStage.fillTimer then
currentStage:setup()
else
currentStage:rotateClockwise()
end
end
playdate.BButtonDown = function()
currentStage:rotateCounterClockwise()
end
playdate.upButtonDown = function()
if currentStage.enableHold then
if not currentStage.isWaitingForHoldLock then
currentStage.tickTimer:pause()
end
upHoldTimer = playdate.timer.performAfterDelay(currentStage.holdDelay, function()
currentStage:switchHold()
end)
else
currentStage:hardDrop()
end
end
playdate.upButtonUp = function()
currentStage.tickTimer._lastTime = nil
currentStage.tickTimer:start()
if currentStage.enableHold and upHoldTimer.timeLeft > 0 then
upHoldTimer:remove()
currentStage:hardDrop()
end
end
playdate.downButtonDown = function()
currentStage:startSoftDrop()
end
playdate.downButtonUp = function()
currentStage:stopSoftDrop()
end
playdate.leftButtonDown = function()
buttonRepeatTimerLeft = playdate.timer.keyRepeatTimer(function() currentStage:shiftLeft() end)
end
playdate.leftButtonUp = function()
buttonRepeatTimerLeft:remove()
end
playdate.rightButtonDown = function()
buttonRepeatTimerRight = playdate.timer.keyRepeatTimer(function() currentStage:shiftRight() end)
end
playdate.rightButtonUp = function()
buttonRepeatTimerRight:remove()
end
playdate.update = function()
Gfx.clear()
scene:draw()
currentStage:draw()
playdate.timer.updateTimers()
end
playdate.gameWillTerminate = function()
currentStage:saveData()
end
playdate.deviceWillSleep = function()
currentStage:saveData()
end