-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
825 lines (680 loc) · 18.6 KB
/
main.lua
File metadata and controls
825 lines (680 loc) · 18.6 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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
require "gob"
require "sprite"
require "util"
--
-- Globals
--
local _alive
local _elapsed
local _dramaticPause
local _gameOver
local _copSpeed
local _copSpeedCurrent
local _copAccel
local _speed
local _damage
local _accel
local _turnAccel
local _pos
local _lastCarSpawn
local _carSpawnInterval
local _debug = false
--
-- Constants
--
local _dramaticPauseTime = 0.5
local _carSpawnIntervalMax = 0.25
local _carSpeedBase = 100
local _speedFactor = .1 -- increases difficulty
local _copSpeedIncrement = 0.00 -- XXX
local _damageIncrement = 0.15
local _perspOffset = 8
local _roadOffset = 60
local _roadHeight = 120
local _laneCount = 6
local _laneHeight = _roadHeight / _laneCount
local _roadTop = _roadOffset - _perspOffset
local _roadBottom = _roadOffset + _roadHeight
local _roadSpeed = 300
local _width = 400
local _height = 200
--
-- Game entities
--
function makeHitbox(width, perspFudge)
return {
x = 8,
y = _perspOffset + 2,
w = width - 16,
h = 12,
}
end
local _sprites = {
-- Gameplay
player = Sprite:newAnim("player.png", 11, {
hitbox = makeHitbox(32),
center = { x = 0, y = -3 },
}),
-- Background
bg = Sprite:new("bg.png"),
fg = Sprite:new("fg.png"),
cop = Sprite:newAnim("cop.png", 4, {
hitbox = { x = 0, y = _perspOffset, w = 32, h = 120 },
}),
-- Effects
smoke = Sprite:new("smoke.png"),
explosion = Sprite:newAnim("explosion.png", 17, {
center = { x = 8, y = 8 },
}),
}
local _carSprites = {
Sprite:new("car.png", {
hitbox = makeHitbox(32),
}),
Sprite:new("bus.png", {
hitbox = makeHitbox(48),
}),
Sprite:new("semi.png", {
hitbox = makeHitbox(64),
}),
}
local _bg1 = Gob:new({
sprite = _sprites.bg
})
local _bg2 = Gob:new({
sprite = _sprites.bg
})
local _fg1 = Gob:new({
sprite = _sprites.fg
})
local _fg2 = Gob:new({
sprite = _sprites.fg
})
local _player = Gob:new({
x = 0,
y = 0,
sprite = _sprites.player,
fps = 20,
smoke1 = love.graphics.newParticleSystem(_sprites.smoke.image, 50),
smoke2 = love.graphics.newParticleSystem(_sprites.smoke.image, 20),
})
local _cops = {}
for i = 1, _laneCount do
local y = _roadOffset - _perspOffset + 2 -- fudge
y = y + (i - 1) * _laneHeight
local cop = Gob:new({
sprite = _sprites.cop,
fps = 15,
time = randf(0.0, 1.0),
y = y
})
table.insert(_cops, cop)
end
local _lanes = {}
local _numberFont = love.graphics.newImageFont(
loadImage("numberFont.png"), "1234567890.:")
local _gameOverImage = loadImage("gameover.png")
local _attractModeImage = loadImage("attractMode.png")
local _explosions = {}
--
-- Audio
--
local _sounds = {
engine = love.audio.newSource("rsrc/engine.mp3", "static"),
brokenEngine = love.audio.newSource("rsrc/engineBroken.mp3", "static"),
brokenEngine2 = love.audio.newSource("rsrc/engineBroken2.mp3", "static"),
brokenEngine3 = love.audio.newSource("rsrc/engineBroken3.mp3", "static"),
crash = love.audio.newSource("rsrc/crash.mp3", "static"),
boomEcho = love.audio.newSource("rsrc/boomEcho.mp3", "static"),
}
local _booms = {
}
for i = 1, 10 do
local b = love.audio.newSource("rsrc/boom.mp3", "static")
table.insert(_booms, b)
end
_sounds.engine:setLooping(true)
_sounds.brokenEngine:setLooping(true)
_sounds.brokenEngine2:setLooping(true)
_sounds.brokenEngine3:setLooping(true)
--
-- Functions
--
function reset(firstTime)
_alive = true
_elapsed = 0
_dramaticPause = _dramaticPauseTime
_gameOver = false
_attractMode = false
_copSpeed = 0.1
_copSpeedCurrent = _copSpeed
_copAccel = 0.1
_speed = 0.2
_damage = 0
_accel = 0.3
_turnAccel = 0.0
_pos = 0.0
_lastCarSpawn = 0
_carSpawnInterval = 1
_lanes = {}
for i = 1, _laneCount do
table.insert(_lanes, {})
end
for _, cop in pairs(_cops) do
cop.alive = true
cop.visible = true
end
_player.alive = true
_player.visible = true
_player.smoke1:stop()
_player.smoke1:reset()
_player.smoke2:stop()
_player.smoke2:reset()
silence()
_engineSound = _sounds.engine
_engineSound:play()
for k,v in pairs(_explosions) do
_explosions[k] = nil
end
if (firstTime) then
-- XXX hacky setup for title screen
_alive = false
_gameOver = true
_attractMode = true
for _, cop in pairs(_cops) do
cop.alive = false
cop.visible = false
end
_player.alive = false
_player.visible = false
_engineSound:stop()
-- Spawn a few cars in random locations
for x = 1, 10 do
spawnCar(true)
end
end
end
function die()
_alive = false
_dramaticPause = _dramaticPauseTime
end
function boom()
if (not _alive) then
-- Death sequence
return
end
for _, b in pairs(_booms) do
if (b:isStopped()) then
b:play()
return
end
end
end
function silence()
if (_engineSound) then
_engineSound:stop()
end
for _, b in pairs(_sounds) do
b:stop()
b:rewind()
end
for _, b in pairs(_booms) do
b:stop()
b:rewind()
end
end
function updateGame(dt)
_elapsed = _elapsed + dt
local addRate = 0.5
local subRate = 0.5
-- Update player speed
if (love.keyboard.isDown(" ")) then
_accel = _accel + (addRate * dt)
_accel = math.min(1, _accel)
else
_accel = _accel - (subRate * dt)
_accel = math.max(-1, _accel)
end
_accel = _accel - _damage / 100
local maxSpeed = 0.9
_speed = _speed + (_accel * dt)
if (_speed > maxSpeed) then
_speed = maxSpeed
_accel = 0
end
-- Update (vertical) position
local turnRate = 15
local maxTurnAccel = 4
if (love.keyboard.isDown("down")) then
_turnAccel = _turnAccel + (turnRate * dt)
_turnAccel = math.min(maxTurnAccel, _turnAccel)
elseif (love.keyboard.isDown("up")) then
_turnAccel = _turnAccel - (turnRate * dt)
_turnAccel = math.max(-maxTurnAccel, _turnAccel)
else
-- Return to center
if (_turnAccel > 0) then
_turnAccel = _turnAccel - (turnRate * dt * 2)
_turnAccel = math.max(0, _turnAccel)
else
_turnAccel = _turnAccel + (turnRate * dt * 2)
_turnAccel = math.min(0, _turnAccel)
end
end
_pos = _pos + (_turnAccel * dt)
-- Keep car on the road
_pos = clamp(_pos, -1, 1)
if (_pos >= 1 or _pos <= -1) then
_turnAccel = 0
end
-- Update cops' speed
if (_copSpeedCurrent < _copSpeed) then
_copSpeedCurrent = _copSpeedCurrent + (_copAccel * dt)
end
-- Update engine sound
if (_damage > 0) then
local newSound = _engineSound
if (_damage < 0.3) then
newSound = _sounds.brokenEngine
elseif (_damage < 0.6) then
newSound = _sounds.brokenEngine2
else
newSound = _sounds.brokenEngine3
end
if (_engineSound ~= newSound
and not _engineSound:isPaused()) then
_engineSound:stop()
_engineSound = newSound
_engineSound:play()
end
end
_engineSound:setPitch(1.5 + _damage + (_accel / 2))
end
function updateBg(dt)
local boost = -_speedFactor * _elapsed
boost = clamp(0, -2, boost)
local dx = -_roadSpeed * dt + boost
local w = _bg1:getWidth()
_bg1.x = _bg1.x + dx
if (_bg1.x < -w) then
_bg1.x = _bg1.x + w
end
_bg2.x = _bg1.x + w
-- Sync foreground to background
_fg1.x = _bg1.x
_fg2.x = _bg2.x
end
function updateCars(dt)
-- Spawn cars
if (_lastCarSpawn < 0) then
spawnCar()
local x = math.min(1.0, _elapsed / 30.0)
local t = lerp(_carSpawnInterval, _carSpawnIntervalMax, x)
_lastCarSpawn = math.random() * t
-- XXX TODO check for collisions
else
_lastCarSpawn = _lastCarSpawn - dt
end
-- Update cars
for _, lane in pairs(_lanes) do
for i, c in pairs(lane) do
c:update(dt)
local boost = -_speedFactor * _elapsed
local dx = c.speed * dt + boost
c.x = c.x + dx
if (c.turnAccel) then
local dy = c.turnSpeed * dt
c.y = c.y + dy
c.angle = c.turnSpeed / -540
c.turnSpeed = c.turnSpeed + c.turnAccel
end
end
end
end
function updateCops(dt)
for i, cop in pairs(_cops) do
cop:update(dt)
cop.x = _copSpeedCurrent * _width - 32
end
end
function updateExplosions(dt)
for i, x in pairs(_explosions) do
x:update(dt)
local boost = -_speedFactor * _elapsed
boost = clamp(0, -2, boost)
x.x = x.x + (-_roadSpeed * dt) + boost
-- XXX HACK
if (x.x < -50) then
table.remove(_explosions, i)
end
end
end
function updatePlayer(dt)
_player:update(dt)
_player.x = _speed * _width
local y = (_pos + 1) / 2.0;
_player.y = lerp(_roadTop, _roadBottom - _player:getHeight(), y)
-- Update related objects
_player.smoke1:update(dt)
_player.smoke1:setPosition(
_player.x + 8,
_player.y + 12)
_player.smoke2:update(dt)
_player.smoke2:setPosition(
_player.x + 8,
_player.y + 12)
end
function checkCollisions()
-- cops
if (_alive) then
for _, cop in pairs(_cops) do
if (_player:hitGob(cop)) then
die()
silence()
break
end
end
end
-- cars
local top = _roadOffset - _laneHeight
local bottom = _roadOffset + _roadHeight - _laneHeight
for _, lane in pairs(_lanes) do
for i, c in pairs(lane) do
if (c.alive and _player:hitGob(c)) then
c.alive = false
if (_alive) then
_sounds.crash:play()
end
-- Make the car swerve off
if (_turnAccel == 0) then
c.turnAccel = randf(-6.0, 6.0)
else
c.turnAccel = _turnAccel * 3
end
c.turnSpeed = _turnAccel * 2
-- Uh-oh, hit a car, the cops will not be pleased
_copSpeed = _copSpeed + _copSpeedIncrement
_damage = _damage + _damageIncrement
local p = _player.smoke1
p:start()
p:setEmissionRate(5 + _damage * 40)
p:setSpeed(100, 120)
p:setSpread(math.pi / 15)
p:setLifetime(-1)
p:setParticleLife(0.5)
p:setDirection(math.pi)
p:setTangentialAcceleration(20, 100)
p:setSpin(-8, 8)
p:setSizes(1.0, 2.0)
p:setColors(255, 255, 255, 224, 58, 128, 255, 0)
if (_damage > 0.2) then
p = _player.smoke2
p:start()
p:setEmissionRate(1 + _damage * 10)
p:setSpeed(80, 90)
p:setSpread(math.pi / 15)
p:setLifetime(-1)
p:setParticleLife(0.75)
p:setDirection(math.pi)
p:setTangentialAcceleration(20, 100)
p:setSpin(-4, 4)
p:setSizes(1.0, 3.0)
p:setColors(0, 0, 0, 192, 58, 128, 255, 0)
end
end
if (_attractMode) then
-- Special case for intro; quietly remove when offscreen
if (c.x + c:getWidth() < 0) then
table.remove(lane, i)
end
else
local copPosition = _cops[1]:getWorldSpaceBbox().br_x
if (c.x <= copPosition) then
-- Car intersected with police line
c.alive = false
end
if (not c.alive and c.visible) then
if (c.y < top or c.y > bottom
or c.x <= copPosition) then
-- Explosions!
boom()
for i = 1, math.random(8, 10) do
spawnExplosion(c.x, c.y, c:getWidth(), c:getHeight())
end
c.visible = false
table.remove(lane, i)
end
end
end
end
end
end
function spawnCar(randomX)
local x = _width
local laneIndex = math.random(1, _laneCount)
local y = (laneIndex - 1) * _laneHeight + _roadOffset
if (randomX) then
x = math.random(_width)
end
y = y + 2 - _perspOffset -- fudge
local c = Gob:new({
x = x,
y = y,
sprite = _carSprites[math.random(#_carSprites)],
speed = -_carSpeedBase
})
-- Make sure it doesn't overlap with another car in the same lane
local lane = _lanes[laneIndex]
local pad = 5
local xMin = x - pad
local xMax = x + c:getWidth() + pad
for _, otherCar in pairs(lane) do
if (xMax > otherCar.x
and xMin < otherCar.x + otherCar:getWidth()) then
-- Overlap detected; bail
-- XXX might be better to try and find another lane
return
end
end
table.insert(lane, c)
end
function spawnExplosion(x, y, w, h)
local x1 = randf(x, x + w)
local y1 = randf(y, y + h)
local x = Gob:new({
x = x1,
y = y1,
sprite = _sprites.explosion,
fps = 30,
speed = -_carSpeedBase,
time = randf(0.0, 1.0),
angle = randf(0, math.pi * 2)
})
table.insert(_explosions, x)
end
function drawDebug()
if (not _debug) then
return
end
function fmt(n)
return string.format("%.2f", n)
end
love.graphics.push()
love.graphics.setColor(0, 0, 0)
love.graphics.setFont(love.graphics.newFont(9))
-- Debug output
love.graphics.print(
"Speed: " .. fmt(_speed)
.. " Accel: " .. fmt(_accel)
.. " Damage: " .. fmt(_damage),
10, 10)
love.graphics.print(
"Pos: " .. fmt(_pos)
.. " Turn: " .. fmt(_turnAccel),
10, 20)
love.graphics.print("Time: " .. fmt(_elapsed), 10, 30)
local status
if (_alive) then
status = "ALIVE"
else
status = "DEAD"
end
love.graphics.print(status, 10, 40)
-- player speed
love.graphics.setColor(255, 0, 255)
local width = (_width - 20) * _speed
love.graphics.rectangle("fill", 10, 3, width, 5)
-- min speed
love.graphics.setColor(0, 255, 255)
local width = (_width - 20) * _copSpeed
love.graphics.rectangle("fill", 10, 3, width, 5)
-- draw hitboxes
love.graphics.setColor(0, 255, 255, 192)
for _, lane in pairs(_lanes) do
for _, c in pairs(lane) do
c:drawHitbox()
end
end
_player:drawHitbox()
for _, cop in pairs(_cops) do
cop:drawHitbox()
end
love.graphics.pop()
end
function drawExplosions()
for _, x in pairs(_explosions) do
x:draw()
end
end
function drawOverlay()
love.graphics.push()
love.graphics.setFont(_numberFont)
local min = _elapsed / 60
local sec = _elapsed % 60
-- XXX hmm, can't get this to work the same way as printf() ...
local str = ""
if (min >= 1) then
str = str .. string.format("%d", min)
end
if (sec < 10) then
str = str .. string.format(":0%.2f", sec)
else
str = str .. string.format(":%.2f", sec)
end
-- Time overlay
if (not _attractMode) then
-- local x = (_width - _numberFont:getWidth(str)) / 2
local x = 160
love.graphics.print(str, x, 10)
end
function drawCentered(img)
local x = (_width - img:getWidth()) / 2
local y = (_height - img:getHeight()) / 2
love.graphics.draw(img, x, y)
end
-- Game over?
if (_gameOver) then
if (_attractMode) then
drawCentered(_attractModeImage)
else
drawCentered(_gameOverImage)
end
end
love.graphics.pop()
end
function drawPlayer()
_player:draw()
love.graphics.draw(_player.smoke1)
love.graphics.draw(_player.smoke2)
end
--
-- Love entry points
--
function love.load()
-- Initialize for first-time startup
reset(true)
-- Graphics setup
love.graphics.setFont(love.graphics.newImageFont(
loadImage("font.png"),
" abcdefghijklmnopqrstuvwxyz"
.. "ABCDEFGHIJKLMNOPQRSTUVWXYZ0"
.. "123456789.,!?-+/():;%&`'*#=[]\""))
end
function love.draw()
love.graphics.push()
love.graphics.scale(3.0, 3.0)
_bg1:draw()
_bg2:draw()
for _, cop in pairs(_cops) do
cop:draw()
end
-- Cheap-ass depth sort
local playerDrawn = false
for i = 1, _laneCount do
local lane = _lanes[i]
if (not playerDrawn) then
local y = _roadHeight / _laneCount * (i-1) + _roadOffset
if (y > _player:getWorldSpaceBbox().ul_y) then
drawPlayer()
playerDrawn = true
end
end
for _, c in pairs(lane) do
c:draw()
end
end
if (not playerDrawn) then
drawPlayer()
end
_fg1:draw()
_fg2:draw()
drawExplosions()
drawOverlay()
drawDebug()
love.graphics.pop()
end
function love.update(dt)
if (_alive) then
updateGame(dt)
updateBg(dt)
updatePlayer(dt)
updateCars(dt)
updateCops(dt)
updateExplosions(dt)
checkCollisions()
elseif (_dramaticPause > 0) then
_dramaticPause = _dramaticPause - dt
else
if (not _gameOver) then
_gameOver = true
-- Boom, oom, oom, oom ...
_sounds.boomEcho:play()
_player.smoke1:stop()
_player.smoke2:stop()
_player.alive = false
_player.visible = false
for i = 1, math.random(15, 20) do
spawnExplosion(
_player.x, _player.y,
_player:getWidth(), _player:getHeight())
end
end
dt = dt / 4
updateBg(dt)
updatePlayer(dt)
updateCars(dt)
updateCops(dt)
updateExplosions(dt)
checkCollisions()
end
end
function love.keypressed(key, unicode)
if (key == "escape") then
love.event.quit()
elseif (key == "d") then
_debug = not _debug
elseif (key == "return" and not _alive) then
-- XXX game restart hack
reset()
end
end