-
Notifications
You must be signed in to change notification settings - Fork 9
/
self.p8
1940 lines (1702 loc) · 58.1 KB
/
self.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 8
__lua__
time_colors={11,3,4,9,10,14,2,8}
-- debug shit can be deleted
function table_to_string(tbl)
if tbl == nil then
return "nil"
end
if type(tbl) == "boolean" then
return tbl and "true" or "false"
end
if type(tbl) == "table" then
local retval = " table{ "
for k, v in pairs(tbl) do
retval = retval .. k .. ": ".. table_to_string(v).. ","
end
retval = retval .. "} "
return retval
end
return ""..tbl
end
function _init()
g_tick=0
g_ct=0 --controllers
g_ctl=0 --last controllers
g_cs = {} --camera stack
g_scroffset = 128 --scrolling
g_scrspeed = 0
g_scrline = 40
g_airdragmod=0.5
g_state = 0 --don't play
g_objs = {}
g_uiobjs = {}
-- make_title()
game_start()
end
function update_collision(o1,o2)
local o1s = o1.speed
local o2s = o2.speed
if o1.speedy <= 0 then
o1.speed*=-1
o1.x+=(o1.speed*2)
else
o1.speedy*=-1
o1.y+=o1.speedy
--straight down bounce
if o1s==0 then
if o1.direction==1 then
o1s=-2
else
o1s=2
end
end
end
if o2.speedy <= 0 then
o2.speed*=-1
o2.x+=(o2.speed*2)
else
o2.speedy*=-1
o2.y+=o2.speedy
--straight down bounce
if o1s==0 then
if o1.direction==1 then
o1s=-2
else
o1s=2
end
end
end
if o1s == 0 then
o1.speed = o2s
end
if o2s == 0 then
o2.speed = o1s
end
end
function is_holding(obj, held)
return (
obj.will_hold and
held.is_holdable and
(held.held_by == nil or
held.held_by == obj)
)
end
function _update()
g_tick = max(0,g_tick+1)
-- current/last controller
g_ctl = g_ct
g_ct = btn()
animate_tiles()
foreach(g_uiobjs, function(t)
if t.update then
t:update(g_uiobjs)
end
end)
if g_state == 0 then
return
end
-- disabling for now to test other mechanic
if false and g_tick % 6 == 0 then
g_timer -= 1
end
if (g_timer < 1) then
g_state = 0
add(g_uiobjs, make_menu(
{'retry', 'huh'},
function(t,i,s)
--del(s,t)
add(s, make_trans(
function()
game_start()
end))
end,
64,64
))
--cls()
-- print("game over.")
--print("height: "..128-g_scroffset)
--stop()
end
if shouldscroll() then
g_scrspeed = -4
else
g_scrspeed = min(
0,g_scrspeed+0.5)
end
scrollby(g_scrspeed)
for v in all(g_violets) do
if v.y > 127 and not v.off then
v.off = g_tick
v.y = 140
--v.x = 60
v.speed=0
v.speedy=0
v.will_hold=false
end
v:update()
end
for v in all(g_blocks) do
if v.y > 128 then
v.y = -8
--v.x = 20
end
end
foreach(g_violets, update_phys)
foreach(g_blocks, update_phys)
foreach(g_objs,update_phys)
collide(
g_violets[1],
g_violets[2])
-- todo - accel structures?
for v in all(g_violets) do
for b in all(g_blocks) do
collide(v, b)
end
for o in all(g_objs) do
if o.getrect and
rectintersect(
v:getrect(),
o:getrect()) then
if o.pickup then
o:pickup(v)
end
end
end
end
foreach(g_violets, update_held)
end
function animate_tiles()
--experiment with animating
--sprites in maps by copying
--sprite data around
local src=119+flr((g_tick%20)/4)
sprcpy(71,src)
src=73+flr((g_tick%12)/4)
sprcpy(72,src)
foreach(g_objs, function(t)
if t.update then
t:update(g_objs)
end
end)
end
function collide(o1, o2)
if not o1 or not o2 then
return
end
if o1.off or o2.off then
return
end
if rectintersect(
o1:getrect(), o2:getrect())
then
if is_holding(o1, o2) then
update_holding(o1, o2)
else
update_collision(o1,o2)
end
end
end
function game_start()
g_state = 1 -- play!
g_objs={}
g_uiobjs={}
g_violets={}
g_violets={
make_violet(0)
,
-- make_violet(1)
}
foreach(g_violets, init_phys)
-- the pushable blocks (heart boxes)
g_blocks={
-- make_block(50,35)
}
foreach(g_blocks, init_phys)
for i = 0,15 do
mset(i,0,72)
end
-- not sure why 30 works, have to ask @stevel
for i = 0,15 do
mset(i,30,72)
end
-- make some walls to bounce off of
for x in all({2, 13}) do
for y=8,30 do
mset(x, y, 72)
end
end
if #g_violets > 1 then
g_violets[2].x = 86
g_violets[2].direction=0
end
--init as random
if true then
for i=1,32 do
scrollby(-8)
end
g_scroffset = 128
foreach(g_violets, function(v)
v.y=41
end)
foreach(g_blocks, function(v)
v.y=41
end)
end
g_scroffset = 128
g_timer=99
end
function make_title()
add(g_uiobjs,
make_drawon(d_climbing,
function(t,s)
add(s,
make_drawon(d_violets,
function(t,s)
add(s, make_menu(
{'1 player',
'2 player',
'exit',},
function(t, i)
if i == 2 then
load('git/menu')
run()
return
end
add(s, make_trans(
function()
game_start()
end))
-- game_start()
end
))
--game_start()
end))
end))
end
function update_held(obj)
local held=obj.holding
if obj.will_hold then
if held then
update_holding(obj, held)
if obj.direction == 0 then
held.x=obj.x-0.5*obj.hbx1
else
held.x=obj.x+1.25*obj.hbx1
end
held.y=obj.y+obj.hby0+0.25*obj.hby1
end
else
if held then
held.held_by=nil
obj.holding=nil
end
end
end
function update_holding(obj, held)
held.speed = obj.speed
held.speedy= obj.speedy
held.direction=obj.direction
held.speedinc=obj.speedinc
obj.holding=held
held.held_by=obj
end
function numdigits(n)
local d = 1
while ((n / 10) > 1) do
n/=10
d+=1
end
return d
end
function draw_thing(thing)
thing:draw()
end
function _draw()
cls()
rectfill(0,0,127,127,12)
--test cloud
spr(128,(((g_tick%640)/4+88)%160)-32,
((0-g_scroffset/3)%144)-16,4,2)
spr(128,(((g_tick%800)/5+44)%160)-32,
((44-g_scroffset/3)%144)-16,4,2)
spr(128,(((g_tick%640)/4+0)%160)-32,
((88-g_scroffset/3)%144)-16,4,2)
for i=0,15 do
pal(i,1)
end
camera(-1,-1)
draw_map(128)
camera()
camera(-2,-2)
draw_map(2)
camera()
pal()
draw_map()
--[[
line(9,g_scrline,
127,g_scrline,5)
print(g_scrline,0,g_scrline-2,
5)
--]]
foreach(g_violets, draw_thing)
foreach(g_blocks, draw_thing)
foreach(g_objs, function(t)
if t.draw then
t:draw(g_objs)
end
end)
draw_uiobjs(g_uiobjs)
if g_state == 0 then
return
end
-- height/score display
color(1)
local scrl=128-g_scroffset
rectfill(1,120,
1+4*(8+numdigits(scrl)),126)
if shouldscroll() then
color(11)
else
color(3)
end
print("height: "..flr(scrl),
2,121)
if g_violets[1] then
color(11)
g_violets[1].x += g_violets[1].speed
if next_to_wall(g_violets[1]) then
color(9)
end
g_violets[1].x -= g_violets[1].speed
print("speed: "..table_to_string(g_violets[1].speed), 2, 116)
print("jumps: "..table_to_string(g_violets[1].jumps), 2, 110)
end
color(5)
--time remaining display
color(1)
rectfill(1,1,
13+4*(8+numdigits(g_timer)),7)
local ncolors=#time_colors
local t_c = max(1+ncolors-min(
flr((g_timer+10) / 10),
ncolors),1)
color(time_colors[t_c])
print("remaining: "
..flr(g_timer),2,2)
--[[
pal(0,g_tick%15,1)
pal(9,flr(rnd(16)),1)
--]]
--[[
--debug offset and ground
print(off,0,0,7)
local f,smy,my=g_violets[1]:getflr()
print(f..' '..smy..' '..my,0,8,7)
--]]
end
function draw_map(lyr)
local top=flr(
(g_scroffset%256)/8)
local off=getlocaloff()
if top<16 then
local mh = 16-top
map(0,top,0,off,16,mh,lyr)
if mh<17 then
map(0,16,0,off+mh*8,16,17-mh,lyr)
end
else
local mh = 32-top
map(0,top,0,off,16,mh,lyr)
if mh<17 then
map(0,0,0,off+mh*8,16,17-mh,lyr)
end
end
end
function getlocaloff()
return 7-(g_scroffset%8)-7
end
function getscrmy()
result = {}
local top=flr(
(g_scroffset%256)/8)
if top<16 then
for i=top,15 do
add(result,i)
end
local rm=16-#result
for i=16,16+rm do
add(result,i)
end
else
for i=top,31 do
add(result,i)
end
local rm=16-#result
for i=0,rm do
add(result,i)
end
end
return result
end
function make_block(x,y)
return {
x=x,
y=y,
hbx0=0,
hbx1=8,
hby0=0,
hby1=8,
is_holdable=true,
update=function(b) end,
draw=function(b)
spr(96,b.x,b.y)
end,
}
end
function make_apple(x,y)
local r = {
breaks_blocks=true,
xstop_reverse=true,
x=x,
y=y,
hbx0=0,
hbx1=8,
hby0=0,
hby1=8,
pickup=function(a, o)
g_timer += 15
del(g_objs, a)
end,
update=function(b,s)
if b.y > 127 then
del(s,b)
end
if b.speed > 0 then
b.speed = 1
else
b.speed = -1
end
end,
draw=function(b)
spr(64,b.x,b.y)
end
}
init_phys(r)
return r
end
function init_phys(o)
local phys={
held_by=nil,
direction=1,
speed=0,
speedinc=0.25,
speedy=0,
getrect=function(t)
return {
t.x+t.hbx0,
t.y+t.hby0,
t.x+t.hbx1,
t.y+t.hby1
}
end,
--
getflr=function(t)
local mx = flr((t.x+t.hbx0)/8)
local mx2 =
min(15,max(0,mx+1))
local hit=false
local off=getlocaloff()
local mys=getscrmy()
local myp=
flr((t.y+t.hby1-off)/8)
local my=myp+1
local smy = my
for i=my,17 do
local m=mys[i]
--local s=mget(mx,m)
if band(1,fget(mget(mx,m)))>0 or
band(1,fget(mget(mx2,m)))>0 then
hit=true
break
end
my+=1
end
if not hit then
return 128,0,0
end
local r = (my-1)*8-t.hby1+off
--return (my-1)*8-t.hby1--+off
--return my*8-t.hby1-g_scroffset
return r,smy,my
end
}
for k,v in pairs(phys) do o[k] = v end
return o
end
function make_violet(p)
return {
x=27,
y=64,
frame=0,
hbx0=4,
hbx1=12,
hby0=0,
hby1=16,
holding=nil,
will_hold=false,
is_holdable=false,
breaks_blocks=true,
jumps=2,
---
update=function(t)
if t.off then
t.frame=0
t.xoff = sin(g_tick/100)*10
t.yoff = sin(g_tick/200)*10
if btn(0,p) then
t.x-=1
end
if btn(1,p) then
t.x+=1
end
if t.y > 20 then
t.y-=1
end
if t.y < 88 and btn(5,p)
then
t.off=nil
t.x+=t.xoff
t.y+=t.yoff
t.xoff=nil
t.yoff=nil
add(g_objs,
make_pop(t.x,t.y))
end
return
end
local ground = t:getflr()
local spdadj=1
local frameadj=0.5
--run
if btn(4,p) then
spdadj=2 --was 2
frameadj=1
t.will_hold=true
else
t.will_hold=false
end
--left
if btn(0,p) then
if t.direction == 1 then
t.frame = 0
end
t.direction = 0
t.speed =
max(-2-spdadj,
t.speed-2*t.speedinc)
--right
elseif btn(1,p) then
if t.direction == 0 then
t.frame = 0
end
t.direction = 1
t.speed =
min(2+spdadj,
t.speed+2*t.speedinc)
--stop
else
if abs(t.speed) <
t.speedinc then
t.speed=0
--t.frame=(t.frame+0.5)%3
t.frame = 0
end
end
t.x += t.speed
local flip = false
if next_to_wall(t) then
flip = true
end
t.x -= t.speed
if flip then
t.jumps = max(t.jumps, 1)
end
--jump
if btnn(5,p) and t.jumps > 0 then
t.jumps -= 1
-- if t.y == ground and
-- t.speedy == 0 then
t.speedy = -6 - abs(t.speed)
-- end
if flip then
t.speed *= -1
t.jumps +=1
end
end
if t.y == ground and t.speedy == 0 then
t.jumps = 2
end
t.frame=(t.frame+frameadj)%3
end,
---
draw=function(t)
local sflip =
(t.direction == 1)
local s = 4
--duck
if g_state == 1
and btn(3,p) then
s = 14
end
local ground = t:getflr()
if t.speed ~= 0 then
if ((sflip and t.speed<0) or
(not sflip and
t.speed > 0))
then
s=12
else
s=2*flr(t.frame)+6
end
end
if p==1 then
pal(2,3)
pal(14,11)
pal(8,2)
end
if t.y ~= ground then
s=0
if g_state == 1
and (g_tick%4)>2 then
s = 2
end
end
--debug ground detection
if false then
line(t.x, ground,
t.x+16, ground, 7)
line(t.x,t.y,t.x+16,t.y,8)
if t.gy then
line(t.x,t.gy,t.x+16,
t.gy,11)
end
end
if t.holding then
s+=32
end
local x = t.x
local y = t.y
if t.off then
s=4
if t.xoff then
x+=t.xoff
y+=t.yoff
end
end
spr(s,x,y,2,2,sflip)
pal()
if t.off then
s=132
if g_tick%20>10 then
s=134
end
spr(s,x,y,2,2)
end
end
---
}
end
function next_to_wall(o)
local off=getlocaloff()
local my = flr((o.y-off)/8)+1
local mx = flr((o.x+o.hbx0)/8)
local mx2 =
min(15,max(0,mx+1))
local mys = getscrmy()
--hby1/8
local h=0
if o.hby1 > 8 then
h=1
end
local hit=false
--todo, base on rect
for i=0,h do
if band(shl(1,2),
fget(mget(mx,mys[my+i]))) > 0 then
hit=true
break
end
if band(shl(1,2),
fget(mget(mx2,mys[my+i]))) > 0 then
hit=true
break
end
end
return hit
end
function test_xstop(o)
if (not o.breaks_blocks) return
if next_to_wall(o) then
if o.speed==0 then
if o.direction==0 then
o.x-=1
else
o.x+=1
end
else
--hack for apple vs violet
if o.xstop_reverse then
o.speed*=-1
else
o.x+=o.speed*-1
end
end
end
end
function test_break(o)
if not o.breaks_blocks then
return
end
if o.speedy >= 0 then
return
end
local off=getlocaloff()
local my =
flr((o.y-off-1)/8)+1
local mx = flr((o.x+o.hbx0)/8)
local mx2 =
min(15,max(0,mx+1))
local mys = getscrmy()
local mxs={}
if o.direction==1 then
add(mxs,mx2)
add(mxs,mx)
else
add(mxs,mx)
add(mxs,mx2)
end
for m in all(mxs) do
if band(fget(mget(m,mys[my])),
shl(1,1)) > 0 then
o.speedy=abs(o.speedy)
if band(fget(mget(m,mys[my])),
shl(1,3)) == 0 then
--todo, don't make apple
--if there's already
--a block there
local f=false
if my>1 then
if band(fget(
mget(m,mys[my-1])),
shl(1,1)) > 0 then
f=true
end
end
if f or rnd(100) < 10 then
mset(m,mys[my],0)
make_break(m*8,(my-1)*8+off)
else
--mset(m,mys[my],88)
mset(m,mys[my],0)
make_break(m*8,(my-1)*8+off)
local a = make_apple(
m*8, (my-2)*8+off)
if o.direction == 0 then
a.speed = 1
else
a.speed = -1
end
add(g_objs, a)
end
end
return
end
end
end
function update_phys(o)
if o.off then
return
end
if o.held_by ~= nil then
return
end
local ground=o:getflr()
local drag=o.speedinc
if o.y ~= ground then
drag*=g_airdragmod
end
-- xdrag
if abs(o.speed) >= drag then
if o.direction==0 then
if o.speed < 0 then
o.speed+=drag
else
o.speed-=drag
end
else
if o.speed > 0 then