forked from stevelavietes/pico8carts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
blobkey.p8
1705 lines (1471 loc) · 57.1 KB
/
blobkey.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 5
__lua__
-- blobs of steel
function _init()
stdinit()
add(g_objs, make_bg(6))
add(g_objs, make_title())
add(g_objs, make_menu(
{
'start game'
},
function(t,i,s)
add(s, make_trans(
function()
game_start()
end
))
end
))
--[[ add test object
local f = 1
for i=1,6 do
local o = make_physobj(10*(i-1),10,3*i)
add(g_objs, o)
add(g_pobjs, o)
o.c = 6 + i
add_force(o,{f,0})
f*=-1
end
--]]
end
function game_start()
g_objs = {}
-- global board
g_brd = make_board()
add(g_objs, g_brd)
-- board "state"
g_brd.st = 0
g_points = {0,0}
add(g_objs, make_clock(2,0,-1))
add(g_objs, make_vsscore())
g_brd:faceoff()
end
function make_vsscore()
return {
x=0,
y=0,
draw=function()
for i=0,1 do
pushc(-111*i,0)
rectfill(1,1,15,7,6)
spr(72+i,1,1)
local v = g_points[i+1]
local pad=' '
if (v>9) pad=''
print(pad..v,8,2,0)
popc()
end
end
}
end
function make_timer(e,f,d)
return {
--e=e,f=f, --closure
d=d, --data for callback
s=g_tick,
update=function(t,s)
if elapsed(t.s) > e then
del(s,t)
f(t,s)
end
end
}
end
function make_clock(
t_start_m,-- time to start (s)
t_start_s,
t_inc -- time increment
)
return {
x=54,
y=2,
c=0,
b=g_brd,
m=t_start_m,
s=t_start_s,
inc=t_inc,
draw=function(t)
rectfill(-1,-1,19,5,6)
local mp,sp = '',''
if (t.m<10) then
mp=0
end
if (t.s<10) then
sp=0
end
print(mp..t.m..':'..sp..t.s,
0,0,0)
end,
update=function(t)
if (t.b.st~=0) then
return
end
t.c+=1
--fixed-point math not
--accurate enough for
--division of seconds.
--do addition instead
if t.c>=30 then
t.c=0
t.s+=t.inc
if t.inc > 0 and t.s>=60 then
t.s=0
t.m+=t.inc
elseif t.inc < 0 and t.s <= 0 then
t.s=59
t.m+=t.inc
end
end
end
}
end
function reset_ball(ball)
ball.x=124
ball.y=64
end
function make_ball()
return make_physobj({
x=124,
y=64,
spd=2,
is_ball=true,
update=function(t)
--[[
if g_brd.st ~= 0 then
return
end
-- left
if btn(0) then
t.x-=t.spd
end
-- right
if btn(1) then
t.x+=t.spd
end
-- up
if btn(2) then
t.y-=t.spd
end
-- down
if btn(3) then
t.y+=t.spd
end
--]]
end,
draw=function(t)
circfill(-1,1,6,6)
circfill(0,0,6,2)
end
},5)
end
function make_board()
local ball = make_ball()
local player = make_player(0)
player.eyetrack=ball
local player2 = make_player(1)
player2.eyetrack=ball
--inertblob = make_inertblob()
ball.force = {x=0,y=0}
local goal_l = make_goal(false,ball)
local goal_r = make_goal(true,ball)
local result = {
x=0,
y=0,
bobjs={ball,goal_l,goal_r,player,player2},
ball=ball,
update=function(t)
updateobjs(t.bobjs)
foreachp(t.bobjs, update_phys)
foreachp(t.bobjs, update_collision)
t.x = -max(min(t.ball.x-64,124),0)
end,
draw=function(t)
rectfill(0,0,255,255,5)
-- center stuff
circ(124,64,20,11)
line(124,0,124,127,11)
drawobjs(t.bobjs)
end,
faceoff=function(t)
reset_ball(t.ball)
player:reset()
player2:reset()
g_brd.st=0
end
}
for i = 1, 5 do
add(result.bobjs, make_inertblob())
end
return result
end
function make_goal(should_flip,ball)
local x=20
if should_flip then
x=228
end
return {
x=x,
y=42,
ball=ball,
flipped=should_flip,
is_hit=false,
first_hit=false,
update=function(t)
--[[
coll_wall = coll_rect(
t.x,
t.y+1,
8,
8*5-1,
t.ball
)
if coll_wall < 0 then
return
end
--]]
t.is_hit = false
if crc_rect_isect(ball, t.x, t.y+1,8,8*5-1) then
t.is_hit=true
if t.first_hit == false then
g_brd.st = 1 -- not play
player = 2
if should_flip then
player = 1
end
g_points[player]+=1
add(
g_objs,
make_menu(
{"goaaaallll"},
function(t_m,i,s)
del(s,t_m)
g_brd:faceoff()
t.first_hit = false
end
)
)
t.first_hit = true
end
end
end,
draw=function(t)
-- color 0 opaque
palt(0,false)
palt(1,true)
spr(10,0,0,1,1,t.flipped)
for i=1,3 do
spr(26,0,8*i,1,1,t.flipped)
end
spr(11,0,32,1,1,t.flipped)
palt()
if t.is_hit then
circfill(0,0,20,4)
end
end
}
end
function crc_rect_isect(
ball,
xmin,
ymin,
xsize,
ysize
)
local sf=0.1
local center = {x=ball.x*sf, y=ball.y*sf}
local box = {
{xmin*sf+xsize*sf/2, xsize*sf/2},
{ymin*sf+ysize*sf/2, ysize*sf/2}
}
local radius = ball.radius*sf
local c_dist = {}
local co_dist_sq=0
c_dist.x = abs(center.x-box[1][1])
c_dist.y = abs(center.y-box[2][1])
if c_dist.x > box[1][2]+radius then
return false
elseif c_dist.y > box[2][2]+radius then
return false
end
if c_dist.x <= box[1][2] then
return true
end
if c_dist.y <= box[2][2] then
return true
end
co_dist_sq+=(c_dist.x-box[1][2])^2
co_dist_sq+=(c_dist.y-box[2][2])^2
return co_dist_sq <= radius^2
end
-- collide with a rectangle
-- -1 is miss, lrud,0123
function make_title()
return {
x=30,
y=20,
update=function(t)
end,
draw=function(t)
rectfill(-1,-1,71,21,11)
rectfill(0,0,70,20,1)
spr(1,12,2,6,2)
end
}
end
function make_bg(col)
return {
x=0,
y=0,
col=col,
update=function(t) end,
draw=function(t)
rectfill(0,0,127,127,t.col)
end
}
end
-- creates a physics object out
-- of the parent object with a
-- given mass
function make_physobj(p,mass)
phys = {
p=p,
mass=mass,
force={x=0,y=0},
velocity={x=0,y=0},
radius=5,
c=6,
is_phys=true,
is_static=false,
collides=function(o, pos)
local r_2 = o.radius*o.radius
local x_d = pos.x - o.x
local y_d = pos.y - o.y
return (x_d*x_d)+(y_d*y_d)<r_2
end
}
for k,v in pairs(phys) do
p[k] = v
end
return p
end
function add_force(o, f)
o.force.x += f.x
o.force.y += f.y
end
function compute_force_1d(pos, f, m, v, dt)
local a=0
if f ~= 0 then
a = f/m
end
-- update position half way
pos += 0.5 * dt * v
-- update velocity (drag)
v += dt * a
-- update position other half
pos += 0.5 * dt * v
return pos, v
end
g_friction=0.01
function update_phys(o)
-- in case we want to play
-- with time, even though pico
-- gives us a constant clock
local dt = 1
o.x, o.velocity.x=compute_force_1d(
o.x,
o.force.x,
o.mass,
o.velocity.x,
dt
)
o.y, o.velocity.y=compute_force_1d(
o.y,
o.force.y,
o.mass,
o.velocity.y,
dt
)
-- zero out the force
o.force = {x=0,y=0}
-- drag
o.force.x -= g_friction*o.velocity.x
o.force.y -= g_friction*o.velocity.y
end
function collide_walls_1d(b,p,v,r)
local p_orig = p
p = max(p-r,b[1]+r)
p = min(p+r,b[2]-r)
-- the position changed
if p ~= p_orig then
v = -v
end
return p,v
end
g_edges = {{-4,250},{-5,124}}
function update_collision(o)
-- (checking o, pos is new pos
-- check boundaries first
local pos = {x=o.x,y=o.y}
local r = o.radius
pos.x, o.velocity.x = collide_walls_1d(
g_edges[1],pos.x,o.velocity.x,o.radius)
pos.y, o.velocity.y = collide_walls_1d(
g_edges[2],pos.y,o.velocity.y,o.radius)
for i, t in pairs(g_pobjs) do
if t ~= o then
if t:collides(pos) then
if not t.is_static then
local o_v = o.velocity
local t_v = t.velocity
local o_m = o.mass
local t_m = t.mass
local t_pos = {x=t.x,y=t.y}
-- push the objects back
local r = o.radius + t.radius
local v = vecsub(pos,t_pos)
local ratio = r/sqrt(vecdistsq(v))
local d = vecscale(v, ratio*0.51)
local new_pos = vecadd(d,t_pos)
-- result
pos = new_pos
-- a.v = (a.u * (a.m - b.m) + (2 * b.m * b.u)) / (a.m + b.m)
-- b.v = (b.u * (b.m - a.m) + (2 * a.m * a.u)) / (a.m + b.m)
local o_v = o.velocity
local t_v = t.velocity
local o_m = o.mass
local t_m = t.mass
o.velocity = vecscale(vecadd(vecscale(o_v, (o_m-t_m)),(vecscale(t_v,2*t_m))),(1/(o_m+t_m)))
t.velocity = vecscale(vecadd(vecscale(t_v, (t_m-o_m)),(vecscale(o_v,2*o_m))),(1/(o_m+t_m)))
end
end
end
end
o.x = pos.x
o.y = pos.y
end
function _update()
stdupdate()
end
function _draw()
stddraw()
end
function make_inertblob()
local p = make_player()
p.blobs = {}
local x = rnd(128) + 64
local y = rnd(128)
local v = vecrot({x=0,y=5},
rnd(360))
for i = 1, flr(rnd(4))+1 do
x += v.x
y += v.y
add(p.blobs, {x=x,y=y,r=6})
v = vecrot(v, rnd(180))
end
return p
end
function make_player(playnum)
local startx = 84
if playnum ~= 1 then
startx = 176
end
result = {
x = 0,
y = 0,
p = playnum,
startx=startx,
blobs = {
{x=128, y=32, r=8},
},
vel = {x=0,y=0},
reset=function(t)
local wrap =
shrinkwrap(t.blobs,
#t.blobs*10, eyed, t.p)
local disp = vecsub(
{x=t.startx,y=60},
wrap.center
)
for i,b in pairs(t.blobs) do
t.blobs[i] = vecadd(b,disp)
t.blobs[i].r = b.r
end
t.vel={x=0,y=0}
end,
update=function(t,s)
if t.p then
for i=1, #s do
local t2 = s[i]
if t ~= t2
and t2
and t2.blobs
and not t2.p
and (not t2.lastsplit or
elapsed(t2.lastsplit) > 30)
then
local hit = false
for j=1,#t.blobs do
local b1 = t.blobs[j]
for k=1,#t2.blobs do
local b2 = t2.blobs[k]
if circcircsect(
b1, b1.r, b2, b2.r)
then
hit = true
break
end
end
if hit then
break
end
end
if hit then
del(s, t2)
for j=1,#t2.blobs do
add(t.blobs,t2.blobs[j])
end
t.vel = {x=0,y=0}
end
end
end
end
-- test ejection
if t.p and (t.vel.x ~= 0 or
t.vel.y ~= 0)
and btn(4, t.p)
and #t.blobs > 1
then
local blob = t.blobs[#t.blobs]
t.blobs[#t.blobs] = nil
local n = make_player(t.p)
n.vel = vecscale(t.vel, 3)
n.blobs = {blob,}
blob.x += n.vel.x
blob.y += n.vel.y
t.p = nil
n.lastsplit = g_tick
add(s, n)
end
-- todo: base drag from size
t.vel = vecscale(t.vel, 0.95)
local adjang = nil
if t.p then
if btn(0, t.p) then
adjang = 270
if btn(2, t.p) then
adjang -= 45
elseif btn(3, t.p) then
adjang += 45
end
elseif btn(1, t.p) then
adjang = 90
if btn(2, t.p) then
adjang += 45
elseif btn(3, t.p) then
adjang -= 45
end
elseif btn(2, t.p) then
adjang = 180
elseif btn(3, t.p) then
adjang = 0
end
end
if t.p and adjang then
t.vel = vecadd(t.vel,
vecrot({x=0,y=0.25}, adjang))
end
foreach(t.blobs, function(b)
b.x += t.vel.x
b.y += t.vel.y
end)
end,
draw=function(t,s)
for i = 1,#t.blobs do
local pt = t.blobs[i]
local col = 13
if t.p == 0 then
col = 14
elseif t.p == 1 then
col = 12
end
circfill(pt.x, pt.y, pt.r,
col)
end
--[[
if not t.p then
return
end
--]]
-- eyes
local eyed = t.vel
if eyed.x == 0
and eyed.y == 0 then
-- todo, find a center
eyed = nil
end
local wrap =
shrinkwrap(t.blobs,
#t.blobs*10, eyed, t.p)
local hits = wrap.pts
for i=1,#hits-1 do
local p1 = hits[i]
local p2 = hits[i+1]
line(p1.x,p1.y,p2.x,p2.y,7)
end
local p1 = hits[#hits]
local p2 = hits[1]
line(p1.x,p1.y,p2.x,p2.y,7)
if not t.p then
return
end
local eyept = wrap.center
if wrap.hitvector then
local iv = vecscale(vecnorm(
vecsub(wrap.hitvector,
wrap.center)), 8)
eyept = vecsub(
wrap.hitvector, iv)
end
local eyespr = 32
if t.eyetrack then
local lookdir = vecsub(
eyept, t.eyetrack)
local lookang = atan2(
lookdir.x, lookdir.y)
if lookang >= 0.75 then
elseif lookang >= 0.5 then
eyespr = 33
elseif lookang >= 0.25 then
eyespr = 49
else
eyespr = 48
end
end
spr(eyespr,
eyept.x-4, eyept.y-4)
end,
}
return result
end
--[[
function _update()
g_tick = max(0,g_tick+1)
local l = l2
if btn(4) then
l = l1
end
if btn(5) then
l = ctr
end
if btn(0) then
l.x = l.x - 1
end
if btn(1) then
l.x = l.x + 1
end
if btn(2) then
l.y = l.y - 1
end
if btn(3) then
l.y = l.y + 1
end
end
function _draw()
cls()
color(7)
line(l1.x, l1.y, l2.x, l2.y)
local h =
circlinesect(l1, l2, ctr, r)
if h then
circfill(h.x, h.y, 3, 14)
local n =
vecscale(vecnorm(h.n), 10)
line(h.x,h.y, h.x+n.x, h.y+n.y,
2)
color(8)
end
circ(ctr.x, ctr.y, r)
circfill(l1.x, l1.y, 2, 7)
local pts = {
{x=96,y=54},
{x=80,y=70},
{x=70,y=56},
l2,
}
foreach(pts, function(pt)
circfill(pt.x, pt.y, 16,14)
end)
local wrap =
shrinkwrap(pts, 36,
vecsub(l1,l2))
local hits = wrap.pts
for i=1,#hits-1 do
local p1 = hits[i]
local p2 = hits[i+1]
line(p1.x,p1.y,p2.x,p2.y,7)
end
local p1 = hits[#hits]
local p2 = hits[1]
line(p1.x,p1.y,p2.x,p2.y,7)
if wrap.hitvector then
local iv = vecscale(vecnorm(
vecsub(wrap.hitvector,
wrap.center)), 9)
local lookdir = vecsub(
wrap.hitvector, l1)
local lookang =
atan2(lookdir.x, lookdir.y)
eyespr = 0
if lookang >= 0.75 then
elseif lookang >= 0.5 then
eyespr = 1
elseif lookang >= 0.25 then
eyespr = 17
else
eyespr = 16
end
if g_tick % 72 < 4 then
eyespr = eyespr + 2
end
spr(eyespr, wrap.hitvector.x-4-iv.x,
wrap.hitvector.y-4-iv.y)
end
print (stat(0))
end
--]]
------------------------------
function stdinit()
g_tick=0 --time
g_ct=0 --controllers
g_ctl=0 --last controllers
g_cs = {} --camera stack
g_objs = {} --objects
g_pobjs= {} --physics objects
end
function add_gravity(t)
add_force(t, {0,1})
end
function foreachp(lst, fnc)
foreach(lst, function(t)
if t.is_phys then
fnc(t)
end
end)
end
function stdupdate()
g_tick = max(0,g_tick+1)
-- current/last controller
g_ctl = g_ct
g_ct = btn()
updateobjs(g_objs)
end
function updateobjs(objs)
foreach(objs, function(t)
if t.update then
t:update(objs)
end
end)
end
g_sigfigs=3
function fmt(n)
n*=10^g_sigfigs
n = flr(n)
n/=10^g_sigfigs
return n
end
g_maxv = 0
function stddraw()
cls()
drawobjs(g_objs)
--[[
g_maxv = max(
abs(
g_pobjs[1].velocity[2]
),
g_maxv
)
color(13)
foreach(
g_pobjs,
function(o)
print(
fmt(o.velocity[1])..
", "..o.force[1]..
--{", ".. fmt(o.velocity[2])..
--", ".. fmt(o.force[1])..
--", ".. fmt(o.force[2])..
", maxv: "..g_maxv..
""
)
end
)
--]]
--[[
local bd = g_objs[1]
local ball = bd.ball
if ball then
print(ball.x .. ", " .. ball.y)
print(bd.x .. ", " .. bd.y)
end
--]]
end
function drawobjs(objs)
foreach(objs, function(t)
if t.draw then
pushc(-t.x,-t.y)
t:draw(objs)
popc()
end
end)
end
--returns state,changed
function btns(i,p)
i=shl(1,i)
if p==1 then
i=shl(i,8)
end
local c,cng =
band(i,g_ct),
band(i,g_ctl)
return c>0,c~=cng
end
--returns new press only
function btnn(i,p)
if p==-1 then --either
return btnn(i,0) or btnn(i,1)
end
local pr,chg=btns(i,p)
return pr and chg
end