-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclimbgood.p8
3318 lines (2920 loc) · 96.9 KB
/
climbgood.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__
-- skate up the tower, i dare you
function repr(arg)
-- turn any thing into a string (table, boolean, whatever)
if arg == nil then
return "nil"
end
if type(arg) == "boolean" then
return arg and "true" or "false"
end
if type(arg) == "table" then
if arg[1] then
-- hackity hack hac
local retval = " list[ "
for _, v in pairs(arg) do
retval = retval .. repr(v) .. ","
end
retval = retval .. "] "
return retval
end
local retval = " table{ "
for k, v in pairs(arg) do
retval = retval .. k .. ": ".. repr(v).. ","
end
retval = retval .. "} "
return retval
end
return ""..arg
end
function print_stdout(msg)
-- print 'msg' to the terminal, whatever it might be
printh("["..repr(g_tick).."] "..repr(msg))
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
-- { particle stuff from @casualeffects
-- fast particle system
-- by morgan mcguire @casualeffects
-- http://casual-effects.com
-- released as bsd-license open source february 2017.
function add_particle(x, y, dx, dy, life, color, ddy)
particle_array_length += 1
-- grow if needed
if (#particle_array < particle_array_length) add(particle_array, 0)
-- insert into the next available spot
particle_array[particle_array_length] = {x = x, y = y, dx = dx, dy = dy, life = life or 8, color = color or 6, ddy = ddy or 0.0625}
end
function process_particles()
-- @casualeffects particle system
-- http://casual-effects.com
-- simulate particles during rendering for efficiency
local p = 1
while p <= particle_array_length do
local particle = particle_array[p]
-- the bitwise expression will have the high (negative) bit set
-- if either coordinate is negative or greater than 127, or life < 0
if bor(band(0x8000, particle.life), band(bor(particle.x, particle.y), 0xff80)) != 0 then
-- delete dead particles efficiently. pico8 doesn't support
-- table.setn, so we have to maintain an explicit length variable
particle_array[p], particle_array[particle_array_length] = particle_array[particle_array_length], nil
particle_array_length -= 1
else
-- draw the particle by directly manipulating the
-- correct nibble on the screen
local addr = bor(0x6000, bor(shr(particle.x, 1), shl(band(particle.y, 0xffff), 6)))
local pixel_pair = peek(addr)
if band(particle.x, 1) == 1 then
-- even x; we're writing to the high bits
pixel_pair = bor(band(pixel_pair, 0x0f), shl(particle.color, 4))
else
-- odd x; we're writing to the low bits
pixel_pair = bor(band(pixel_pair, 0xf0), particle.color)
end
poke(addr, pixel_pair)
-- acceleration
particle.dy += particle.ddy
-- advance state
particle.x += particle.dx
particle.y += particle.dy
particle.life -= 1
-- for _, c in pairs(collision_objects) do
-- local collision_result = c:collides(particle)
-- if collision_result != nil then
-- particle.x += collision_result[1]
-- particle.y += collision_result[2]
-- particle.dy = -particle.dy
-- end
-- end
p += 1
end -- if alive
end -- while
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
respawn_in_level()
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)
src=76+(g_tick%4)
sprcpy(m_zipup,src)
src=92+flr((g_tick%16)/4)
sprcpy(m_zipdown,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
-- list of level
c_level_indices = {178, 176, 162, 160}
function game_start(level)
g_state = 1 -- play!
g_objs={}
g_uiobjs={}
particle_array, particle_array_length = {}, 0
-- 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
-- load the level
if level == nil then
level = 1
end
g_current_level = level
board = load_board(
c_level_indices[level])
g_leveltable = board2table(
board)
g_spawn_loc = nil
g_sprobjs = {}
for i = 1, #board.items do
local item = board.items[i]
if item.itemtype ==
it_sprobj then
add(g_sprobjs, item)
end
if item.itemtype == it_spawn_loc then
g_spawn_loc = {
8*item.xpos,
g_scroffset - (
16
+item.yscr*128
+item.ypos*8
)
}
end
end
g_sprobj_index = 1
for y = 0, 31 do
local r = g_leveltable[y+1]
local yy = 31 - y
for x = 0, 15 do
if r != nil then
mset(x, yy, r[x+1])
else
mset(x, yy, 0)
end
end
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 not g_spawn_loc then
cls()
print(
"error: level ["
..g_current_level
.."] does not have a spawn"
.." set."
)
stop()
end
g_violets={
make_violet(
0,
g_spawn_loc[1],
g_spawn_loc[2]
),
}
add(g_objs,
make_pop(g_violets[1].x,
g_violets[1].y))
foreach(g_violets, init_phys)
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
spawn_sprobj()
end
ot_apple = 0
ot_spikes_up = 1
ot_spikes_left = 2
ot_spikes_down = 3
ot_spikes_right = 4
ot_bounceypad_up = 5
ot_bounceypad_up_left = 6
ot_bounceypad_left = 7
ot_bounceypad_down_left = 8
ot_bounceypad_down = 9
ot_bounceypad_down_right = 10
ot_bounceypad_right = 11
ot_bounceypad_right_up = 12
function make_bouncey_ctor(ind)
return function(x, y)
return make_bouncypad(
x,
y-8,
ind - 5
)
end
end
function make_spikes_ctor(ind)
return function(x,y)
return make_spikes(x, y-8, ind-1)
end
end
sprobj_ctors = {
[ot_apple] = function(x,y)
return make_apple(x,y-8)
end,
}
-- add the rotated ctors to the table
for i=1,4 do
sprobj_ctors[i] = make_spikes_ctor(i)
end
for i=5,12 do
sprobj_ctors[i] = make_bouncey_ctor(i)
end
function spawn_sprobj()
local ypos =
flr((128-g_scroffset)/8)+17
for i = g_sprobj_index,
#g_sprobjs do
local objypos =
g_sprobjs[i].yscr*16 +
g_sprobjs[i].ypos
if objypos <= ypos then
local item = g_sprobjs[i]
local ctor = sprobj_ctors[
item.objtype]
if ctor then
--make the object
local obj = ctor(
item.xpos*8,
g_scroffset -
(item.yscr*128+
item.ypos*8))
--find the pos
add(g_objs, obj)
--add(g_objs, make_leveltrans(
--g_current_level + 1))
end
g_sprobj_index += 1
else
break
end
end
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)
if thing.draw then
thing:draw()
end
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, draw_thing)
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)
if g_violets[1]:next_to_wall() then
color(9)
if g_violets[1].wall_hang_timer and g_violets[1].wall_hang_timer > 0 then
color(12)
end
end
local mp = map_position(g_violets[1])
cursor(2,122)
color(1)
print("speed: "..g_violets[1].speed)
-- print(
-- "fields: "
-- .. repr(map_fields_list2(g_violets[1])),
-- 2,
-- 122,
-- 1
-- )
end
color(5)
--[[
--debug offset and ground
print(off,0,0,7)
local f,smy,my=g_violets[1]:getflr()
print(f..' '..smy..' '..my,0,8,7)
--]]
process_particles()
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_spikes(x, y, ind)
local r = {
x=x,
y=y,
hbx0=0,
hbx1=8,
hby0=0,
hby1=8,
off = true,
spr_offset=ind,
pickup=function(a, o)
respawn_in_level()
end,
draw=function(b)
spr(98+b.spr_offset,b.x,b.y)
end
}
init_phys(r)
return r
end
g_bouncey_directions = {
{0, -1},
{-1,-1},
{-1, 0},
{-1, 1},
{0, 1},
{1, 1},
{1, 0},
{1, -1},
}
function make_bouncypad(x, y, ind)
local r = {
x=x,
y=y,
hbx0=0,
hbx1=8,
hby0=0,
hby1=8,
off = true,
spr_offset=ind,
pickup=function(a, o)
-- todo: check the normal direction before flipping the vector
local result = {0, 0}
-- local spd = min(2*abs(veclen(o.speed, o.speedy)), 9)
local spd = 8
local table = g_bouncey_directions[a.spr_offset+1]
if table == nil then
cls()
print(a.spr_offset)
stop()
end
for i=1,2 do
local val = table[i]
result[i] = val * spd
end
o.speed = result[1]
o.speedy = result[2]
cls()
print(o.speedy)
print(spd)
print(repr(table))
print(a.spr_offset)
-- stop()
end,
draw=function(b)
spr(198+b.spr_offset,b.x,b.y)
end
}
init_phys(r)
return r
end
g_rot_cw = true
g_rot_c_cw = false
function pix_coords_from_spr_ind(ind)
-- def func(index, width, result_mult):
-- return ((index % width) * result_mult, math.floor(index / width) * result_mult)
return {
( ind % 16) * 8,
flr(ind/16) * 8
}
end
function rotate_sprite_90(from_spr, to_spr, shifts, clockwise)
local spr_width = 2
local centering = 8* spr_width / 2
local from = pix_coords_from_spr_ind(from_spr)
local from_x_base = from[1]
local from_y_base = from[2]
local to = pix_coords_from_spr_ind(to_spr)
local to_x_base = to[1]
local to_y_base = to[2]
local cos_theta = cos(shifts/ 30)
local sin_theta = sin(shifts/ 30)
-- todo: fix th
-- cls()
-- print("from: "..repr({from_x_base, from_y_base}))
-- print("to: "..repr({to_x_base, to_y_base}))
-- stop()
for x=0,8*spr_width - 1 do
for y=0,8*spr_width - 1 do
local from_x = x - centering
local from_y = y - centering
local to_x = from_x * cos_theta - from_y * sin_theta
local to_y = from_y * cos_theta + from_x * sin_theta
to_x += centering
to_y += centering
local c = abs(xp) < 7 and abs(yp) < 7 and sget(from_x_base + x, from_y_base + y) or 0
-- print("from: "..repr({x, y}))
-- print("from: "..repr({from_x, from_y}))
-- print("to: "..repr({to_x, to_y}))
-- print("to: "..repr({to_x_base + to_x + centering, to_y + to_y_base + centering})
-- )
-- local col = sget(from_x_base + x, from_y_base + y)
sset(to_x_base + to_x, to_y_base + to_y, c)
end
end
-- stop()
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,
off=true,
pickup=function(a, o)
o.jumps += 1
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