-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgame.lua
463 lines (394 loc) · 14.1 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
local moduleName = "[Ingame]"
var = 0
var2 = 0
--General background vars/Core
local mouseSens = 0.01
local PlayerX = 0
local alphaEffect = 1
local playerPaddleSize = 0.86
local currentSongTime = 0
local songSkippedIntro = false
local songVol = 0.87
local playerAlpha = 255
gameOver = false
--Game resolution specifics
local screenRatio = 1
local ingameBoundaryX1 = 0
local ingameBoundaryX2 = 0
local ingameCalculatedScreenResX = 512*1
--Should the game play itself?
autoPlay = true
--Positional vars/Control Vars
local nextNote = 1
local songTimeOld = 0
local noteDrawOffset = 0
local combo = 1
local run = false
local kiaiEnabled = 0
local kiaiAlpha = 0
local score = 0
local scoreAdd = 0
local scorePosY = 0
local comboPosY = 0
local newsSpeed = 0.4
local leadIn = 0
local endDelay = 0
--Gameplay UI related vars
local precision = 0
local noteHits = 1
local noteMisses = 1
local block = 1
local redAlert = 255
--Global audio offset. This *will* be configurable in the options menu. Feel free to mess with it, optimal value is around 24~32ms
globalOffset = 29
function gameLoad(selectedSong)
love.graphics.setBackgroundColor(0, 0, 0, 255)
--Fades screen if set to 0
screenAlpha = 60
--Graphic loading routine
debugLog("Loading player sprites and glow FX", 1, moduleName)
playerImageFX = love.graphics.newImage("img/garsom_hitech.png")
playerImageNormal = love.graphics.newImage("img/garsom_semswag.png")
pelletHitCircle = love.graphics.newImage("img/hexa.png")
pelletSlidertick = love.graphics.newImage("img/fruteenha.png")
fx = love.audio.newSource("uisounds/normal-hitclap.wav", "static")
fx:setVolume(0.5)
fxMiss = love.audio.newSource("uisounds/miss.ogg", "static")
fxMiss:setVolume(1.0)
kiaiGlow = love.graphics.newImage("img/grad.png")
--Particle System setup
particleClickTexture = love.graphics.newImage("img/crashParticle.png")
particleClick = love.graphics.newParticleSystem(particleClickTexture, 60)
particleClick:setParticleLifetime(1, 1.6)
particleClick:setSizeVariation(1)
particleClick:setLinearAcceleration(-30, 20, 30, 120)
particleClick:setColors(255, 255, 255, 255, 255, 255, 255, 0) -- Fade to transparency.
particleClick:setDirection(math.rad(90))
particleClick:setSpin(1)
particleClick:setSpeed(-50, -290)
particleClick:setSpread(3.14159)
--End of particle system setup
--End of graphics loading
--Parse stuff and load music
debugLog("Parsing selected file", 1, moduleName)
parser.loadOsuFile(selectedSong.filePath)
loadSong(selectedSong.audioFile)
leadIn = parser.getAudioLeadIn()
--Generate a list suitable for messing around with it's values and a static list for original value referencing
noteListStatic = parser.getHitObjects(pelletHitCircle, pelletSlidertick)
noteListDinamic = parser.getHitObjects(pelletHitCircle, pelletSlidertick)
--Begin BPM calculation
timingPoints = parser.getTimingPoints()
curTimingPoint = 1
initialTime = {Time = timingPoints[1].offset, kiai = 0}
BPMList = {}
curMPB = timingPoints[1].mpb
while (initialTime.Time < tonumber(noteListStatic[#noteListStatic].objTime)) do
initialTime = {Time = initialTime.Time + curMPB, kiai = timingPoints[curTimingPoint].kiai }
if timingPoints[curTimingPoint + 1] ~= nil then
if (initialTime.Time > timingPoints[curTimingPoint + 1].offset) then
if tonumber(timingPoints[curTimingPoint + 1].inherited) == 1 then
curMPB = timingPoints[curTimingPoint + 1].mpb
end
curTimingPoint = curTimingPoint + 1
end
end
table.insert(BPMList, initialTime)
end
curTimingPoint = 1
--Begins game
debugLog("Playing the song using interpolated timer", 1, moduleName)
playInterpolated(dt, leadIn)
playerImageSize, playerImagePos, playerImageBoundaries = getCurrentSize(playerImageNormal, "player", playerPaddleSize, PlayerX, -ScreenSizeH/2.17, false)
--Tells the player how to skip song intro
if tonumber(noteListDinamic[1].objTime) > 8000 then
print("You can skip the song intro by pressing the [P] Key")
end
debugLog("Done loading!", 1, moduleName)
debugLog("Game begins!", 1, moduleName)
end
function gameUpdate(dt)
--Var updates
particleClick:setSizes(ScreenSizeH/700, ScreenSizeH/650, ScreenSizeH/600)
currentSongTime = getInterpolatedTimer() - globalOffset
noteDrawOffset = ScreenSizeH - (ScreenSizeH - playerImageBoundaries.Y1)
screenRatio = ScreenSizeH/384
speed = (ScreenSizeH/ScreenSizeHOld)*screenRatio --Speed at which notes fall
--particleClick:setSpeed(0, clamp(-170-combo, -170, -500))
ingameCalculatedScreenResX = 512*screenRatio
ingameBoundaryX1 = (ScreenSizeW - ingameCalculatedScreenResX)/2
ingameBoundaryX2 = (ScreenSizeW - ingameCalculatedScreenResX)/2 + ingameCalculatedScreenResX
--Skips song if user told to do so
if songSkippedIntro ~= true and noteListDinamic[1].objTime - currentSongTime > 6000 then
if love.keyboard.isDown("p") then
songSkippedIntro = true
musicSeek((noteListStatic[1].objTime/1000 - 3))
screenAlpha = 0
songVol = 0
print("Skipped!")
end
end
--BPMs on the score
if currentSongTime > BPMList[curTimingPoint].Time then
scorePosY = ScreenSizeH*0.06
if BPMList[curTimingPoint+1] ~= nil then
curTimingPoint = curTimingPoint + 1
if BPMList[curTimingPoint+1]~= nil then
if tonumber(BPMList[curTimingPoint].kiai) == 1 then
kiaiAlpha = 160
end
end
end
end
--Gets player input. This should later be actually done in the keyboard callback to avoid delays
if love.keyboard.isDown("right") and playerImageBoundaries.X2 < ingameBoundaryX2 + ScreenSizeW*0.017 then
if love.keyboard.isDown("lshift") then
PlayerX = PlayerX - (ScreenSizeW*(0.63*2)*dt)
run = true
else
PlayerX = PlayerX - (ScreenSizeW*0.63)*dt
run = false
end
end
if love.keyboard.isDown("left") and playerImageBoundaries.X1 > ingameBoundaryX1 - ScreenSizeW*0.017 then
if love.keyboard.isDown("lshift") then
PlayerX = PlayerX + (ScreenSizeW*(0.63*2)*dt)
run = true
else
PlayerX = PlayerX + (ScreenSizeW*0.63)*dt
run = false
end
end
if love.keyboard.isDown("lshift") then
run = true
else
run = false
end
--Notelist update
--Updates only 170 notes every frame, optimization
--Makes sure the game doesn't lag beyond belief in marathon maps
for i = block, block+170 do
if noteListDinamic[i] ~= nil then
noteListDinamic[i].objTime = noteListStatic[i].objTime
noteListDinamic[i].objTime = noteListDinamic[i].objTime*(speed*round(newsSpeed, 4)) - currentSongTime*(speed*round(newsSpeed, 4))
--Collision with notes
if noteListDinamic[i].objTime + noteDrawOffset <= playerImageBoundaries.Y1 + ScreenSizeH*0.01 then
if ScreenSizeW/2-((noteListDinamic[i].x-256)*screenRatio) > playerImageBoundaries.X1-(playerImageBoundaries.X1*0.013)
and ScreenSizeW/2-((noteListDinamic[i].x-256)*screenRatio) < playerImageBoundaries.X2*1.013 then
--User hit the note!
if noteListDinamic[i].hasBeenHit == false then
if autoPlay then
if noteListDinamic[nextNote+1] ~= nil then
nextNote = nextNote + 1
end
end
--Updates rendering block pos
block = block + 1
--Adds score accordingly
if noteListDinamic[i].objType == 1 then
scoreAdd = scoreAdd + (300 * combo)
comboPosY = ScreenSizeH*0.03
combo = combo + 1
alphaEffect = 255
fx:stop()
fx:rewind()
fx:setVolume(0.5)
fx:play()
--Emit particles
particleClick:setPosition((ScreenSizeW/2-((noteListDinamic[i].x-256)*screenRatio)), playerImageBoundaries.Y1)
particleClick:setColors(noteListDinamic[i].r, noteListDinamic[i].g, noteListDinamic[i].b, 170, noteListDinamic[i].r, noteListDinamic[i].g, noteListDinamic[i].b, 0)
particleClick:emit(15)
--Speed up game
newsSpeed = newsSpeed + 0.0005
elseif noteListDinamic[i].objType == 2 then
--Pellet
scoreAdd = scoreAdd + 100
end
--Updates precisions
noteHits = noteHits + 1
noteMisses = noteMisses + 1
noteListDinamic[i].hasBeenHit = true
end
elseif noteListDinamic[i].hasBeenHit == false then
--User missed the note!
if noteListDinamic[nextNote+1] ~= nil then
nextNote = nextNote + 1
end
block = block + 1
noteListDinamic[i].hasBeenHit = true
noteMisses = noteMisses + 1
redAlert = 0
--Changes game pace a bit
newsSpeed = newsSpeed - 0.0028
--Plays error SFX
if noteListDinamic[i].objType == 1 then
if combo > 35 then
fxMiss:play()
end
--Resets combo
combo = 1
end
end
end
end
end
--Checks to see if the last note has been hit
if noteListDinamic[#noteListDinamic].hasBeenHit == true and gameOver == false then
gameOver = true
endDelay = currentSongTime
end
--Updates vars that control ingame graphics
newsSpeed = clamp(newsSpeed, 0.66, 0.423)
alphaEffect = lerp(alphaEffect, 1, 0.08*dt*100)
songVol = lerp(songVol, 0.87, 0.08*dt*100)
score = lerp(score, scoreAdd, 0.2*dt*100)
scorePosY = lerp(scorePosY, 0, 0.2*dt*100)
comboPosY = lerp(comboPosY, 0, 0.2*dt*100)
precision = noteHits/noteMisses
redAlert = lerp(redAlert, 255, 0.03*dt*100)
kiaiAlpha = lerp(kiaiAlpha, 0, 0.06*dt*100)
--Updates particle systems
particleClick:update(dt*4.5)
--Autoplay scripting
if autoPlay then
if PlayerX ~= (noteListDinamic[nextNote].x-256)*screenRatio then
if PlayerX > (noteListDinamic[nextNote].x-256)*screenRatio then
PlayerX = PlayerX - (((ScreenSizeW*0.6)*dt)*2)
elseif PlayerX < (noteListDinamic[nextNote].x-256)*screenRatio then
PlayerX = PlayerX + (((ScreenSizeW*0.6)*dt)*2)
end
if (PlayerX +(((ScreenSizeW*0.6)*dt)*2) > (noteListDinamic[nextNote].x-256)*screenRatio)
and (PlayerX -(((ScreenSizeW*0.6)*dt)*2) < (noteListDinamic[nextNote].x-256)*screenRatio) then
PlayerX = (noteListDinamic[nextNote].x-256)*screenRatio
end
end
end
if not gameOver then
screenAlpha = lerp(screenAlpha, 60, 0.06*dt*100)
else
playerAlpha = lerp(playerAlpha, 0, 0.05*dt*100)
if currentSongTime >= endDelay + 3000 then
screenAlpha = lerp(screenAlpha, 0, 0.16*dt*100)
--Fade screen and dispose of game elements
if screenAlpha <= 0.7 then
--Finished fading, dispose and go to gameOver stat screen
gameDispose()
gameOverLoad(scoreAdd, precision)
Screen = 3
end
else
--Wait 3 seconds before fading
screenAlpha = lerp(screenAlpha, 140, 0.04*dt*100)
end
end
end
function gameDraw()
drawBGParallax(PlayerX*0.02, my * mouseSens, false)
if kiaiAlpha > 5 then
love.graphics.setColor(255, 255, 255, kiaiAlpha)
love.graphics.draw(kiaiGlow, 0, 0, 0, 0.8, ScreenSizeW/(kiaiGlow:getWidth()/0.14))
love.graphics.draw(kiaiGlow, ScreenSizeW, ScreenSizeH, 3.14159, 0.8, ScreenSizeW/(kiaiGlow:getWidth()/0.14))
end
--Note crash effect
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(particleClick,0,0)
love.graphics.setColor(255, 255, 255, 255)
for i = block, block + 170 do
--Draws only 170 notes after the next note that should be hit. Makes sure FPS doesn't abysmally on marathon songs (not goood having over 1000 draw calls here)
if noteListDinamic[i] ~= nil then
if noteListDinamic[i].hasBeenHit == false then
if tonumber(noteListDinamic[i].objTime) < ScreenSizeH then
love.graphics.setColor(noteListDinamic[i].r, noteListDinamic[i].g, noteListDinamic[i].b, 255)
getCurrentSize(noteListDinamic[i].image, "HO", 0.6, (noteListDinamic[i].x-256)*screenRatio, ScreenSizeH/2+noteListDinamic[i].objTime - noteDrawOffset, true)
end
end
end
end
--If player is running...
if run then
love.graphics.setColor(20, 255, 20, playerAlpha)
playerImageSize, playerImagePos, playerImageBoundaries = getCurrentSize(playerImageNormal, "player", playerPaddleSize, PlayerX, -ScreenSizeH/2.17, true)
else
love.graphics.setColor(255, 255, 255, playerAlpha)
playerImageSize, playerImagePos, playerImageBoundaries = getCurrentSize(playerImageNormal, "player", playerPaddleSize, PlayerX, -ScreenSizeH/2.17, true)
end
--Combo count
love.graphics.setFont(ingameFont)
love.graphics.setColor(255, 255, 255, playerAlpha)
love.graphics.printf(combo,playerImageBoundaries.X1, (ScreenSizeH/1.8)+(comboPosY/2), playerImageBoundaries.X2 - playerImageBoundaries.X1, "center" )
--Glow effect
love.graphics.setColor(80, 255, 40, alphaEffect)
getCurrentSize(playerImageFX, "playerEffect", 1.75*playerPaddleSize, PlayerX, -ScreenSizeH/2.17, true)
--Score text
love.graphics.setColor(255, 255, 255, playerAlpha)
love.graphics.printf(round(score, 0), 0, scorePosY, 400, "left")
--Precision text
love.graphics.setFont(font)
love.graphics.setColor(255, redAlert, redAlert, playerAlpha)
love.graphics.printf((round(precision, 4)*100).."%", ScreenSizeW-(ScreenSizeW*0.1), 0, ScreenSizeW*0.1, "right")
--FPS debug
love.graphics.setFont(debugFont)
love.graphics.setColor(255, 255, 255, 255)
end
function gameDispose()
--Resets vars to nil
local oldmem = collectgarbage("count")
noteListDinamic = nil
noteListStatic = nil
playerImageFX = nil
playerImageNormal = nil
pelletHitCircle = nil
pelletSlidertick = nil
fx = nil
fxMiss = nil
kiaiGlow = nil
particleClickTexture = nil
particleClick = nil
BPMList = nil
collectgarbage("collect")
local newmem = collectgarbage("count")
debugLog("Disposed of resources! "..round(oldmem-newmem, 3).."kB memory freed.", 1, moduleName)
end
function gameReset()
mouseSens = 0.01
PlayerX = 0
alphaEffect = 1
playerPaddleSize = 0.86
currentSongTime = 0
songSkippedIntro = false
songVol = 0.87
playerAlpha = 255
gameOver = false
screenRatio = 1
ingameBoundaryX1 = 0
ingameBoundaryX2 = 0
ingameCalculatedScreenResX = 512*1
nextNote = 1
songTimeOld = 0
noteDrawOffset = 0
combo = 1
run = false
kiaiEnabled = 0
kiaiAlpha = 0
score = 0
scoreAdd = 0
scorePosY = 0
comboPosY = 0
newsSpeed = 0.4
leadIn = 0
endDelay = 0
precision = 0
noteHits = 1
noteMisses = 1
block = 1
redAlert = 255
end
--THOUGHTS
--after a bit of looking around, finally got to find the line that calculates the AR based on it's .osu value
--Taken from opsu source code:
--// approachRate (hit object approach time)
--if (approachRate < 5)
-- approachTime = (int) (1800 - (approachRate * 120));
--else
-- approachTime = (int) (1200 - ((approachRate - 5) * 150));
--I should implement this soon.