-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.lua
304 lines (242 loc) · 7.51 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
local composer = require("composer")
local scene = composer.newScene()
local physics = require("physics")
local h = require("lib.helper")
local m = require("lib.mydata")
physics.start()
---------------------------------------------------------------------------------
-- FUNCTIONS
local screenTouched
local update
local getDeltaTime
local onCollision
local gameOver
-- TIME
local lastUpdate = 0
local dt = 0
-- GAME STARTED AND ENDED
local gameStarted = false
local death = false
-- TEXT
local startTextTimer
local startText
local scoreText
-- PLAYER
local rect
local indicator
local maxX = 220
local maxUp = 290
local accelX = 7
local gravityMax = 3
-- TOUCHES
local leftId
local rightId
local touchDirection
-- PLATFORMS
local platformSpeed = 0.75
local platformSpeedMax = 2.75
local platformTimer = 0
local platformTimerMax = 5.3
local platformTimerMin = 1.3
local platformGroup = display.newGroup()
local platforms = {}
function getDeltaTime()
if lastUpdate == 0 then
dt = 0
else
dt = (system.getTimer() - lastUpdate) / 1000
end
lastUpdate = system.getTimer()
if dt > 0.02 then
dt = 0.02
end
end
function update()
getDeltaTime()
-- PLAYER MOVEMENT
local vx, vy = rect:getLinearVelocity()
local newVx, newVy = 0,0
if touchDirection == "left" and leftId then
newVx = vx - accelX
newVy = vy - 50
elseif touchDirection == "right" and rightId then
newVx = vx + accelX
newVy = vy - 50
end
newVx = h.clamp(newVx, -maxX, maxX)
newVy = h.clamp(newVy, -maxUp, 10000)
if touchDirection then
rect:setLinearVelocity(newVx, newVy)
end
if rect.x > display.contentWidth then
rect.x = 0
end
if rect.x < 0 then
rect.x = display.contentWidth
end
rect.y = h.clamp(rect.y, -25, display.contentHeight + rect.height + 100)
if rect.y >= (display.contentHeight + rect.height) then
gameOver()
end
if rect.y < 0 then
indicator.isVisible = true
scoreText:setFillColor(0.3, 0.3, 0.3)
else
indicator.isVisible = false
scoreText:setFillColor(1, 0, 0)
end
-- INDICATOR
indicator.x = rect.x
-- OBSTACLES
if gameStarted then
platformTimer = platformTimer - dt
if platformTimer <= 0 then
platformTimer = platformTimerMax
local platform = platforms[#platforms]
platform.isVisible = true
platforms[#platforms] = nil
table.insert(platforms, 1, platform)
platform.x = math.random(platform.width/2, display.contentWidth-(platform.width/2))
platform.y = display.contentHeight + 100
end
for _, platform in pairs(platforms) do
if platform.isVisible then
platform.randomSin = platform.randomSin + (platform.randomDir * 0.055)
platform.x = platform.x + (math.sin(platform.randomSin) * 2.5)
platform.y = platform.y - platformSpeed
if platform.y <= 0 and platform.isVisible then
platform.x, platform.y = 10000, 10000
platformSpeed = platformSpeed + 0.15
if platformSpeed >= platformSpeedMax then platformSpeed = platformSpeedMax end
platformTimerMax = platformTimerMax - 0.2
if platformTimerMax <= platformTimerMin then platformTimerMax = platformTimerMin end
platform.isVisible = false
if rect.y > 0 then
m.score = m.score + 1
scoreText.text = m.score
end
end
end
end
end
end
function screenTouched(event)
if event.phase == "began" or event.phase == "moved" then
if not gameStarted then
gameStarted = true
startText.isVisible = false
timer.cancel(startTextTimer)
rect.gravityScale = gravityMax
end
if event.x < display.contentCenterX then
touchDirection = "left"
leftId = event.id
else
touchDirection = "right"
rightId = event.id
end
elseif event.phase == "ended" then
if event.id == leftId then leftId = nil end
if event.id == rightId then rightId = nil end
touchDirection = nil
end
end
function gameOver()
if not death then
death = true
m.setBestScore()
composer.gotoScene("retry", {effect="fade", time=100})
end
end
function onCollision(self, e)
if e.phase == "began" then
gameOver()
end
end
function scene:create(event)
local sceneGroup = self.view
-- START TEXT
startText = display.newText({
text = "TOUCH TO START",
x = display.contentCenterX,
y = display.contentCenterY,
width = display.contentWidth,
fontSize = 35,
font = native.systemFont,
align = "center"
})
startTextTimer = timer.performWithDelay(1000, function()
if startText.isVisible then
startText.isVisible = false
else
startText.isVisible = true
end
end, 0)
-- SCORE TEXT
m.score = 0
scoreText = display.newText({
text = m.score,
x = display.contentCenterX + 15,
y = 30,
width = display.contentWidth,
fontSize = 24,
font = native.systemFont,
align = "left"
})
sceneGroup:insert(scoreText)
scoreText:setFillColor(1,0,0)
-- OUT OF BOUNDS INDICATOR
indicator = display.newRect(sceneGroup, 0, 0, 5, 10)
indicator.isVisible = false
-- PLAYER
rect = display.newRect(sceneGroup, display.contentCenterX, display.contentCenterY-100, 20, 20)
rect:setFillColor(1,1,0)
physics.addBody(rect, "dynamic", {})
rect.collision = onCollision
rect:addEventListener("collision", rect)
rect.gravityScale = 0
rect.isFixedRotation = true
-- PLATFORMS
for i=1, 12, 1 do
local platform = display.newRect(sceneGroup, 10000, 1000, 75, 10)
platform.randomSin = math.random(-1, 1)
platform.randomDir = h.randomSign()
physics.addBody(platform, "static")
platform.isVisible = false
platforms[#platforms+1] = platform
end
end
function scene:show(event)
local sceneGroup = self.view
local phase = event.phase
if phase == "will" then
Runtime:addEventListener("enterFrame", update)
elseif phase == "did" then
end
end
function scene:hide(event)
local sceneGroup = self.view
local phase = event.phase
if event.phase == "will" then
Runtime:removeEventListener("enterFrame", update)
Runtime:removeEventListener("touch", screenTouched)
Runtime:removeEventListener("collision", onCollision)
elseif phase == "did" then
end
end
function scene:destroy(event)
local sceneGroup = self.view
-- Called prior to the removal of scene"s "view" (sceneGroup)
--
-- INSERT code here to cleanup the scene
-- e.g. remove display objects, remove touch listeners, save state, etc
end
---------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener("create", scene)
scene:addEventListener("show", scene)
scene:addEventListener("hide", scene)
scene:addEventListener("destroy", scene)
Runtime:addEventListener("touch", screenTouched)
---------------------------------------------------------------------------------
return scene