forked from PAK9/PicoWorldRace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwr.p8
1571 lines (1351 loc) · 66.2 KB
/
pwr.p8
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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pico-8 cartridge // http://www.pico-8.com
version 35
__lua__
-- pico world race 1.1
-- by pak-9
#include menus.lua
#include particle.lua
#include renderutils.lua
#include sound.lua
#include spritedef.lua
#include trackdef.lua
#include utility.lua
#include profile.lua
-- custom font
font="8,8,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,240,248,252,28,28,0,0,0,15,31,63,56,56,0,0,0,192,192,192,128,128,0,0,0,3,3,3,3,3,0,0,0,252,252,252,0,240,0,0,0,15,31,63,56,63,0,0,0,252,252,252,0,252,0,0,0,15,31,63,56,63,0,0,0,224,240,120,60,28,0,0,0,28,28,28,28,28,0,0,0,252,252,252,28,252,0,0,0,63,63,63,0,15,0,0,0,240,248,252,28,252,0,0,0,31,31,31,0,15,0,0,0,240,248,252,28,0,0,0,0,15,31,63,56,56,28,28,28,252,248,240,0,0,56,56,56,63,31,15,0,0,128,128,128,128,128,128,0,0,3,3,3,3,3,3,0,0,248,252,28,252,252,252,0,0,31,15,0,63,63,63,0,0,252,252,0,252,252,252,0,0,63,63,56,63,31,15,0,0,28,252,248,0,0,0,0,0,28,63,63,28,28,28,0,0,252,252,0,252,252,252,0,0,31,63,56,63,31,15,0,0,252,252,28,252,248,240,0,0,31,63,56,63,31,15,0,0,0,0,0,0,128,128,0,0,56,60,30,15,7,3,0,0,0,0,0,240,248,252,28,252,0,0,0,15,31,63,56,63,0,0,0,240,248,252,28,252,0,0,0,15,31,63,56,63,0,0,0,240,248,252,28,156,0,0,0,63,63,63,0,63,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,252,252,28,252,248,240,0,0,63,63,56,63,31,15,0,0,252,248,0,248,248,248,0,0,63,63,56,63,63,31,0,0,156,156,28,252,248,240,0,0,63,63,56,63,63,63,0,0,0,0,0,0,0,0,0,0,0"
CType = split"0,37,40"
Frame, SEG_LEN, DRAW_DIST, CANVAS_SIZE, CAM_HEIGHT = 0,10,100,128,21
ROAD_WIDTH = 60 -- half
CAM_DEPTH = 0.55; -- 1 / tan((100/2) * pi/180) (fov is 100)
-- horizon sprite def: 1. sx 2. sy 3. sw 4. sh 5. xscale 6. yscale
HORZSDEF = {
split"32, 83, 48, 15, 1, 1", -- 1. City
split"32, 98, 48, 10, 1.2, 0.8", -- 2. Mountain
split"32, 109, 45, 9, 1.2, 0.8", -- 3. Glacier
split"32, 119, 46, 9, 1.4, 0.7", -- 4. Hills
}
-- 1. Road c1 2. Road c2 3. Road pat 4. Ground c1 5. Ground c2(x2) 6. Edge c1 7. Edge c2(x2) 8. Lane pat 9. Sky c1 10. Sky c2 11. horizon spr
-- Road patterns: 1. alternating stripes 2. random patches
-- Lane patterns: 1. edges 2. centre alternating 3. 3 lane yellow
THEMEDEF = {
-- r1 r2 rp g1 g2 e1 e2 lp sk1 sk2 hz
split"5, 0x5D, 1, 3, 0x3B, 6, 0x42, 1, 6, 12, 1 ", -- 1. USA
split"5, 0x05, 1, 6, 0x6D, 6, 0x15, 3, 6, 12, 3 ",-- 2. Alaska
split"5, 0x15, 1, 3, 0x23, 6, 0xC5, 2, 7, 12, 4 ", -- 3. Japan
split"5, 0x25, 2, 2, 0x21, 5, 0x42, 3, 13, 2, 2 ", -- 4. Oz
split"4, 0x45, 2, 4, 0x34, 1, 0xD5, 2, 13, 12, 2 ", -- 5. kenya
split"5, 0x65, 2, 6, 0x76, 6, 0x15, 2, 6, 12, 3 ", -- 6. Nepal
split"5, 0x51, 1, 5, 0x35, 6, 0x82, 1, 1, 0, 1 ", -- 7. Germany
split"13, 0xCD, 1, 2, 0x2E, 10, 0xBD, 3, 6, 14, 2 ", -- 8. Funland
}
-- 1. Theme 2. spr pattern 3. yscale 4. curvescale 5. seed 6. name
LEVELDEF={
split"1, 1, 0.5, 0.8, 1, usa ",
split"4, 4, 0.8, 1, 4, australia",
split"2, 2, 1.1, 0.8, 8, alaska",
split"3, 3, 0.9, 1.1, 13, japan",
split"5, 4, 0.8, 1, 30, kenya",
split"6, 2, 1.2, 0.9, 14, nepal",
split"7, 1, 0.9, 1.2, 88, germany",
split"8, 5, 1.3, 1.4, 29, funland"
}
Theme, Level, IsCustomRace=1,1,0
NumSegs = 0
sPointsX, sPointsY, sPointsZ, sPointsC = {},{},{},{}
NUM_LAPS = 3
sSprite,sSpriteX,SpriteCollideRect = {},{},{}
sSpriteSc = {} -- scale
SpriteCollideIdx=-1
sTokensX, sTokensExist = {},{}
TokenCollected,NumTokens = 0,0
LastY = 0 -- last y height when building a track
Position = 0 -- current position around the track
-- -1 to 1 TODO: maybe don't make relative to road width
PlayerX, PlayerXd, PlayerY, PlayerYd, PlayerVl, PlayerVf, PlayerDrift, PlayerAir, PlayerLap, Pfrm = 0,0,0,0,0,0,0,0,0,0
PlayerSeg = 0 -- current player segment
PlayerStandF = 0 -- final standing
BURNOUT_SPD = 1.3
RecoverStage = 0 -- 1. pause 2. lerp to track 3. flash
RecoverTimer, InvincibleTime = 0,0
OpptPos, OpptLap, OpptSeg, OpptX, OpptV = {},{},{},{},{}
HznOffset = 0
HUD_HEIGHT = 16
sScreenShake = {0,0}
-- 1. Menus 2. Racing
TitleState=1
-- 1. countdown 2. race 3. end standing 4. Summary UI
RaceState = -1
RaceStateTimer, RaceCompleteTime = 0,0
RaceCompletePos = 0 -- player standing
function LoopedTrackPos(z)
lps=flr(z/(SEG_LEN*NumSegs))
return z-SEG_LEN*NumSegs*lps
end
function DepthToSegIndex(z)
return flr(z/SEG_LEN) % NumSegs + 1
end
function AddSeg( c, y )
NumSegs+=1
add( sPointsC, c )
add( sPointsX, 0 )
add( sPointsY, y )
add( sPointsZ, NumSegs * SEG_LEN + 1 )
add( sTokensX, 0 )
add( sTokensExist, 0 )
end
function AddSprites( n, p )
sp=LEVELDEF[Level][2]
sd=SPDEF[sp][p]
for i = 1, n do
if p == 0 then
add( sSprite, 0 )
add( sSpriteX, 0 )
add( sSpriteSc, 0 )
else
srand( #sSprite )
added = false
for j = 1, #sd do
sdi=sd[j]
if (#sSprite+sdi[3]) % sdi[2] == 0 then
-- SDEF, interval, interval offset, minx (*roadw), maxx (*roadw), rand l/r
xrand = 1
if (sdi[6] == 1 and rnd( 30000 ) > 15000) xrand = -1
spindex=sdi[1]
add( sSprite, spindex )
add( sSpriteX, ( sdi[4] + rnd( sdi[5] - sdi[4] ) ) * xrand )
add( sSpriteSc, 0.3*(SDEF[spindex][5]+rnd(SDEF[spindex][6]-SDEF[spindex][5])) )
added = true
break
end
end
if added == false then
add( sSprite, 0 )
add( sSpriteX, 0 )
add( sSpriteSc, 0 )
end
end
end
end
function AddCurve( enter, hold, exit, c, y, sprp )
tot=(enter+hold+exit)
AddSprites( tot, sprp )
for i=1,enter do
AddSeg( easein( 0, c, i/enter ), easeinout( LastY,y,i/tot ) )
end
for i=1,hold do
AddSeg( c, easeinout( LastY,y,(i+enter)/tot ) )
end
for i=1,exit do
AddSeg( easeout(c, 0, i/exit ), easeinout( LastY,y,(i+enter+hold)/tot ) )
end
LastY=y
end
function AddStraight( n, y, sprp )
AddSprites( n, sprp )
for i=1,n do
AddSeg( 0, easeinout( LastY, y, i/n ) )
end
LastY=y
end
function AddTokens( seg, x, n )
for i=1,n do
sTokensX[seg+i*3-3] = x
sTokensExist[seg+i*3-3]=1
end
NumTokens += n
end
function InitOps()
for i=1,8 do
OpptPos[i] = SEG_LEN+SEG_LEN * i
OpptX[i]=((i%2)*2-1)*0.2
OpptV[i]=0
OpptLap[i]=1
end
end
function MenuRestart()
InitRace()
end
function MenuQuit()
OpenMenu(MenuState)
end
function InitRace()
TitleState=2
menuitem( 1, "restart race", MenuRestart )
menuitem( 2, "abandon race", MenuQuit )
NumTokens=0
TokenCollected=0
EraseTrack()
if IsCustomRace==1 then
BuildCustomTrack( CustomLevel, CT_HILLS[CustomHills], CT_CURVES[CustomCurves], CustomSeed )
else
BuildCustomTrack( Level, LEVELDEF[Level][3], LEVELDEF[Level][4], LEVELDEF[Level][5] )
end
InitOps()
RaceStateTimer = time()
RaceState = 1
Position = SEG_LEN
PlayerX = -0.2 -- -1 to 1 TODO: maybe don't make relative to road width
PlayerXd, PlayerY, PlayerYd, PlayerVl, PlayerVf, PlayerDrift, PlayerAir, Pfrm = 0,0,0,0,0,0,0,0
PlayerSeg = 0 -- current player segment
PlayerLap = 1
RecoverStage = 0 -- 1. pause 2. lerp to track 3. flash
RecoverTimer, InvincibleTime = 0,0
UpdatePlayer()
end
function _init()
--font init
memset(0x5600,0,256*8)
poke(0x5600,unpack(split(font)))
brush={}
for n=0,4,4 do
brush[n]={}
local start = 0x2000+n*128
local l=peek(start)
for i=1, l do
local cmd={}
for j=1,5 do
cmd[j]=peek(start+(i-1)*6+j)-64
end
cmd[6]=peek(start+(i-1)*6+6)
cmd[7],cmd[6]=(cmd[6]&240)>>4,(cmd[6]&15)
add(brush[n],cmd)
end
end
LoadProfile()
--EraseProfile()
InitSpriteDef()
palt(0, false)
palt(15, true)
InitParticles()
OpenMenu(1)
end
function RaceStateTime()
return time()-RaceStateTimer
end
function IsOffRoad()
return abs( PlayerX*ROAD_WIDTH ) > ROAD_WIDTH and PlayerAir == 0
end
function IsBurnout()
return PlayerAir==0 and IsOffRoad() == false and PlayerVf < BURNOUT_SPD and btn(🅾️) -- z / btn1
end
function UpdateRaceInput()
if RaceState == 2 and PlayerAir == 0 then
if btn(❎) then -- btn2
if abs( PlayerXd ) > 0.1 then
PlayerDrift=sgn(PlayerXd)
Pfrm=max(.2,Pfrm)
else
PlayerVl=PlayerVl-0.08
end
end
if btn(🅾️) then -- z / btn1
PlayerVl=PlayerVl+0.09
end
if btn(⬅️) then -- left
PlayerXd-= (0.022 + -PlayerDrift*0.01) * (1-PlayerVl*0.0005)*min(PlayerVl*0.125,1)
elseif btn(➡️) then -- right
PlayerXd+= (0.022 + PlayerDrift*0.01) * (1-PlayerVl*0.0005)*min(PlayerVl*0.125,1)
end
end
end
function UpdatePlayer()
if InvincibleTime-time() < 0 then
InvincibleTime = 0
end
if PlayerAir == 0 then
if RecoverStage == 0 then
UpdateRaceInput()
end
local drftslw=(1-abs(PlayerDrift)*0.001)
if IsOffRoad() then
PlayerVl=PlayerVl*0.989*drftslw
PlayerXd=PlayerXd*0.96
else
PlayerVl=PlayerVl*0.995*drftslw
PlayerXd=PlayerXd*0.95
end
end
if PlayerVl < 0.02 then
PlayerVl = 0
end
PlayerVf = PlayerVl*0.35
Position=Position+PlayerVf
if Position > SEG_LEN*NumSegs then
Position -= SEG_LEN*NumSegs
PlayerLap += 1
end
PlayerSeg=DepthToSegIndex(Position)
nxtseg=(PlayerSeg)%NumSegs + 1
posinseg=1-(PlayerSeg*SEG_LEN-Position)/SEG_LEN
if RaceState == 3 then
PlayerX=lerp(PlayerX,sPointsX[PlayerSeg],0.05)
end
if abs( PlayerXd ) < 0.005 then
PlayerXd = 0
end
PlayerX+=sPointsC[PlayerSeg]*0.45*PlayerVl*0.01 + PlayerXd*0.15
if abs( PlayerXd ) < 0.08 then
PlayerDrift=0
end
HznOffset = HznOffset + sPointsC[PlayerSeg] * 0.14 * (PlayerVf)
if PlayerDrift==0 then
Pfrm-=.2
else
Pfrm+=.2
end
Pfrm=mid(0,Pfrm,1)
-- jumps / player y
local ground = lerp( sPointsY[PlayerSeg], sPointsY[nxtseg], posinseg)
PlayerY=max(PlayerY+PlayerYd, ground)
if( PlayerY == ground ) then
if PlayerYd < -2 and PlayerAir > 4 then
sScreenShake = {1.5,4}
sfx( 11, 2 )
AddParticle( 6, 52, 122 )
AddParticle( 7, 78, 126 )
AddParticle( 1, 52, 122 )
AddParticle( 2, 78, 122 )
end
local nposinseg=1-(PlayerSeg*SEG_LEN-(Position+PlayerVf ))/SEG_LEN
local nground = lerp( sPointsY[PlayerSeg], sPointsY[nxtseg], nposinseg )
PlayerYd = ( nground - ground ) - 0.2
PlayerAir = 0
else
PlayerYd=PlayerYd-0.25
PlayerAir = PlayerAir + 1
end
-- particles
if RecoverStage < 2 then
if IsOffRoad() then
if PlayerVf > 1 then
if Frame%5 == 0 then
srand(Frame)
AddParticle( 3, 64 + flr(rnd(32))-16, 120 + rnd( 1 ) )
end
if Frame%10 == 0 then
sScreenShake[1],sScreenShake[2] = 1,1
end
end
else
if Frame%8 == 0 and PlayerAir == 0 then
if PlayerDrift < 0 then
AddParticle( 1, 62 - rnd( 4 ), 120 + rnd( 2 ) )
elseif PlayerDrift > 0 then
AddParticle( 2, 74 + rnd( 4 ), 120 + rnd( 2 ) )
elseif IsBurnout() then
AddParticle( 1, 50 - rnd( 4 ), 122 )
AddParticle( 2, 80 + rnd( 4 ), 122 )
end
end
end
end
end
function UpdateRecover()
if RecoverStage == 0 then
return
else
t1,t2,t3=1.5,2.5,3.5
if RecoverStage == 1 then
srand( time() )
if Frame%2==0 then
AddParticle( 8, 64 + rnd(8)-4, 98 + rnd( 2 ) )
end
if Frame%4==0 then
AddParticle( 9, 64 + rnd(8)-4, 88 + rnd( 8 ) )
end
if time() - RecoverTimer >= t1 then
RecoverStage = 2
ClearParticles()
end
elseif RecoverStage == 2 then
instage=(time()-RecoverTimer-t1)/(t2-t1)
PlayerVl=8
PlayerX=lerp(PlayerX,0,instage)
if time() - RecoverTimer >= t2 then
RecoverStage = 3
InvincibleTime=time()+1
end
elseif RecoverStage == 3 then
PlayerX = 0
PlayerVl=8
if time() - RecoverTimer >= t3 then RecoverStage = 0 end
end
end
end
function UpdateOpts()
for i=1,#OpptPos do
OpptPos[i]=OpptPos[i]+OpptV[i]
if OpptPos[i] > SEG_LEN*NumSegs then
OpptPos[i] -= SEG_LEN*NumSegs
OpptLap[i] += 1
end
OpptSeg[i]=DepthToSegIndex(OpptPos[i])
plsegoff1=(OpptSeg[i]-PlayerSeg)%NumSegs+1
if RaceState > 1 then
local opv=(NUM_LAPS-OpptLap[i])*0.017
local opspd=(0.04+PlayerVl*0.022+i*0.008+opv)
if RaceState >= 3 then
opspd=0.08
end
OpptV[i]=OpptV[i]+opspd
OpptV[i]=OpptV[i]*0.92
if RecoverStage == 0 then
srand(i)
OpptX[i] = min( max( OpptX[i] + sPointsC[OpptSeg[i]] * (0.008+rnd(0.01)), -0.6 ), 0.6 ) * (0.99+rnd(0.005))
end
end
end
end
function AddCollisionParticles()
AddParticle( 4, 64 + rnd(32)-16, 96 + rnd( 8 ) )
AddParticle( 5, 64 + rnd(32)-16, 96 + rnd( 8 ) )
AddParticle( 6, 64 + rnd(16)-8, 102 - rnd( 8 ) )
AddParticle( 7, 54 + rnd(32)-16, 102 + rnd( 8 ) )
AddParticle( 7, 64 + rnd(16)-8, 102 - rnd( 8 ) )
AddParticle( 6, 74 + rnd(32)-16, 102 + rnd( 8 ) )
end
function UpdateCollide()
if InvincibleTime > 0 or RecoverStage > 0 or RaceState >= 3 then
return
end
local nxtseg=(PlayerSeg)%NumSegs + 1
-- opponents
local carlen=2+PlayerVl*0.1
local ground = lerp( sPointsY[PlayerSeg], sPointsY[nxtseg], posinseg)
for i=1,#OpptPos do
local opposl = LoopedTrackPos( OpptPos[i] )
if ( Position + PlayerVf ) > ( opposl - carlen + OpptV[i] ) and
( Position + PlayerVf ) < ( opposl + OpptV[i] ) and
ROAD_WIDTH * abs( PlayerX - OpptX[i] ) < 12 and
( PlayerY-ground ) < 2 then
sfx( 7, 2 )
PlayerVl = OpptV[i]
PlayerXd = -sgn(PlayerX) * 0.2
sScreenShake[1],sScreenShake[2] = 4, 2
AddCollisionParticles()
end
end
-- tokens
if sTokensX[nxtseg] != 0 and sTokensExist[nxtseg] != 0 then
hitbox=0.2
if PlayerDrift != 0 then
hitbox=0.25
end
if abs( PlayerX - sTokensX[nxtseg] ) < hitbox and
( Position + carlen + PlayerVf ) > PlayerSeg*SEG_LEN then
sTokensExist[nxtseg] = 0
TokenCollected+=1
if TokenCollected == NumTokens then
sfx( 10, 3 )
else
sfx( 9, 3 )
end
end
end
-- sprites
if SpriteCollideIdx > 0 then --and ( Position + carlen + PlayerVf ) > PlayerSeg*SEG_LEN then
sdef1=SDEF[SpriteCollideIdx]
if sdef1[8]==1 then
-- work out range of pixels in the source sprite that we overlap
-- player is ~40-80px
local insprx1=(48-SpriteCollideRect[1])/SpriteCollideRect[3]
local insprx2=(80-SpriteCollideRect[1])/SpriteCollideRect[3]
it1=flr(max(sdef1[3]*insprx1,0))
it2=flr(min(sdef1[3]*insprx2,sdef1[3]))
collided=0
if sdef1[7]==0 then
for colit=flr(it1), flr(it2)-1 do
if sget(sdef1[1]+colit,sdef1[2]+sdef1[4]-1)!=15 then
collided=1
break
end
end
else
--flipped
for colit=sdef1[3]-flr(it2), (sdef1[3]-flr(it1))-1 do
if sget(sdef1[1]+colit,sdef1[2]+sdef1[4]-1)!=15 then
collided=1
break
end
end
end
if collided == 1 then
if PlayerVf < 4 then
-- small hit
sfx( 7, 2 )
sScreenShake[1],sScreenShake[2] = 3, 1
PlayerVl = PlayerVl * 0.2
PlayerXd = -sgn(PlayerX) * 0.2
InvincibleTime=time()+1
AddParticle( 4, 64 + rnd(32)-16, 96 + rnd( 8 ) )
AddParticle( 5, 64 + rnd(32)-16, 96 + rnd( 8 ) )
else
-- big hit
sfx( 6, 2 )
sScreenShake[1],sScreenShake[2] = 10, 4
PlayerXd = sgn(PlayerX) * 0.2
PlayerVl = PlayerVl * 0.2
RecoverStage = 1
RecoverTimer = time()
AddCollisionParticles()
end
end
end
end
end
function UpdateRaceState()
if RaceState==1 and RaceStateTime() > 3 then
RaceState=2
RaceStateTimer=time()
elseif RaceState==2 and PlayerLap == NUM_LAPS+1 then
RaceState=3
RaceCompleteTime=RaceStateTime()
RaceCompletePos=GetPlayerStanding()
RaceStateTimer=time()
ProfTime=ReadProfile( Level, 3 )
if ProfTime==0 or RaceCompleteTime < ProfTime then
WriteProfile( Level, 3, RaceCompleteTime )
end
ProfStand=ReadProfile( Level, 1 )
if ProfStand==0 or RaceCompletePos < ProfStand then
WriteProfile( Level, 1, RaceCompletePos )
end
if TokenCollected > ReadProfile( Level, 2 ) then
WriteProfile( Level, 2, TokenCollected )
end
end
end
function UpdateRace()
if RaceState < 4 then
-- screenshake
sScreenShake[1]=lerp(sScreenShake[1],0, 0.1)
sScreenShake[2]=lerp(sScreenShake[2],0, 0.1)
if Frame%3 == 0 then
sScreenShake[1]=-sScreenShake[1]
sScreenShake[2]=-sScreenShake[2]
end
if( abs( sScreenShake[1] ) + abs( sScreenShake[2] ) < 1 ) then
sScreenShake = {0,0}
end
UpdatePlayer()
UpdateRecover()
UpdateCollide()
UpdateOpts()
UpdateParticles()
UpdateRaceState()
else
if btnp(🅾️) then
OpenMenu(MenuState)
elseif btnp(❎) then
InitRace()
end
end
end
function _update60()
--DebugUpdate()
Frame=Frame+1
if TitleState == 1 then
UpdateMenus()
elseif TitleState == 2 then
UpdateRace()
end
UpdateRaceSound()
end
function HrzSprite( x, ssx, ssy, f )
hsprdef=HORZSDEF[THEMEDEF[Theme][11]]
ssy=ssy*hsprdef[6]
sspr( hsprdef[1],hsprdef[2],hsprdef[3],hsprdef[4],
(HznOffset + x) % 256 - 128, 64 - flr( ssy * 16 ), ssx*hsprdef[5] * 48, ssy * 16, f )
end
function RenderHorizon()
fillp(0)
rectfill( 0, 64, 128, 128, THEMEDEF[Theme][4] ) -- block out the ground
HrzSprite(10, 1.0, 0.7, true)
HrzSprite(64, 0.3, 1.2, false)
HrzSprite(60, 2.3, 0.3, false)
HrzSprite(128, 1, 1, false)
HrzSprite(178, 1.5, 0.5, true)
end
function RenderSky()
local thm = THEMEDEF[Theme]
fillp(0)
rectfill( 0, 0, 128, 20, thm[10] ) -- block out
BayerRectV( 0, 20, 138, 50, thm[9], thm[10] )
fillp(0)
rectfill( 0, 50, 128, 64, thm[9] ) -- block out
end
function RenderP4( xlt, xrt, xlb, xrb, yt, yb, c )
if yt - yb < 1 then
return
elseif yt - yb < 2 then
line( xlt, yt, xrt, yt, c)
else
yd=yt-yb
rp=1/yd
xldlt=(xlt-xlb)*rp
xrdlt=(xrt-xrb)*rp
for i=yb,yt do
if i > 126 then return end
line( xlb, i, xrb, i, c)
xlb+=xldlt
xrb+=xrdlt
end
end
end
function RenderSeg( x1, y1, w1, x2, y2, w2, idx )
local thm=THEMEDEF[Theme]
-- Ground, We only render intermittent strips, most of the ground has been
-- blocked out in the road render before this
if idx % 8 <= 3 then
fillp(0x5A5A)
RenderP4( -1, x1-w1, -1, x2-w2, y1, y2, thm[5] )
RenderP4( x1+w1, 128, x2+w2, 128, y1, y2,thm[5] )
end
-- Edge
if idx % 4 > 1 then
fillp(0)
col = thm[6]
else
fillp(0x5A5A)
col = thm[7]
end
edgew1=w1*0.86
edgew2=w2*0.86
RenderP4( x1-edgew1, x1-w1,x2-edgew2, x2-w2, y1, y2, col )
RenderP4( x1+w1, x1+edgew1, x2+w2, x2+edgew2, y1, y2, col )
-- Road
fillp(0)
if thm[3] == 1 then
-- stripes
if idx == 1 then
col = 1
else
if idx % 3 == 0 then
fillp(0x5A5A)
col = thm[2]
else
col = thm[1]
end
end
RenderP4( x1-edgew1, x1+edgew1, x2-edgew2, x2+edgew2, y1, y2, col )
elseif thm[3] == 2 then
-- patches
fillp(0x5A5A)
-- TODO: dont overdraw
RenderP4( x1-edgew1, x1+edgew1, x2-edgew2, x2+edgew2, y1, y2, thm[2] )
fillp(0)
if idx == 1 then
col = 1
else
col = thm[1]
end
srand( idx )
rx1=rnd( 0.6 ) + 0.3
rx2=rnd( 0.6 ) + 0.3
RenderP4( x1-edgew1*rx1, x1+edgew1*rx2, x2-edgew2*rx1, x2+edgew2*rx2, y1, y2, col )
end
-- Lanes
if thm[8] == 1 then
-- edge lane
if idx % 2 > 0 then
fillp(0)
RenderP4( x1-w1*0.74, x1-w1*0.78, x2-w2*0.74, x2-w2*0.78, y1, y2, 6 )
RenderP4( x1+w1*0.78, x1+w1*0.74, x2+w2*0.78, x2+w2*0.74, y1, y2, 6 )
end
elseif thm[8] == 2 then
-- centre alternating
if idx % 4 > 2 then
fillp(0)
lanew=0.02
RenderP4(x1-w1*lanew,x1+w1*lanew,x2-w2*lanew,x2+w2*lanew,y1,y2,6 )
end
elseif thm[8] == 3 then
-- 3 lane yellow
if idx % 4 == 0 then
fillp(0)
RenderP4( x1-w1*0.3, x1-w1*0.34, x2-w2*0.3, x2-w2*0.34, y1, y2, 9 )
RenderP4( x1+w1*0.34, x1+w1*0.3, x2+w2*0.34, x2+w2*0.3, y1, y2, 9 )
end
end
end
function _draw()
--cls()
if TitleState == 1 then
RenderMenus()
elseif TitleState == 2 then
if RaceState < 4 then
camera( sScreenShake[1], HUD_HEIGHT + sScreenShake[2] )
RenderSky()
RenderHorizon()
RenderRoad()
camera( 0, 0 )
RenderRaceUI()
else
RenderSummaryUI()
end
end
--DebugRender()
end
function PrintBigDigit( n, x, y,nrend)
x-=2
if not nrend then
poke(0x5f58, 0x1 | 0x80) --custom font
n1=n*2+16+n\8*16
print(chr(n1)..chr(n1+1).."\n"..chr(n1+16)..chr(n1+17),x,y-3,7) --4 chars to print 1 big
poke(0x5f58, 0x0) --default font
end
if (n==1) return 12
return 16
end
function PrintBigDigitOutline( n, x, y, col )
i=n+1
pal( 7, col )
PrintBigDigit( n, x-1, y )
PrintBigDigit( n, x+1, y )
PrintBigDigit( n, x, y-1 )
PrintBigDigit( n, x, y+1 )
pal( 7, 7 )
end
function GetPlayerStanding()
s=#OpptPos+1
for i=1,#OpptPos do
if OpptLap[i] < PlayerLap then
s-=1
elseif OpptLap[i] == PlayerLap and OpptPos[i]<Position then
s-=1
end
end
return s
end
function GetStandingSuffix(n)
stnd=split"st,nd,rd"
if n < 4 then
return stnd[n]
end
return "th"
end
function RenderCountdown()
if RaceState == 2 and RaceStateTime() < 1 then
frac=( time() - RaceStateTimer )%1
x=64-16
PrintBigDigitOutline( 10,x,30, 0 )
PrintBigDigit( 10,x,30)
x=x+16
PrintBigDigitOutline( 0,x,30, 0 )
PrintBigDigit( 0,x,30)
elseif RaceState == 1 then
num= 3-flr( RaceStateTime() )
frac=( RaceStateTime() )%1
if num <= 0 then
return
elseif frac < 0.9 then
x=64-8
PrintBigDigitOutline( num,x,30, 0 )
PrintBigDigit( num,x,30)
end
end
end
function RenderRaceEndStanding()
if (RaceState != 3) return
if RaceStateTime() < 1 then
clip( 0, 0, (RaceStateTime()*8)*128, 128 )
elseif RaceStateTime() > 3 then
clip( ((RaceStateTime()-3)*8)*128, 0, 128, 128 )
end
rectfill( 0, 25, 128, 49, 1 )
tw=PrintBigDigit( RaceCompletePos, 0, 0, 1 )
PrintBigDigit( RaceCompletePos, 53, 32)
print( GetStandingSuffix(RaceCompletePos), 64+tw*0.5-5, 32, 7 )
sspr(unpack(split"120, 16, 7, 18, 37, 27, -7, 18"))
sspr(unpack(split"120, 16, 7, 18, 84, 27, 7, 18"))
clip()
if RaceStateTime() > 3.6 then
fade=max( (0.5-(time()-(RaceStateTimer+3.6)))/0.5, 0 )
BayerRectT( fade, 0, 0, 128, 128, 0xE0 )
if RaceStateTime() > 4.8 then
RaceState = 4
RaceStateTimer=time()
end
end
end
function RenderSummaryUI()
rectfill( 0, 0, 128, 128, 0 )
if RaceStateTime() < 1 then
clip( 0, 0, 128, ((RaceStateTime())*3)*128 )
end
for x=-1,7 do
for y=-1,7 do
off=(Frame*0.5)%16
xoff=x*16+off
yoff=y*16+off
RenderFlag( xoff, yoff, Level )
BayerRectT( max(sin(Frame/120+xoff/53+yoff/63)+1,0.1), xoff, yoff, xoff+10, yoff+7 )
end
end
fillp()
if RaceStateTime() > 1 then
if (RaceStateTime() < 2) then
clip( 0, 0, ((RaceStateTime()-1)*16)*128, 128 )
end
rectfill( 0, 10, 128, 23, 13 )
rectfill( 0, 34, 128, 90, 13 )
line( 0, 33, 128, 33, 1 )
line( 0, 91, 128, 91, 1 )
rectfill( 0, 101, 128, 117, 13 )
fillp(0x33CC)
col = bor( 6 << 4, 0 )
rectfill(0,12,33,21, col)
rectfill(94,12,128,21, col)
RenderTextOutlined( "race complete", 38, 15, 0, 7 )
fillp()
-- position
rectfill(0,39,64,51, 1)
print( "position", 19, 43, 6 )
sspr( 65, 49, 8, 8, 54, 41 ) -- trophy
-- tokens
rectfill(0,56,64,68, 2)
print( "tokens", 27, 60, 6 )
sspr( 17, 121, 7, 7, 55, 59 )
-- time
rectfill(0,73,64,85, 3)
print( "time", 35, 77, 6 )
sspr( 73, 50, 7, 7, 55, 76 )
-- position text
col=RaceCompletePos == 1 and 9 or 7
print( tostr( RaceCompletePos ).. tostr( GetStandingSuffix(RaceCompletePos) ), 69, 43, col )
-- tokens text
col=7
if TokenCollected == NumTokens then
col = 9
end
print( tostr( TokenCollected ).."/".. tostr( NumTokens ), 69, 60, col )
-- time text
PrintTime( RaceCompleteTime, 69, 77 )
-- controls
print( " \142 menu", 45, 104, 6 )
print( " \151 retry", 45, 110, 6 )
clip()
end
end
function RenderRaceUI()
fillp(0)
rectfill( 0,111, 127, 127, 0 )
rect( 0, 111, 127, 127, 6 )
rect( 1, 112, 126, 126, 13 )
stand=GetPlayerStanding()
strlen=PrintBigDigit( GetPlayerStanding(), 3, 114)
print( GetStandingSuffix(stand), strlen+1, 114, 7 )