-
Notifications
You must be signed in to change notification settings - Fork 0
/
pilot8.p8
2111 lines (2034 loc) · 68.9 KB
/
pilot8.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 16
__lua__
-- pilot8
-- bruno oliveira
--
-- this source code is pre-processed
-- (comments removed and constants inlined)
-- for the more readable original, check:
-- https://github.com/btco/pilot8
local k_lit_clip_r={x=-25,y=-30,w=95,h=60}
local k_rnd=nil
local k_drop_int={300,200}
local k_cr_full={x=0,y=0,w=8,h=8}
local k_cr_plr={x=2,y=2,w=6,h=6}
local k_cr_ship={x=2,y=2,w=6,h=6}
local k_cr_hbar={x=2,y=3,w=6,h=2}
local k_cr_hbar_s={x=3,y=3,w=2,h=1}
local k_cr_dbl={x=1,y=2,w=6,h=3}
local k_cr_ball={x=3,y=3,w=2,h=2}
local k_cr_ball4={x=2,y=2,w=4,h=4}
local k_cr_pwup={x=0,y=0,w=8,h=7}
local k_mus_for_mode={
[0]=5,
[1]=0,
[3]=7,
[2]=10,
[7]=11,
}
local g_mus=-1
local k_drop_sched={
18,
39,
47,
0,
}
local k_hint_char_color={
["\139"]=12,["\145"]=12,["\148"]=12,["\131"]=12,
["\142"]=11,["\151"]=8
}
local k_scrt_seq={11,7,5,3,2,1}
local g_scrt=nil
local g_lum=nil
local k_anim_die={div=6,fr={12,13,14,15,16,0}}
local k_anim_beacon={div=10,fr={0,30,31},loop=true}
local k_anim_spiky={div=4,fr={26,27},loop=true}
local k_anim_fireball=
{div=2,fr={24,24},flip={0,1},loop=true}
local k_anim_torch={div=8,fr={122,123,123},loop=true}
local k_anim_lantern={div=8,fr={84,100,116},loop=true}
local k_anim_muzzle={div=2,fr={87,88,103,104},loop=false}
local k_lvl_templ_flood={
title="flood city",
bgc=112,bgr=48,bgh=12,
thc=116,thr=29,
cclr=0,
lit=true,
em={
{anim=k_anim_beacon,x=0,y=88,cmap={[3]=2,[11]=8}},
{anim=k_anim_beacon,x=32,y=88,cmap={[3]=4,[11]=9}},
{anim=k_anim_beacon,x=88,y=88},
},
rip_clrs=0x11,
max_enemies=3,
}
local k_lvl_templ_oasis={
title="oasis city",
lit=false,
cclr=1,
bgc=64,bgr=48,bgh=12,
thc=116,thr=26,
skfx={{sx=0,sy=72,c=64,r=61,w=16,h=3}},
sun={x=64,y=134,r=48,clr=15},
rip_clrs=0xdd,
max_enemies=4,
}
local k_lvl_templ_marshes={
title="marshes",
lit=true,
cclr=0,
bgc=32,bgr=48,bgh=12,
thc=116,thr=23,
rip_clrs=0x00,
em={
{anim=k_anim_lantern,x=8,y=88},
{anim=k_anim_lantern,x=48,y=88},
{anim=k_anim_lantern,x=96,y=88},
},
rain=1,
max_enemies=5,
}
local k_lvl_templ_coast={
title="coast",
lit=false,
cclr=0,
bgc=16,bgr=54,bgh=6,bgy=48,
thc=104,thr=29,
rip_clrs=0x11,
skfx={
{sx=0,sy=48,c=16,r=48,w=16,h=6},
{sx=0,sy=0,c=16,r=61,w=16,h=3},
},
sun={x=64,y=114,r=32,clr=7},
owfx={sx=0,sy=96,c=16,r=60,w=16,h=1},
max_enemies=5,
}
local k_lvl_templ_canals={
title="canals",
lit=true,
cclr=0,
bgc=0,bgr=48,bgh=12,
thc=104,thr=26,
rip_clrs=0x11,
em={
{anim=k_anim_torch,x=8,y=88},
{anim=k_anim_torch,x=4*8,y=88},
{anim=k_anim_torch,x=7*8,y=88},
{anim=k_anim_torch,x=9*8,y=88},
{anim=k_anim_torch,x=12*8,y=88},
{anim=k_anim_torch,x=13*8,y=88},
},
max_enemies=6,
}
local k_lvl_templ_castle={
title="castle",
lit=true,
cclr=0,
bgc=48,bgr=48,bgh=12,
thc=104,thr=23,
rip_clrs=0x00,
em={
{anim=k_anim_torch,x=8,y=88},
{anim=k_anim_torch,x=40,y=88},
{anim=k_anim_torch,x=56,y=88},
{anim=k_anim_torch,x=88,y=88},
},
max_enemies=7,
}
local g_lvls={
{
templ=k_lvl_templ_flood,
mapc=1,mapr=4,
text="know how to fly? no? well, \nwhat better way to learn than\non a real mission with real \nenemies? good luck! ",
},
{
templ=k_lvl_templ_flood,
mapc=3,mapr=4,
rain=1,
text="congratulations on not dying.\nyou exceeded our expectations\nplease continue with your \ncurrent strategy of not dying",
},
{
templ=k_lvl_templ_flood,
mapc=5,mapr=4,
text="bob left the gates open last \nnight and enemies returned to\nthe area. bob says sorry.",
},
{
templ=k_lvl_templ_oasis,
mapc=8,mapr=3,
text="the oasis area has strategic \nsignificance to us. we don't \nknow why yet. but go reclaim \nit anyway. use sunscreen. ",
},
{
templ=k_lvl_templ_oasis,
mapc=10,mapr=3,
text="stellar performance! great...\noh? you are pilot 8, not 9. \nsorry, nevermind. how awkward\noff you go. good luck. ",
},
{
templ=k_lvl_templ_oasis,
mapc=12,mapr=3,
text="leadership accepted a statue \nof a horse as a gift from the\nenemy. in an unrelated event,\nsomehow the enemies got in. ",
},
{
templ=k_lvl_templ_marshes,
mapc=1,mapr=8,
rain=1,
text="it's a great night to stay by\nthe fireplace. so leadership \nwill stay by the fireplace \nwhile you go do the work. ",
},
{
templ=k_lvl_templ_marshes,
mapc=3,mapr=8,
text="bad news: the cheap defenses \nwe set up were not waterproof\nso the enemies are back. go \nreclaim the area for us. ",
},
{
templ=k_lvl_templ_marshes,
mapc=5,mapr=8,
text="the last pilot was supposed \nto reclaim this area but got \nrecruited by a startup and \nleft. it's up to you now. ",
},
{
templ=k_lvl_templ_coast,
mapc=10,mapr=9,
text="the pilot we hired turned out\nto be a werewolf so he can't \nfly under a full moon. so \nyou will have to go instead. ",
},
{
templ=k_lvl_templ_coast,
mapc=12,mapr=9,
text="bob sent an email to the \nwrong mailing list and gave \naway our coordinates, so the \nenemies found us. thanks bob.",
},
{
templ=k_lvl_templ_coast,
mapc=14,mapr=9,
text="the ideal vacation spot! \nthe beach, the waves, .... \nexcept there are a bunch of \nenemies, so take care of that",
},
{
templ=k_lvl_templ_canals,
text="we needed the area due to its\ncanals, but remembered we're \na spacefaring society with no\nuse for canals. get it anyway",
mapc=2,mapr=13,
},
{
templ=k_lvl_templ_canals,
mapc=4,mapr=13,
rain=1,
text="we mistook ourselves for the \nenemy so we left the area to \nclear it of enemies. then the\nreal enemies took it from us.",
},
{
templ=k_lvl_templ_canals,
mapc=6,mapr=13,
text="alex set the alarm to 7pm not\n7am so we overslept and lost \nthe area to enemies. they are\nvery punctual, unlike alex. ",
},
{
templ=k_lvl_templ_castle,
mapc=9,mapr=13,
text="leadership wants a castle in \norder to fortify our defenses\nit worked in the middle ages \nso it seems like a good idea.",
},
{
templ=k_lvl_templ_castle,
mapc=11,mapr=13,
text="because they are all spelled \nsimilarly, leadership has \ninvaded the wrong casle. now \ngo conquer the right one. ",
},
{
templ=k_lvl_templ_castle,
mapc=13,mapr=13,
rain=1,
text="okay, that wasn't the right \ncastle either but this time \nwe are really sure. last one!\ngood luck, pilot 8! ",
},
}
local k_weap_info={
[0]={
init_ammo={-1,-1,-1},
shot_eid=1000,
off_x={-6,6},off_y=0,
sfx=10,
fire_cds={12,9,6},
hud_icon=17,
clr=12,
},
[1]={
init_ammo={5,7,10},
shot_eid=38,
off_x={-6,6},off_y=0,
sfx=15,
fire_cds={20,15,10},
hud_icon=18,
clr=11,
},
[2]={
init_ammo={50,60,70},
shot_eid=1002,
off_x={-6,6},off_y=0,
sfx=15,
fire_cds={5,4,3},
hud_icon=47,
clr=14,
},
}
local g_mode=0
local g_clk_sec=0
local g_clk=0
local g_aclk=0
local g_sav=nil
local g_sav_templ={
lvl=1,
coins=0,
upgrades=0,
coins_collected=0,
coins_spent=0,
enemies_killed=0,
levels_played=0,
deaths=0,
powerups_collected=0,
gameplay_minutes=0,
}
local g=nil
local k_g_template={
lvl_no=-1,
lvl=nil,
cmd_cd=0,
next_cmd=0,
play_clk=0,
lit_clip={x=0,y=0,w=0,h=0},
drop_cd=k_drop_int[1],
x=10,y=40,
vx=0,vy=0,
facing=1,
xcr=nil,
fire_cd=0,
weap=0,
weap_info=k_weap_info[0],
ammo=-1,
shield=0,
shield_anim_cd=0,
hurt_cd=0,
ents={},
num_enemies=0,
num_pwups=0,
num_en_projs=0,
end_cd=nil,
muzzle_cc=nil,
}
local k_dmg_cmap={}
for i=1,7 do add(k_dmg_cmap,7) end
local g_sstate={
sel=1,
map={
sel_lvl=nil,
prompt=false,
},
shop={
mode=0,
scr_y=0,
avail=nil,
}
}
local k_upgrade_entries={
{
upf=0x0002,
title="rapid fire i",
desc="faster lasers",
price=10,
},
{
upf=0x0004,
title="rapid fire ii",
desc="even faster lasers",
price=20,
dep=0x0002,
},
{
upf=0x0010,
title="ammo i",
desc="increased ammo",
price=10,
},
{
upf=0x0020,
title="ammo ii",
desc="even more ammo",
price=20,
dep=0x0010,
},
{
upf=0x0080,
title="shield i",
desc="start with shield",
price=15,
},
{
upf=0x0100,
title="shield ii",
desc="start with 2x shield",
price=20,
dep=0x0080,
},
{
upf=0x0200,
title="shield iii",
desc="start with 3x shield",
price=30,
dep=0x0100,
},
{
upf=0x0400,
title="green laser",
desc="start with green laser",
price=30,
},
{
upf=0x0800,
title="more powerups",
desc="get powerups more often",
price=15,
},
}
local k_e_template={
eid=0,
sp=nil,
x=0,y=0,
vx=0,vy=0,
age=0,
facing=-1,
face_plr=false,
bounce_x=false,
vx_div=1,vy_div=1,
cr=k_cr_full,
dead=false,
em_sp=nil,
ttl=nil,
blink_ttl=nil,
fires=false,
fire_eid=1001,
fire_projs={
{off_x_left=-6,off_x_right=6,off_y=0,vx_facing=true}
},
fire_int_min=60,fire_int_max=100,
fire_cd=nil,
dmg_plr=nil,
dmg_en=nil,
dmg_cd=0,
hp=nil,
anim=nil,
em_anim=nil,
enemy=false,
en_proj=false,
cmap=nil,
zigzags=false,
zigzag_v=1,
spawn_y=nil,
}
local g_ent_defs={
[0]={},
[1]={em_sp=17},
[1000]={
sp=0,
em_sp=2,
vx=5,
cr=k_cr_hbar,
ttl=25,
dmg_en=1,
},
[1001]={
sp=0,
em_anim={fr={9,46},div=4,loop=true},
vx=-1,
cr=k_cr_hbar_s,
dmg_plr=1,
ttl=100,
en_proj=true,
},
[7]={
em_sp=8,
enemy=true,
cr=k_cr_ship,
vx=-1,
vx_div=2,
vy_div=2,
fire_int_min=30,
fire_int_max=45,
bounce_x=true,
zigzags=true,
dmg_plr=1,
hp=3,
face_plr=true,
},
[12]={
sp=0,
ttl=5,
vx=-1,vx_div=2,
},
[22]={
em_sp=23,
enemy=true,
vx=-1,
vx_div=3,
bounce_x=true,
cr=k_cr_half,
fires=true,
fire_eid=24,
fire_projs={
{off_x_left=-1,off_x_right=2,off_y=-2,vx_facing=true},
},
fire_int_min=55,
fire_int_max=75,
dmg_plr=1,
hp=3,
face_plr=true,
spawn_y=96-8,
},
[24]={
em_anim=k_anim_fireball,
vy=-1,
vx=-1,
vx_div=2,
vy_div=2,
cr=k_cr_ball,
dmg_plr=1,
ttl=120,
en_proj=true,
},
[10]={
em_sp=11,
enemy=true,
cr=k_cr_ship,
fires=true,
fire_int_min=40,fire_int_max=60,
laser_clr=10,
bounce_x=true,
zigzags=true,
vx=-1,
vx_div=3,
vy_div=2,
dmg_plr=1,
hp=3,
face_plr=true,
},
[18]={
em_sp=37,
ttl=120,
blink_ttl=60,
pwup=true,
cr=k_cr_pwup,
vx=-1,vx_div=2,
},
[47]={
em_sp=112,
ttl=120,
blink_ttl=60,
pwup=true,
cr=k_cr_pwup,
vx=-1,vx_div=2,
},
[38]={
sp=0,
em_sp=38,
vx=3,
cr=k_cr_dbl,
ttl=25,
dmg_en=3,
},
[39]={
em_sp=40,
ttl=120,
blink_ttl=60,
pwup=true,
cr=k_cr_pwup,
vx=-1,vx_div=2,
},
[20]={
em_sp=42,
enemy=true,
cr=k_cr_ship,
fires=true,
fire_int_min=30,fire_int_max=45,
laser_clr=8,
bounce_x=true,
vx=-1,
vx_div=30,
dmg_plr=1,
hp=9,
zigzags=true,
face_plr=true,
},
[1]={
em_sp=1,
ttl=120,
blink_ttl=60,
pwup=true,
cr=k_cr_pwup,
vx=-1,vx_div=2,
},
[21]={
em_anim=k_anim_spiky,
enemy=true,
cr=k_cr_ball4,
fires=true,
fire_eid=24,
fire_int_min=30,fire_int_max=45,
fire_projs={
{off_x=-1,off_y=-1,vx=-1,vy=-1},
{off_x=-1,off_y=2,vx=-1,vy=1},
{off_x=2,off_y=-1,vx=1,vy=-1},
{off_x=2,off_y=2,vx=1,vy=1},
},
laser_clr=8,
bounce_x=true,
vx=-1,
dmg_plr=1,
hp=1,
zigzags=true,
},
[43]={
em_sp=45,
anim={fr={43,44},div=2,loop=true},
enemy=true,
cr=k_cr_ship,
fires=true,
fire_int_min=15,fire_int_max=30,
laser_clr=11,
bounce_x=true,
vx=-1,
vx_div=2,
dmg_plr=1,
hp=15,
zigzags=true,
zigzag_v=1,
face_plr=true,
},
[1002]={
sp=0,
em_sp=9,
vx=3,
cr=k_cr_hbar,
ttl=25,
dmg_en=1,
cmap={[12]=14},
},
}
local g_loaded_data=false
function _init()
cartdata("btco_pilot8_v1")
init_rnd()
init_lvls()
load_data()
init_lum()
set_mode(0)
end
function init_rnd()
k_rnd={}
for i=1,64 do
add(k_rnd,irnd_incl(0,31))
end
end
function init_lvls()
for i=1,#g_lvls do
local lvl=g_lvls[i]
g_lvls[i]=overlay(lvl.templ,lvl)
g_lvls[i].templ=nil
end
local last_lvl=nil
for r=0,31,6 do
for c=0,128 do
local t=mget(c,r)
if t==113 then
local lvl_no_tens=meta_value(mget(c,r+1))
local lvl_no_ones=meta_value(mget(c,r+2))
assert(lvl_no_tens and lvl_no_ones)
local lvl_no=lvl_no_tens*10+lvl_no_ones
last_lvl=g_lvls[lvl_no]
last_lvl.mc0=c
last_lvl.mr0=r
elseif t==48+15 and last_lvl then
last_lvl=nil
end
end
last_lvl=nil
end
for lvl in all(g_lvls) do
assert(lvl.mc0 and lvl.mr0)
end
end
function _update()
g_clk_sec+=0x0.08
g_clk=band(g_clk+1,0x7fff)
g_aclk=band(g_aclk+1,0x7fff)
g_btn_left,g_btn_right,g_btn_up,g_btn_down,g_btn_x,g_btn_o=
btn(0),btn(1),btn(2),
btn(3),btn(5),btn(4)
g_btnp_left,g_btnp_right,g_btnp_up,g_btnp_down,g_btnp_x,g_btnp_o=
btnp(0),btnp(1),btnp(2),
btnp(3),btnp(5),btnp(4)
if g_aclk%225==224 then g_sav.gameplay_minutes+=0.125 end
local fun=g_update_fun[g_mode]
assert(fun)
fun()
end
function update_title()
if g_btnp_o then
set_mode(3)
end
end
function update_play()
g.play_clk=g_clk
update_plr()
update_lvl()
update_ents()
collide_ents()
end
function update_dying()
if g_clk_sec>4 then
set_mode(3)
end
end
function update_hangar()
local h=g_sstate
local dx,dy=get_dpadp()
h.sel=mid(h.sel+dy,1,4)
if g_btnp_o then
if h.sel==1 then
start_lvl(g_sav.lvl)
elseif h.sel==2 then
start_map()
elseif h.sel==3 then
start_shop()
elseif h.sel==4 then
set_mode(6)
end
elseif g_btnp_x then
set_mode(0)
end
end
function start_map()
local state=g_sstate.map
state.sel_lvl=g_sav.lvl
state.prompt=false
set_mode(5)
end
function start_shop()
local shop=g_sstate.shop
shop.scr_y=0
shop.mode=0
shop.avail={}
for i=1,#k_upgrade_entries do
local e=k_upgrade_entries[i]
if not has_upgrade(e.upf) then
if not e.dep or has_upgrade(e.dep) then
add(shop.avail,e)
end
end
end
set_mode(4)
end
function update_shop()
local shop=g_sstate.shop
local sel_entry=#shop.avail>0 and
shop.avail[g_sstate.sel] or nil
if shop.mode==1 then
if g_btnp_x then
shop.mode=0
return
end
if g_btnp_o then
grant_upgrade(sel_entry.upf)
g_sav.coins=max(0,g_sav.coins-sel_entry.price)
g_sav.coins_spent+=sel_entry.price
save_data()
shop.mode=3
end
return
elseif shop.mode==2 then
if g_btnp_x or g_btnp_o then
shop.mode=0
end
return
elseif shop.mode==3 then
if g_btnp_x or g_btnp_o then
start_shop()
end
return
end
assert(shop.mode==0)
local sel_spos=
(g_sstate.sel-1)*24+shop.scr_y
if sel_spos<0 then
shop.scr_y=min(0,shop.scr_y+4)
elseif sel_spos>24*3 then
shop.scr_y-=4
end
if g_btnp_x then
set_mode(3)
return
end
if #shop.avail==0 then return end
local dx,dy=get_dpadp()
g_sstate.sel=mid(g_sstate.sel+dy,1,#shop.avail)
if g_btnp_o then
shop.mode=sel_entry.price>g_sav.coins and
2 or 1
end
end
function update_map()
local state=g_sstate.map
if state.prompt then
if g_btnp_o then
start_lvl(state.sel_lvl)
return
end
if g_btnp_x then
state.prompt=false
return
end
end
if g_btnp_o then
if state.sel_lvl~=g_sav.lvl then
state.prompt=true
return
else
set_mode(3)
end
elseif g_btnp_x then
set_mode(3)
return
end
local dx,dy=get_dpadp()
state.sel_lvl=mid(state.sel_lvl+dx,1,g_sav.lvl)
end
function update_ents()
local ents=g.ents
local num_enemies,num_pwups,num_en_projs=0,0,0
for i=#ents,1,-1 do
local e=ents[i]
ent_update(e)
if e.dead then
fast_del(ents,i)
elseif e.enemy then
num_enemies+=1
elseif e.en_proj then
num_en_projs+=1
elseif e.pwup then
num_pwups+=1
end
end
g.num_enemies=num_enemies
g.num_pwups=num_pwups
g.num_en_projs=num_en_projs
end
function collide_ents()
local ents=g.ents
for i=1,#ents do
local e=ents[i]
if e.dmg_plr then check_coll_dmg_plr(e) end
if e.dmg_en then check_coll_dmg_en(e) end
if e.pwup then check_coll_pwup(e) end
end
end
function ent_get_cr(e)
return rect_xlate(e.cr,e.x,e.y)
end
function check_coll_dmg_plr(e)
if g.hurt_cd>0 then return end
local ecr=ent_get_cr(e)
local pcr=g.xcr
if not rect_isct(ecr,pcr) then return end
if g.shield>0 then
g.shield-=1
g.hurt_cd=40
sfx(18)
else
start_dying()
end
end
function check_coll_pwup(e)
local ecr=ent_get_cr(e)
local pcr=g.xcr
if not rect_isct(ecr,pcr) then return end
pickup_pwup(e)
e.dead=true
end
function pickup_pwup(e)
if e.eid==18 then
pickup_weap(1)
elseif e.eid==39 then
pickup_shield()
elseif e.eid==1 then
pickup_coin()
elseif e.eid==47 then
pickup_weap(2)
end
g_sav.powerups_collected+=1
end
function pickup_weap(weap_id,play_sfx)
play_sfx=play_sfx or true
local wi=k_weap_info[weap_id]
assert(wi)
g.weap=weap_id
g.weap_info=wi
local upg_level=get_ammo_upg_lvl()
g.ammo=wi.init_ammo[upg_level+1]
if play_sfx and weap_id~=0 then
sfx(16)
end
end
function pickup_shield()
g.shield=min(g.shield+1,3)
g.shield_anim_cd=30
sfx(17)
end
function pickup_coin()
g_sav.coins+=1
g_sav.coins_collected+=1
save_data()
sfx(19)
end
function start_dying()
sfx(13)
set_mode(2)
g_sav.deaths+=1
end
function check_coll_dmg_en(e)
local ents=g.ents
local ecr=ent_get_cr(e)
for i=1,#ents do
local victim=ents[i]
if victim.hp then
local vcr=ent_get_cr(victim)
if rect_isct(vcr,ecr) then
e.dead=true
hurt_enemy(victim,e.dmg_en)
end
end
end
end
function hurt_enemy(target,dmg)
target.hp-=dmg
target.dmg_cd=3
target.dead=target.dead or target.hp<1
sfx(target.dead and 12 or 11)
if target.dead then
ent_add(12,target.x,target.y)
maybe_drop_loot(target,target.x,target.y)
g_sav.enemies_killed+=1
end
end
function maybe_drop_loot(e,x,y)
if g.drop_cd>0 then
maybe_drop_coin(e,x,y)
return
end
if g.num_pwups>2 then return end
if g.weap~=0 then return end
local i=irnd_incl(1,#k_drop_sched)
if k_debug_force_drop then i=k_debug_force_drop end
local eid=k_drop_sched[i]
if not eid or eid==0 then
maybe_drop_coin(e,x,y)
return
end
ent_add(eid,x,y)