-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgame.lua
More file actions
414 lines (358 loc) · 11.4 KB
/
game.lua
File metadata and controls
414 lines (358 loc) · 11.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
local Game = {}
local Vector = require("lib.vector")
local Settings = require("settings")
local Powerups = require("powerups")
local Sound = require("sound")
local Colors = require("colors")
local MathUtils = require("math_utils")
local GameState = require("gamestate")
local PowerupsManager = require("powerups_manager")
local LevelsSelector = require("levels_selector")
local PlayerProgress = require("player_progress")
Game.jumpPings = {}
Game.circles = {}
Game.playerCircle = nil
Game.particles = {}
local circles
local particles
local circleAddDist
local lastCircle
local playerCircle
local ticks
local difficulty
local baseDifficulty
local baseScrollSpeed = 0.08
local isAttractMode = false
local minCircleDist = Settings.INTERNAL_HEIGHT / 4
local trigCache = {}
local TRIG_CACHE_SIZE = 3600
local function getCachedTrig(angle)
local normalizedAngle = angle % (math.pi * 2)
if normalizedAngle < 0 then
normalizedAngle = normalizedAngle + (math.pi * 2)
end
local key = math.floor(normalizedAngle * TRIG_CACHE_SIZE / (math.pi * 2))
if not trigCache[key] then
trigCache[key] = {
cos = math.cos(angle),
sin = math.sin(angle),
}
end
return trigCache[key]
end
local function vec(x, y)
return Vector:new(x, y)
end
local function remove(tbl, predicate)
local i = #tbl
while i >= 1 do
if predicate(tbl[i]) then
table.remove(tbl, i)
end
i = i - 1
end
end
function Game.init(is_attract_mode, initial_difficulty)
circles = {}
particles = {}
circleAddDist = 0
lastCircle = nil
playerCircle = nil
ticks = 0
difficulty = initial_difficulty or 1
baseDifficulty = initial_difficulty or 1
isAttractMode = is_attract_mode
end
function Game.get_circles()
return circles
end
function Game.get_particles()
return particles
end
function Game.get_player_circle()
return playerCircle
end
function Game.set_player_circle(circle)
playerCircle = circle
end
local function addCircle(isSlowed)
local radius = MathUtils.rnd(20, 30)
local yPos = -radius
if playerCircle then
yPos = math.min(yPos, playerCircle.position.y - minCircleDist)
end
local newCircle = {
position = vec(MathUtils.rnd(15, Settings.INTERNAL_WIDTH - 15), yPos),
radius = radius,
obstacleCount = MathUtils.rndi(1, 3),
angle = MathUtils.rnd(math.pi * 2),
angularVelocity = MathUtils.rnds(0.005, 0.015) * difficulty,
obstacleLength = MathUtils.rnd(15, 25),
next = nil,
isPassed = false,
}
if isSlowed then
newCircle.angularVelocity = MathUtils.rnds(0.005, 0.015)
newCircle.obstacleLength = newCircle.obstacleLength * 0.5
end
if lastCircle ~= nil then
lastCircle.next = newCircle
end
if playerCircle == nil then
playerCircle = newCircle
end
lastCircle = newCircle
table.insert(circles, newCircle)
end
local function updateDifficulty()
local ticksPerUnit = 3600
local exponent = 1.25
local scaleFactor = 1.5
local timeUnits = ticks / ticksPerUnit
difficulty = baseDifficulty + (timeUnits ^ exponent) * scaleFactor
end
function Game.update(dt, PowerupsManager, endGame, addScore)
ticks = ticks + 1
updateDifficulty()
if ticks == 1 then
addCircle(PowerupsManager.isSlowed)
end
if circleAddDist <= 0 then
addCircle(PowerupsManager.isSlowed)
circleAddDist = circleAddDist + MathUtils.rnd(Settings.INTERNAL_HEIGHT * 0.25, Settings.INTERNAL_HEIGHT * 0.45)
end
local baseSpeedForScore = difficulty * baseScrollSpeed
if playerCircle then
local playerY = playerCircle.position.y
if playerY < (Settings.INTERNAL_HEIGHT / 2) then
baseSpeedForScore = baseSpeedForScore + ((Settings.INTERNAL_HEIGHT / 2) - playerY) * 0.02
end
end
local scrollSpeed = baseSpeedForScore
if PowerupsManager.isSlowed then
local playerY = playerCircle and playerCircle.position.y or 0
if playerY < (Settings.INTERNAL_HEIGHT * 0.2) then
scrollSpeed = baseScrollSpeed + ((Settings.INTERNAL_HEIGHT / 2) - playerY) * 0.02
elseif playerY > (Settings.INTERNAL_HEIGHT * 0.5) and playerY < (Settings.INTERNAL_HEIGHT * 0.8) then
scrollSpeed = baseScrollSpeed
elseif playerY >= (Settings.INTERNAL_HEIGHT * 0.8) then
scrollSpeed = baseScrollSpeed * 0.10
end
end
circleAddDist = circleAddDist - scrollSpeed
addScore(baseSpeedForScore)
if playerCircle and playerCircle.position.y > Settings.INTERNAL_HEIGHT - 1 then
if not isAttractMode then
if PowerupsManager.isBoltActive and playerCircle.next then
if Powerups.checkLightningCollision(playerCircle) then
Sound.play("teleport")
particle(playerCircle.position, 20, 3, 0, math.pi * 2, Colors.yellow)
particle(playerCircle.next.position, 20, 3, 0, math.pi * 2, Colors.yellow)
playerCircle.isPassed = true
playerCircle = playerCircle.next
return
end
end
Sound.play("explosion")
end
endGame()
return
end
if PowerupsManager.isBoltActive and playerCircle and playerCircle.next then
if Powerups.checkLightningCollision(playerCircle) then
if not isAttractMode then
Sound.play("teleport")
end
particle(playerCircle.position, 20, 3, 0, math.pi * 2, Colors.yellow)
particle(playerCircle.next.position, 20, 3, 0, math.pi * 2, Colors.yellow)
playerCircle.isPassed = true
playerCircle = playerCircle.next
end
end
local obstacles = {}
remove(circles, function(circle)
circle.position.y = circle.position.y + scrollSpeed
if circle.position.y > Settings.INTERNAL_HEIGHT + circle.radius then
return true
end
circle.angle = circle.angle + circle.angularVelocity
for i = 1, circle.obstacleCount do
local obstacleAngle = circle.angle + (i * math.pi * 2) / circle.obstacleCount
local rectCenter = vec(circle.position.x, circle.position.y):addWithAngle(obstacleAngle, circle.radius)
table.insert(obstacles, {
center = rectCenter,
width = circle.obstacleLength,
height = 3,
angle = obstacleAngle + math.pi / 2,
})
end
return false
end)
remove(particles, function(p)
p.pos:add(p.vel)
p.life = p.life - 0.4
return p.life <= 0
end)
return obstacles
end
function Game.lineAABBIntersect(x1, y1, x2, y2, minX, minY, maxX, maxY)
local dx = x2 - x1
local dy = y2 - y1
if math.abs(dx) < 1e-8 and math.abs(dy) < 1e-8 then
return x1 >= minX and x1 <= maxX and y1 >= minY and y1 <= maxY
end
local t1, t2 = 0, 1
if math.abs(dx) > 1e-8 then
local invDx = 1 / dx
local tx1 = (minX - x1) * invDx
local tx2 = (maxX - x1) * invDx
t1 = math.max(t1, math.min(tx1, tx2))
t2 = math.min(t2, math.max(tx1, tx2))
else
if x1 < minX or x1 > maxX then
return false
end
end
if math.abs(dy) > 1e-8 then
local invDy = 1 / dy
local ty1 = (minY - y1) * invDy
local ty2 = (maxY - y1) * invDy
t1 = math.max(t1, math.min(ty1, ty2))
t2 = math.min(t2, math.max(ty1, ty2))
else
if y1 < minY or y1 > maxY then
return false
end
end
return t1 <= t2
end
function Game.checkLineRotatedRectCollision(lineP1, lineP2, rectCenter, rectWidth, rectHeight, rectAngle)
local trig = getCachedTrig(-rectAngle)
local cosAngle = trig.cos
local sinAngle = trig.sin
local dx1 = lineP1.x - rectCenter.x
local dy1 = lineP1.y - rectCenter.y
local dx2 = lineP2.x - rectCenter.x
local dy2 = lineP2.y - rectCenter.y
local localP1x = dx1 * cosAngle - dy1 * sinAngle
local localP1y = dx1 * sinAngle + dy1 * cosAngle
local localP2x = dx2 * cosAngle - dy2 * sinAngle
local localP2y = dx2 * sinAngle + dy2 * cosAngle
local halfW = rectWidth * 0.5
local halfH = rectHeight * 0.5
return Game.lineAABBIntersect(localP1x, localP1y, localP2x, localP2y, -halfW, -halfH, halfW, halfH)
end
local function getBlipColor(currentLevelData, blip_counter)
if
currentLevelData
and currentLevelData.winCondition.type == "blips"
and blip_counter.value == currentLevelData.winCondition.value - 1
then
return currentLevelData.winCondition.finalBlipColor
else
return PowerupsManager.getPingColor()
end
end
function Game.handleSuccessfulBlip(blipType, params)
blipType = blipType or "manual" -- "manual", "phase_shift", "bolt"
local playerCircle = Game.get_player_circle()
if not playerCircle or not playerCircle.next then
return
end
local blipColor = getBlipColor(params.currentLevelData, params.blip_counter)
if blipType == "phase_shift" or blipType == "bolt" then
if not GameState.isAttractMode then
Sound.play("teleport")
end
particle(playerCircle.position, 20, 3, 0, math.pi * 2, blipColor)
particle(playerCircle.next.position, 20, 3, 0, math.pi * 2, blipColor)
else -- "manual"
if not GameState.isAttractMode then
Sound.play("blip")
end
local currentPos = playerCircle.position:copy()
-- Divide the distance between player and next point into 10 equal pieces
local stepVector = (Vector:new(playerCircle.next.position.x, playerCircle.next.position.y)
:sub(playerCircle.position)):div(10)
local particleAngle = stepVector:angle()
for _ = 1, 10 do
particle(currentPos, 4, 2, particleAngle + math.pi, 0.5, blipColor)
currentPos:add(stepVector)
end
end
if blipType ~= "bolt" then
params.setFlashLine({
p1 = playerCircle.position:copy(),
p2 = playerCircle.next.position:copy(),
timer = 2,
})
end
playerCircle.isPassed = true
Game.set_player_circle(playerCircle.next)
params.blip_counter.value = params.blip_counter.value + 1
if
params.currentLevelData
and params.currentLevelData.winCondition.type == "blips"
and params.blip_counter.value >= params.currentLevelData.winCondition.value
then
if blipType == "bolt" then
params.setPendingWinLevelDelay(2)
else
params.setPendingWinLevel()
end
end
end
function Game.winLevel(currentLevelData, score, currentLevelHighScore, hiScore)
GameState.set("levelCompleted")
GameState.levelCompletedInputDelay = 1.5
PowerupsManager.reset()
if currentLevelData then -- Arcade mode
if score > currentLevelHighScore then
PlayerProgress.set_level_high_score(currentLevelData.id, score)
currentLevelHighScore = score
GameState.nuHiScore = true
GameState.hiScoreFlashVisible = true
end
else
if score > hiScore then
hiScore = score
GameState.nuHiScore = true
GameState.hiScoreFlashVisible = true
end
end
local current_level_index
for i, level in ipairs(LevelsSelector.get_level_points()) do
if currentLevelData and level.label == currentLevelData.id then
current_level_index = i
break
end
end
if current_level_index and current_level_index < #LevelsSelector.get_level_points() then
local next_level = LevelsSelector.get_level_points()[current_level_index + 1]
PlayerProgress.unlock_level(next_level.label)
end
if current_level_index then
GameState.allLevelsCompleted = (current_level_index == #LevelsSelector.get_level_points())
end
PlayerProgress.save()
return currentLevelHighScore, hiScore
end
function Game.clearGameObjects()
Game.circles = {}
Game.particles = {}
Game.playerCircle = nil
Game.init(GameState.isAttractMode)
PowerupsManager.init(Game.circles, GameState.isAttractMode)
if Powerups then
Powerups.stars = {}
Powerups.clocks = {}
Powerups.phaseShifts = {}
Powerups.bolts = {}
Powerups.scoreMultipliers = {}
Powerups.spawnRateBoosts = {}
Powerups.particles = {}
Powerups.lightning = nil
end
Game.jumpPings = {}
end
return Game