-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmtk.p8
1656 lines (1560 loc) · 75 KB
/
gmtk.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 36
__lua__
-- untitled dice game
function _init()
-- font
poke(0x5600,unpack(split"8,8,10,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,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,0,0,0,0,0,0,24,24,24,24,0,24,0,0,54,54,18,0,0,0,0,0,54,127,54,127,54,0,0,24,124,30,60,120,62,24,0,6,102,48,24,12,102,96,0,28,54,54,28,110,54,108,0,24,24,8,0,0,0,0,0,24,12,6,6,6,12,24,0,24,48,96,96,96,48,24,0,24,126,60,24,60,126,24,0,0,24,24,126,24,24,0,0,0,0,0,0,0,12,12,6,0,0,0,126,0,0,0,0,0,0,0,0,0,0,12,0,0,96,48,24,12,6,0,0,0,0,60,118,110,102,60,0,0,0,24,28,24,24,60,0,0,0,62,96,60,6,126,0,0,0,62,96,56,96,62,0,0,0,56,60,54,126,48,0,0,0,126,6,62,96,62,0,0,0,60,6,62,102,60,0,0,0,126,96,48,24,12,0,0,0,60,102,60,102,60,0,0,0,60,102,124,96,60,0,0,0,0,12,0,12,0,0,0,0,0,12,0,12,12,6,48,24,12,6,12,24,48,0,0,0,0,126,0,0,0,0,12,24,48,96,48,24,12,0,0,60,102,48,24,0,24,0,0,60,102,118,110,118,60,0,0,0,60,102,126,102,102,0,0,0,62,102,62,102,62,0,0,0,60,102,6,102,60,0,0,0,62,102,102,102,62,0,0,0,126,6,30,6,126,0,0,0,126,6,30,6,6,0,0,0,124,6,118,102,124,0,0,0,102,102,126,102,102,0,0,0,60,24,24,24,60,0,0,0,96,96,96,102,60,0,0,0,102,54,30,54,102,0,0,0,6,6,6,6,126,0,0,0,66,102,126,126,102,0,0,0,102,110,126,118,102,0,0,0,60,102,102,102,60,0,0,0,62,102,62,6,6,0,0,0,60,102,102,54,108,0,0,0,62,102,126,54,102,0,0,0,124,6,60,96,62,0,0,0,126,24,24,24,24,0,0,0,102,102,102,102,60,0,0,0,102,102,102,60,24,0,0,0,102,126,126,102,66,0,0,0,102,60,24,60,102,0,0,0,102,102,60,24,24,0,0,0,126,48,24,12,126,0,62,6,6,6,6,6,62,0,0,6,12,24,48,96,0,0,62,48,48,48,48,48,62,0,24,60,102,0,0,0,0,0,0,0,0,0,0,0,0,126,12,24,48,0,0,0,0,0,0,60,102,102,126,102,102,0,0,62,102,62,102,102,62,0,0,60,102,6,6,102,60,0,0,62,102,102,102,102,62,0,0,126,6,30,6,6,126,0,0,126,6,30,6,6,6,0,0,124,6,118,102,102,124,0,0,102,102,126,102,102,102,0,0,60,24,24,24,24,60,0,0,96,96,96,96,102,60,0,0,102,54,30,54,102,102,0,0,6,6,6,6,6,126,0,0,66,102,126,126,102,102,0,0,102,110,126,118,102,102,0,0,60,102,102,102,102,60,0,0,62,102,102,62,6,6,0,0,60,102,102,102,54,108,0,0,62,102,102,62,54,102,0,0,124,6,60,96,96,62,0,0,126,24,24,24,24,24,0,0,102,102,102,102,102,60,0,0,102,102,102,102,60,24,0,0,102,102,126,126,102,66,0,0,102,60,24,60,102,102,0,0,102,102,60,24,24,24,0,0,126,48,24,12,6,126,0,56,12,12,6,12,12,56,0,24,24,24,24,24,24,24,24,14,24,24,48,24,24,14,0,44,26,0,0,0,0,0,0,0,28,54,28,0,0,0,0,255,255,255,255,255,255,255,255,85,170,85,170,85,170,85,170,0,195,255,189,189,255,126,0,60,126,255,129,195,231,126,60,17,68,17,68,17,68,17,0,4,12,252,124,62,63,48,32,60,110,223,255,255,255,126,60,102,255,255,255,126,60,24,0,24,60,102,231,102,60,24,0,24,24,0,60,90,24,60,102,60,126,255,126,82,82,94,0,60,110,231,227,227,231,110,60,0,255,153,153,255,129,255,0,56,120,216,24,30,31,14,0,0,126,195,219,219,195,126,0,8,28,62,127,62,28,8,0,0,0,0,0,85,0,0,0,60,118,231,199,199,231,118,60,0,8,28,127,62,28,54,0,127,34,20,8,8,20,42,127,60,126,231,195,129,255,126,60,0,5,82,32,0,0,0,0,0,17,42,68,0,0,0,0,0,126,219,231,231,219,126,0,255,0,255,0,255,0,255,0,85,85,85,85,85,85,85,85,255,129,129,129,129,129,129,255,255,195,165,153,153,165,195,255,0,126,62,30,62,118,34,0,8,28,62,127,127,62,8,62,8,28,28,107,127,107,8,28,28,34,73,93,73,34,28,0"))
-- srand(0)
-- enable mouse
poke(0x5f2d, 1)
palt(14,true)
palt(0,false)
left_click=false
prev_click=false
camx=7
camy=0
camera(camx,camy)
frame=0
screen_shake=0
-- game feel variables
roll_cooldown=30
roll_spd=3
roll_duration=10
max_hp=15
max_spd=1.5
acceleration=5 -- immediate max speed
drag=0.5
enemy_wait_shoot=10
--
-- modes:
-- -1: nice title
-- 0: title
-- 10: game
-- 11: game over
-- 12: choose upgrade
-- 13: choose where upgrade
-- 14: win
go_title_screen()
-- mode=-1
end
function go_title_screen()
mode=0
depth=0
t1=t()
t2=t()
mapgen(true)
end
function init_game()
_ents={}
_cmps={}
_msgs={}
depth=0
player=ent()
player+="collide"
player+=cmp("destructible",{hp=max_hp,hitboxx=-6,hitboxy=-7,hitboxw=12,hitboxh=14,force_nb=1})
player+=cmp("can_roll",{curr_roll_cooldown=0})
player+=cmp("pos",{x=-1,y=-1})
player+=cmp("spd",{spdx=0,spdy=0})
player+=cmp("render",{anim=anim_idle,face_left=true,render_order=1})
player+=cmp("dice",{dval=1,dice_mods={},dice_proba={}})
init_proba()
local wpn={}
for i=1,3 do
-- if(rnd()>.1) player.dice_mods[i]=spawn_mod(1)
start_weapon=i==1 and spawn_rifle() or spawn_pistol()
start_weapon.wielder=player
add(wpn,start_weapon)
end
player.dice_mods[3]=spawn_mod(3)
player.dice_mods[5]=spawn_mod(5)
player+=cmp("has_wpn",{curr_wpn=1,wpn=wpn})
mapgen(false)
mode=10
end
-->8
-- ecs and sort
function ent()
-- you can remove this function
-- if you delete the asserts
function check_no_duplicates(self)
for k1,t1 in pairs(self) do
for k2,t2 in pairs(self) do
if k1<k2 then
for f1,_ in pairs(t1) do
for f2,_ in pairs(t2) do
assert(k1==k2 or f1!=f2,"duplicated field "..f1.." in "..k1.." and "..k2)
end
end
end
end
end
end
return add(_ents,
setmetatable({},{
-- check value in components
-- components cannot be accessed directly
__index=function(self,a)
for _,t in pairs(self) do
if(t[a]!=nil) return t[a]
end
assert(false,"field not found:"..a)
end,
__newindex=function(self,a,v)
assert(v!=nil)
for _,t in pairs(self) do
if(t[a]!=nil) t[a]=v return
end
assert(false,"field not found:"..a)
end,
__add=function(self,cmp)
-- two cases: string or table
if type(cmp)=="string" then
assert(rawget(self,cmp)==nil,"already existing: "..cmp)
rawset(self,cmp,{})
add(_cmps[cmp],self)
else
-- check if already existing
assert(rawget(self,cmp._cn)==nil,"already existing: "..cmp._cn)
rawset(self,cmp._cn,cmp)
add(_cmps[cmp._cn],self)
-- remove this function if you remove asserts
-- it's useful but costly
cmp._cn=nil -- technically not required
check_no_duplicates(self)
end
return self
end,
__sub=function(self,cn)
-- double removal is not a problem
rawset(self,cn,nil)
del(_cmps[cn],self)
return self
end}))
end
function cmp(cn,t)
if(_cmps[cn]==nil) _cmps[cn]={}
t._cn=cn
return t
end
function del_ent(e)
for k,_ in pairs(e) do
del(_cmps[k],e)
end
del(_ents,e)
end
function is(e,cn)
return rawget(e,cn)!=nil
end
function sys(cmps,f)
return function(...)
for e in all(_cmps[cmps[1]]) do
for cn in all(cmps) do
if(not rawget(e,cn)) goto _
end
f(e,...)
::_::
end
end
end
-- bubble sort
function sort_ents()
local n=#_ents
for i=1,n do
local swapped=false
for j=2,n-i+1 do
local e1,e2=_ents[j],_ents[j-1]
local y1=rawget(e1,"destructible") and e1.y+e1.hitboxy+e1.hitboxh or 999
local y2=rawget(e2,"destructible") and e2.y+e2.hitboxy+e2.hitboxh or 999
if y1<y2 then
local tmp=_ents[j]
_ents[j]=_ents[j-1]
_ents[j-1]=tmp
swapped=true
end
end
if(not swapped) return
end
end
-- components
--"collide"
--"pos": x y
--"spd": spdx spdy
--"render": anim face_left render_order
--"dice": dval dice_mods dice_proba
--"wpn_stats": wpn_type wpn_dmg cooldown curr_cooldown nb_bullets bull_cooldown curr_bull_cooldown wielder spread bull_speed max_range
--"has_wpn": wpn curr_wpn
--"shooting": bullets_left targetx targety wait_shoot
--"rolling": roll_time
--"bullet": bull_dmg force_src bull_ttl
--"destructible": hp hitboxx hitboxy hitboxw hitboxh force_nb
--"dying": dying_curr
--"sprite": enemy_sprite
--"has_name": name
--"is_mod": mod_id mod_title mod_text
-->8
-- update and systems
function _update()
frame+=1
local click=stat(34)&1>0
if(not prev_click and click) left_click=true
if(prev_click==click) left_click=false
prev_click=click
if(frame>=30) frame=0
if mode==14 then
sys_roll()
sys_wpn_cooldown()
sys_update_pos()
sys_die()
if t()-t1>1 and left_click then
sfx(13)
quit_level()
left_click=false
end
end
if mode==0 or mode==-1 then
if(t()-t2>1.8) t2=t() mapgen(true)
if t()-t1>1 and left_click then
sfx(13)
left_click=false
init_game()
return
end
end
if mode==11 then
sys_roll()
sys_wpn_cooldown()
sys_update_pos()
sys_die()
if t()-t1>1 and left_click then
sfx(13)
left_click=false
go_title_screen()
return
end
end
if mode==10 then
sort_ents()
sys_wpn_cooldown()
player_input()
sys_shoot()
if(frame%5==0) sys_ai_act()
sys_roll()
sys_update_pos()
sys_die()
sys_collision()
-- no more enemies
if _cmps["ai"]==nil or #_cmps["ai"]==0 then
won_level()
end
end
end
function quit_level()
mode=12
player.hp=max_hp
player.anim=anim_idle
sys_del_bullets()
sys_del_dying()
screen_shake=0
local w=player.wpn[player.curr_wpn]
w-="shooting"
w.curr_cooldown=0
_msgs={}
end
function won_level()
mode=14
sfx(43)
t1=t()
generate_loot()
end
function init_proba()
local p={3,3,3}
for i=1,6 do
local id=nil
if player.dice_mods[i] then
id=player.dice_mods[i].mod_id
end
if id==0 then
p[(i-1)\2+1]+=2
elseif id==1 then
p[(i-1)\2+1]-=1
end
end
player.dice_proba=p
end
function roll_dice()
local s=0
for i=1,3 do
s+=player.dice_proba[i]
end
local n=1+flr(rnd(s))
s=0
for i=1,3 do
s+=player.dice_proba[i]
if(n<=s) return 2*i-flr(rnd(2))
end
assert(false)
end
function count_curr_mod(e,i)
local s=0
if e==player then
local d=player.dval
local i1,i2
if(d%2==1) i1=d i2=d+1
if(d%2==0) i1=d i2=d-1
local m1=player.dice_mods[i1]
local m2=player.dice_mods[i2]
if(m1 and m1.mod_id==i) s+=1
if(m2 and m2.mod_id==i) s+=1
end
return s
end
function player_chosed_face(i)
if loot_chosen==1 then
if(player.wpn[i]!=nil) del_ent(player.wpn[i])
player.wpn[i]=loot_w
else
player.dice_mods[i]=loot_m
end
loot_chosen=nil
loot_w=nil
loot_m=nil
mode=10
mapgen()
end
function player_chosed_reward(i)
mode=13
loot_chosen=i
if(i==1) del_ent(loot_m)
if(i==2) del_ent(loot_w)
end
function player_input()
if not is(player,"rolling") then
if(btn(⬆️)) update_spd(0,-1)
if(btn(⬇️)) update_spd(0,1)
if(btn(⬅️)) update_spd(-1,0)
if(btn(➡️)) update_spd(1,0)
local w=player.wpn[player.curr_wpn]
if left_click
and not is(w,"shooting")
and w.curr_cooldown<=0
-- and not is(player,"rolling")
then
shoot(player,w,stat(32)+camx,stat(33)+camy,0)
player.anim=anim_player_shoot
end
-- right click
if stat(34)&2>0 and player.curr_roll_cooldown==0 then
-- no speed, go to mouse
if abs(player.spdx)<.3 and abs(player.spdy)<.3 then
-- todo: ou perpendiculairement?
local dx=stat(32)+camx-player.x
local dy=stat(33)+camy-player.y
local d=1+sqrt(dx*dx+dy*dy)
player.spdx=roll_spd*dx/d
player.spdy=roll_spd*dy/d
else
local spdx=player.spdx
local spdy=player.spdy
local d=sqrt(spdx*spdx+spdy*spdy)
player.spdx=roll_spd*spdx/d
player.spdy=roll_spd*spdy/d
end
player+=cmp("rolling",{roll_time=roll_duration})
sfx(60)
player.curr_roll_cooldown=roll_cooldown
player.anim=anim_roll
-- shooting may be canceled
player.wpn[player.curr_wpn]-="shooting"
end
end
end
function update_spd(dx,dy)
player.spdx+=dx*acceleration
player.spdy+=dy*acceleration
if(player.spdx>max_spd) player.spdx=max_spd
if(player.spdx<-max_spd) player.spdx=-max_spd
if(player.spdy>max_spd) player.spdy=max_spd
if(player.spdy<-max_spd) player.spdy=-max_spd
end
function sys_collision()
for b in all(_cmps["bullet"]) do
for e in all(_cmps["destructible"]) do
-- no friendly fire
if b.force_src!=e.force_nb then
local x=e.x+e.hitboxx
local y=e.y+e.hitboxy
if b.x>=x and b.y>=y
and b.x<x+e.hitboxw
and b.y<y+e.hitboxh then
-- collision!
del_ent(b) -- destroy bullet
if is(e,"rolling") then
add_msg(b,"dodged")
else
local proba_miss=0.20*count_curr_mod(e,4)
if rnd()>=proba_miss then
e.hp-=b.bull_dmg
if(e==player) sfx(41)
add_msg(e,"-"..b.bull_dmg)
else
add_msg(e,"missed")
end
if e.hp<=0 then
if is(e,"ai") then
e+=cmp("dying",{dying_curr=10})
e-="destructible"
e-="ai"
e-="spd"
e.wpn[e.curr_wpn]-="shooting"
e.anim=anim_enemy_die
elseif e==player then
sfx(49)
mode=11
t1=t()
e.anim=anim_player_die
break
end
end
end
break -- one bullet can only hit one target
end
end
end
end
end
function sys_roll()
if(player.curr_roll_cooldown>0) player.curr_roll_cooldown-=1
if is(player,"rolling") then
player.roll_time-=1
if player.roll_time==0 then
player-="rolling"
player.anim=anim_idle
player.dval=roll_dice()
player.curr_wpn=(player.dval-1)\2+1
add_msg(player,"\014"..player.dval.."!")
end
end
end
sys_shoot=sys({"shooting"},
function(e)
if(e.wait_shoot>0) e.wait_shoot-=1
if e.wait_shoot==0 then
while is(e,"shooting") and e.curr_bull_cooldown==0 do
if(e.wielder==player) screen_shake=1
bullet=ent()
local dmg=e.wpn_dmg+count_curr_mod(e.wielder,2)
local bull_speed=e.bull_speed
if(is(e.wielder,"ai")) bull_speed*=1/2
bullet+=cmp("bullet",{force_src=e.wielder.force_nb,bull_dmg=dmg,bull_ttl=ceil(e.max_range/bull_speed)})
local x=e.wielder.x
if e.wielder.face_left then
x-=5
else
x+=5
end
if e.wpn_type==1 or e.wpn_type==3 then
sfx(28)
elseif e.wpn_type==2 then
sfx(29)
elseif e.wpn_type==4 then
sfx(33)
end
bullet+=cmp("pos",{x=x,y=e.wielder.y+3})
bullet+=cmp("render",{anim=anim_bullet,face_left=true,render_order=2})
local dx=e.targetx-x
local dy=e.targety-e.wielder.y
local d=1+sqrt(dx*dx+dy*dy)
local spdx=bull_speed*dx/d
local spdy=bull_speed*dy/d
local spread=max(0,e.spread+count_curr_mod(e,6))
if spread>0 then
local a=rnd(spread*0.01)-spread*0.01/2
-- rotation matrix
local c,s=cos(a),sin(a)
local tmp=spdx*c-spdy*s
spdy=s*spdx+c*spdy
spdx=tmp
end
bullet+=cmp("spd",{spdx=spdx,spdy=spdy})
e.bullets_left-=1
if e.bullets_left==0 then
-- stop shooting
e-="shooting"
if(e.wielder==player) player.anim=anim_idle
if(is(e.wielder,"ai")) e.wielder.anim=anim_enemy_idle
else
e.curr_bull_cooldown=e.bull_cooldown
end
end
if(is(e,"shooting")) e.curr_bull_cooldown-=1
end
end)
sys_wpn_cooldown=sys({"wpn_stats"},
function(e)
if(e.curr_cooldown>0) e.curr_cooldown-=1
end)
sys_die=sys({"dying"},
function(e)
e.dying_curr-=1
if(e.dying_curr==0) del_ent(e)
end)
sys_del_bullets=sys({"bullet"},
function(e)
del_ent(e)
end)
sys_del_dying=sys({"dying"},
function(e)
del_ent(e)
end)
sys_update_pos=sys({"pos","spd","render"},
function(e)
if is(e,"has_wpn") and is(e.wpn[e.curr_wpn],"shooting") then
-- shooting chars do not turn
else
if(e.spdx>0) e.face_left=false
if(e.spdx<0) e.face_left=true
end
local nextx=e.x+e.spdx
local nexty=e.y+e.spdy
-- collision with map
if is(e,"bullet") then
if(e.bull_ttl==0) del_ent(e)
e.bull_ttl-=1
end
if not fget(mget(nextx\8,nexty\8),0) then
e.spdx=0
e.spdy=0
if(is(e,"bullet")) del_ent(e)
else
e.x+=e.spdx
e.y+=e.spdy
if is(e,"collide") then
if(e.x<0) e.x=0 e.spdx=0
if(e.x>120) e.x=120 e.spdx=0
if(e.y<0) e.y=0 e.spdy=0
if(e.y>120) e.y=120 e.spdy=0
-- no drag when rolling
-- only player has drag?
if e==player and not is(e,"rolling") then
if e.spdx>0 then
e.spdx-=drag
if(e.spdx<0) e.spdx=0
elseif e.spdx<0 then
e.spdx+=drag
if(e.spdx>0) e.spdx=0
end
if e.spdy>0 then
e.spdy-=drag
if(e.spdy<0) e.spdy=0
elseif e.spdy<0 then
e.spdy+=drag
if(e.spdy>0) e.spdy=0
end
end
elseif is(e,"bullet") then -- destroy outside map
if(e.x<0 or e.x>130 or e.y<0 or e.y>130) del_ent(e)
end
end
end)
function shoot(e,w,x,y,wait)
e.face_left=x<e.x
local nb_bullets=w.nb_bullets+count_curr_mod(e,3)
w+=cmp("shooting",{bullets_left=nb_bullets,targetx=x,targety=y,wait_shoot=wait})
local cooldown=max(5,w.cooldown-5*count_curr_mod(e,5))
w.curr_cooldown=cooldown
end
-->8
-- draw
function _draw()
if mode==0 then
cls(0)
camera(0,0)
map()
spr(176,2,10,16,4)
spr(112,25,40,11,4)
local s="by cpiod for the gmtk2022"
?s,64-2*#s,122,4
if(t()-t1>1 and t()%1<.7) then
local s="\014cLICK TO sTART"
nprint(s,64-4*14,110,10)
end
local x=80
nprint("arrows to move",nil,x,7)
nprint("mouse to aim",nil,x+7,7)
nprint("left click to shoot",nil,x+14,7)
nprint("right click to roll",nil,x+21,7)
end
if mode==-1 then
cls(0)
camera(0,0)
map()
spr(176,2,30,16,4)
spr(112,25,60,11,4)
end
if mode==10 or mode==11 or mode==14 then
camera(camx,camy)
cls(0)
if screen_shake>0 then
screen_shake-=1
camera(camx+rnd(2)-1,camy+rnd(2)-1)
end
map()
sys_draw(1) -- char
sys_draw(2) -- bullets
-- redraw some walls
map(nil,nil,nil,nil,nil,nil,2)
-- sys_draw_hitbox()
camera(camx,camy)
render_msgs()
if mode==10 then
-- aim
spr(48,stat(32)-3+camx,stat(33)-3+camy)
end
draw_ui()
if(mode==11) then
if(t()-t1>1) then
local s="click to restart"
nprint(s,64-2*#s,80,10)
end
nprint("\014gAME oVER!",64-4*10,50,8)
end
if mode==14 then
if(t()-t1>1) then
local s="click to continue"
nprint(s,64-2*#s,80,10)
end
nprint("\014lEVEL COMPLETE",64-4*14,50,10)
end
elseif mode==12 or mode==13 then
camera(0,0)
draw_reward()
end
end
function draw_ui()
-- bottom
local d=player.dval
local i1,i2
if(d%2==1) i1=d i2=d+1
if(d%2==0) i1=d i2=d-1
local m1=player.dice_mods[i1]
local m2=player.dice_mods[i2]
local s="side "..d
if(m1 or m2) s=s..":"
if(m1) s=s.." "..m1.mod_short
if(m1 and m2) s=s.." &"
if(m2) s=s.." "..m2.mod_short
-- if(m1) s=s..": "..m1.mod_title
-- if(m2 and not m1) s=s..": "..m2.mod_title
-- local s2=""
-- if(m2 and m1) s2=m2.mod_title
nprint(s,30,122,7)
-- nprint(s2,33+4*8,128,7)
local s="l"..depth
nprint(s,9,122,7)
local w=player.wpn[player.curr_wpn]
-- nprint(w.name,10,129,7)
local x=128
-- hp
local y=3
?"♥",x-2,y,8
draw_bar(x,y+7,max_hp-max(0,player.hp),max_hp)
-- weapon
local y=38
local s=158-w.wpn_type*32
if(w.super) s+=1
spr(s,x-3,y,1,2)
draw_bar(x,y+17,w.curr_cooldown,w.cooldown)
-- roll
local y=77
local t={"ro","ll"}
for i=1,#t do
print(t[i],x-2,y+6*i,9)
end
draw_bar(x,19+y,player.curr_roll_cooldown,roll_cooldown)
end
function draw_bar(x,y,val,maxval)
local h=20
local p=val/maxval
if(val<maxval) rectfill(x,y+h+1,x+3,y+1+flr(h*p),7)
rect(x,y,x+3,y+h+2,6)
pset(x,y,0)
pset(x+3,y,0)
pset(x,y+h+2,0)
pset(x+3,y+h+2,0)
end
function draw_reward()
cls(1)
nprint("\014iNVENTORY",5,2,7)
for i=1,3 do
local y=38*i-20
local c=6
local dy=0
if mode==13 then
if stat(32)>1 and stat(32)<60 and stat(33)>y-2 and stat(33)<y+33 then
c=10
dy=2-abs(flr(2*cos(t())+.5))
if loot_chosen==1 and left_click then
sfx(13)
left_click=false
player_chosed_face(i)
return
end
else
c=9
end
end
nprint("sides "..tostr(2*i-1).." & "..tostr(2*i),7,y-dy,c)
-- weapon sprite
local sprite=0
local super=false
if mode==13 and c==10 and loot_chosen==1 then
sprite=loot_w.wpn_type*2+80+16
super=loot_w.super
else
sprite=player.wpn[i].wpn_type*2+80
super=player.wpn[i].super
end
if(super) then
spr(sprite,30,y+7,2,1)
nprint("super",4,y+8,10)
else
spr(sprite,20,y+7,2,1)
end
-- mods
for j=0,1 do
local m=player.dice_mods[2*i-1+j]
local cm=6
local my=y+16+8*j
if loot_chosen==2 and stat(32)>1 and stat(32)<60 and stat(33)>=my and stat(33)<my+8 then
if left_click then
sfx(13)
left_click=false
player_chosed_face(2*i-1+j)
return
end
m=loot_m
cm=10
end
if m==nil then
nprint("nO MOD",2,my,5)
else
nprint(m.mod_title,2,my,cm)
end
end
end
if mode==12 and t()%1<.8 then
nprint("cHOOSE A",75,20,7)
nprint("REWARD!",77,27,7)
elseif mode==13 and t()%1<.8 then
if loot_chosen==1 then
nprint("wHICH SIDE",75,20,7)
nprint("FOR THIS GUN?",70,27,7)
else
nprint("wHICH SIDE",75,20,7)
nprint("FOR THIS MOD?",70,27,7)
end
end
local w=64
local h={30,14+8*#loot_m.mod_text}
local x=60
local y={40,100-h[2]/2}
local c={}
for i=1,2 do
-- highlight
if mode==12 or (mode==13 and loot_chosen==i) then
if mode==13 or (stat(32)>x and stat(32)<x+w and stat(33)>y[i] and stat(33)<y[i]+h[i]) then
if mode==12 and left_click then
player_chosed_reward(i)
left_click=false
sfx(13)
return
end
c[i]=10
else
c[i]=9
end
for dx=-1,1 do
rect(x-dx,y[i]-dx,x+w+dx,y[i]+h[i]+dx,dx==0 and c[i] or 0)
end
nprint("reward "..i,x+18,y[i]-2,c[i])
end
end
-- weapon
if mode==12 or loot_chosen==1 then
sspr(loot_w.wpn_type*16,40,16,8,x+10,y[1]+3,32,16)
local col=c[1]
if(loot_w.super and t()%1<.3) col=8
nprint(loot_w.name,x+1+w/2-#loot_w.name*2,y[1]+21,col)
end
-- modifier
if mode==12 or loot_chosen==2 then
nprint(loot_m.mod_title,x+1+w/2-#loot_m.mod_title*2,y[2]+6,c[2])
local my=y[2]+14
for i=1,#loot_m.mod_text do
nprint(loot_m.mod_text[i],x+4,my,7)
my+=8
end
end
-- mouse
spr(9,stat(32)-1,stat(33)-1)
end
function nprint(s,x,y,c)
x=x or 64-2*#s
for dx=-1,1 do
for dy=-1,1 do
?s,x+dx,y+dy,0
end
end
?s,x,y,c
end
sys_draw=sys({"render","pos"},
function(e,layer)
if e.render_order==layer then
e.anim(e)
if is(e,"has_wpn") and not is(e,"rolling") then
local w=e.wpn[e.curr_wpn]
if e.face_left then
spr(w.wpn_type*2+80,e.x-14,e.y,2,1,true)
else
spr(w.wpn_type*2+80,e.x-2,e.y,2,1)
end
end
end
end)
sys_draw_hitbox=sys({"destructible","pos"},
function(e)
local x=flr(e.x)+e.hitboxx
local y=flr(e.y)+e.hitboxy
rect(x,y,x+e.hitboxw-1,y+e.hitboxh-1,9)
pset(e.x,e.y,12)
end)
function draw_base(e,face,dead)
local l1,l2
local x,y=flr(e.x),flr(e.y)
if e.spdx==0 and e.spdy==0 then
l1,l2=35,35
else
local n=flr((t()%.3)*4/.3)
if(n==0) l1,l2=33,34
if(n==1 or n==3) l1,l2=35,35
if(n==2) l1,l2=34,33
end
local dy=t()%1<.35 and 1 or 0
-- legs
if not dead then
if e.face_left then
spr(l1,x-7,y)
spr(l2,x-3,y)
else
spr(l1,x-1,y,1,1,true)
spr(l2,x-5,y,1,1,true)
end
else
dy=2
end
-- base
spr(1,x-8,y-8+dy,2,2,e.face_left)
if e.face_left then
-- number and face
spr(9+e.dval,x+3,y-3+dy)
spr(face,x-5,y-2+dy)
else
spr(9+e.dval,x-6,y-3+dy)
spr(face,x-3,y-2+dy,1,1,true)
end
end
function anim_idle(e)
draw_base(e,3)
end
function anim_player_die(e)
draw_base(e,4,true)
end
function anim_player_shoot(e)
draw_base(e,5)
end
function anim_roll(e)
local s=20+2*flr((t()%.1)*3/.1)
spr(s,e.x-8,e.y-8,2,2,not e.face_left)
end
---------------------
function anim_bullet(e)
-- bullet have no sprite symmetry
local a=abs(e.spdx/e.spdy)
local s=0
if(e.force_src==1) s=16
if a<2 and a>0.5 then
if e.spdx*e.spdy<0 then
spr(s+29,e.x,e.y)
else
spr(s+28,e.x,e.y)
end
elseif abs(e.spdx)>abs(e.spdy) then
spr(s+26,e.x,e.y)
else
spr(s+27,e.x,e.y)
end
end
---------------------
function anim_enemy_idle(e)
spr(e.enemy_sprite+50,e.x-3,e.y-5,1,2,not e.face_left)
end
function anim_enemy_move(e)
anim_enemy_idle(e)
end
function anim_enemy_die(e)
spr(e.enemy_sprite+51,e.x-3,e.y-5,1,2,not e.face_left)
end
function anim_enemy_shoot(e)
spr(e.enemy_sprite+49,e.x-3,e.y-5,1,2,not e.face_left)
end
-- msg
function add_msg(e,str,col)
add(_msgs,{x=e.x+1,y=e.y-6,d=5,s=str,c=col or 7})
end
function render_msgs()
for m in all(_msgs) do
if m.d>0 then
m.d-=1
for dx=-1,1 do
for dy=-1,1 do
?m.s,m.x+dx,m.y+dy,0
end
end
?m.s,m.x,m.y,m.c
m.y-=1
end
end
end
-->8
-- spawn
function spawn_enemy(x,y,enemy_type)
local hp,w,ai_type