-
Notifications
You must be signed in to change notification settings - Fork 0
/
picoroids.p8
791 lines (685 loc) · 13.1 KB
/
picoroids.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
pico-8 cartridge // http://www.pico-8.com
version 16
__lua__
-- picoroids: an asteroids
-- clone for pico-8.
-- by jason pepas.
-- https://github.com/pepaslabs/picoroids
-- mit licensed.
--== constants ==--
-- note: constants are prefixed
-- with a "k".
-- button constants
kleft = 0
kright = 1
kup = 2
kdown = 3
kbut1 = 4
kbut2 = 5
-- a "shape" is a list of
-- points. the first point is
-- the center of rotation, and
-- the rest of the points form
-- a border between which lines
-- are drawn.
-- the ship shape
kship_shp = {
{x=0,y=0},
{x=5,y=0},
{x=-3,y=-3},
{x=-3,y=3}
}
-- the thruster force magnitude
kthrst_mag = 0.035
-- the magnitude of rotation
krot_mag = 0.02
-- the speed of bullets.
kshot_spd = 2.5
-- number of frames a bullet
-- stays on-screen.
kshot_ttl = 30
--== globals ==--
-- note: globals are prefixed
-- with a "g".
-- a ship is a position, a
-- velocity vector, a rotation,
-- a set of shots, and an alive
-- status.
gship = nil
-- the fire button flip-flop.
gfire_ff = false
-- the teleport flip-flop.
gtele_ff = false
-- the "wasted" timer.
-- 90..1: dead
-- 0: respawn
-- -1: alive
gdead_ttl = -1
-- the "win" timer.
-- 150..1: you won
-- 0: respawn
-- -1: playing
gwinner_ttl = -1
-- a "shot" is a position, a
-- velocity vector, and a ttl.
-- the bullets which are
-- currently in-flight.
gshots = {}
-- an "asteroid" is a position,
-- a velocity vector, and a
-- size class.
groids = {}
-- the current round of play.
glevel = 1
-- a message to display to the user.
gmsg = nil
gmsg_x = nil
gmsg_y = nil
gmsg_color = nil
--== math functions ==--
-- return the square of x.
function sq(x)
return x * x
end
--== geometry functions ==--
-- return the distance between
-- two points.
function dist(p1, p2)
-- https://en.wikipedia.org/wiki/Euclidean_distance#Two_dimensions
return sqrt(
sq(p2.x-p1.x) + sq(p2.y-p1.y)
)
end
--== vector functions ==--
-- add two vectors together.
function vadd(v1, v2)
local v3 = {}
v3.x = v1.x + v2.x
v3.y = v1.y + v2.y
return v3
end
--== point functions ==--
-- return a copy of a point.
function copy_pnt(p)
local p2 = {}
p2.x = p.x
p2.y = p.y
return p2
end
-- return a negated copy of a
-- point.
function npnt(p)
local p2 = {}
p2.x = -p.x
p2.y = -p.y
return p2
end
-- return a copy of a point
-- which has been translated
-- by a vector.
function trans_pnt(p, v)
return vadd(p, v)
end
-- translate a point in the
-- other direction.
function ntrans_pnt(p, v)
return trans_pnt(p, npnt(v))
end
-- return a copy of a point
-- rotated about the origin.
-- p: point to rotate
-- t: number of turns
function rot_pnt(p, t)
-- https://en.wikipedia.org/wiki/Rotation_matrix#In_two_dimensions
return {
x = p.x*cos(t) + p.y*sin(t),
y = p.x*sin(t) - p.y*cos(t)
}
end
-- return a copy of a point
-- rotated about another point.
-- p: point to rotate
-- c: center point of rotation
-- t: number of turns
function rot_pntc(p, c, t)
-- thanks to https://stackoverflow.com/q/2259476/558735
-- 1. translate point to origin
-- 2. rotate point
-- 3. translate the point back
local p = ntrans_pnt(p, c)
p = rot_pnt(p, t)
p = trans_pnt(p, c)
return p
end
-- mod a point by 128 (a.k.a.
-- wrap around the edge of the
-- screen).
function mod_pnt(p)
local p2 = {}
p2.x = p.x % 128
p2.y = p.y % 128
return p2
end
-- print a point.
function print_pnt(p)
print("point:")
print(" x: "..p.x..", y: "..p.y)
end
--== shape functions ==--
-- return a copy of the points
-- of a shape.
function shp_points(s)
local ps = {}
for i=2, count(s) do
add(ps, copy_pnt(s[i]))
end
return ps
end
-- return a copy of a shape
-- which has been translated
-- by a vector.
function trans_shp(s, v)
local ts = {}
for n=1, count(s) do
add(ts, trans_pnt(s[n], v))
end
return ts
end
-- translate a shape in the
-- other direction.
function ntrans_shp(s, v)
local ts = {}
for n=1, count(s) do
add(ts, ntrans_pnt(s[n],v))
end
return ts
end
-- return a copy of a shape
-- which has been rotated by
-- some number of turns.
function rot_shp(s, t)
local c = s[1]
local s2 = {}
for p in all(s) do
add(s2, rot_pntc(p, c, t))
end
return s2
end
-- draw a shape.
function draw_shp(s)
local pts = shp_points(s)
for n=1, count(pts) do
local a = n
local b
if n == count(pts)
then b = 1
else b = n+1
end
local pa = pts[a]
local pb = pts[b]
line(pa.x,pa.y,pb.x,pb.y,7)
end
end
-- print a shape.
function print_shp(s)
print("shape:")
print(" center of rotation:")
print(" x: "..s[1].x..", y: "..s[1].y)
local pts = shp_points(s)
for n=1, count(pts) do
local a=n
local b
if n == count(pts)
then b=1
else b=n+1
end
print(" line:")
print(" x: "..pts[a].x..", y: "..pts[a].y)
print(" x: "..pts[b].x..", y: "..pts[b].y)
end
end
--== ship functions ==--
-- return a copy of a ship.
function copy_ship(s)
local s2 = {}
s2.pos = s.pos
s2.vvec = s.vvec
s2.rot = s.rot
s2.alive = s.alive
return s2
end
-- return a moved copy of a
-- ship by applying the
-- velocity vector.
function moved_ship(s)
local s2 = copy_ship(s)
s2.pos = mod_pnt(
vadd(s2.pos, s2.vvec)
)
return s2
end
-- return a copy of the ship
-- shape in its current
-- location.
function ship_shape(s)
return rot_shp(
trans_shp(kship_shp, s.pos),
s.rot
)
end
-- draw a ship.
function draw_ship(s)
draw_shp(ship_shape(s))
end
-- return a copy of a ship
-- after firing its rear
-- thruster for one frame.
function fire_rthruster(s)
local s2 = copy_ship(s)
local thrst_vec = rot_pnt(
{x=kthrst_mag, y=0},
s.rot
)
s2.vvec = vadd(
s.vvec, thrst_vec
)
return s2
end
-- return a copy of a ship
-- after firing its forward
-- thruster for one frame.
function fire_fthruster(s)
local s2 = copy_ship(s)
local nthrst_vec = rot_pnt(
{x=-kthrst_mag, y=0},
s.rot
)
s2.vvec = vadd(
s.vvec, nthrst_vec
)
return s2
end
-- return a teleported copy of
-- a ship.
function teleport(s)
s2 = copy_ship(s)
s2.pos = {
-- at least 1/2 screen away
x = s.pos.x + 64 + rnd(63),
y = s.pos.y + 64 + rnd(63)
}
return s2
end
--== shot functions ==--
-- return a copy of a shot.
function copy_shot(s)
local s2 = {}
s2.pos = s.pos
s2.vvec = s.vvec
s2.ttl = s.ttl
return s2
end
-- return a copy of the shots.
function copy_shots(ss)
local ss2 = {}
for s in all(ss) do
add(ss2, copy_shot(s))
end
return ss2
end
-- draw a shot.
function draw_shot(s)
pset(s.pos.x, s.pos.y, 7)
end
-- draw the shots.
function draw_shots(shots)
for s in all(shots) do
draw_shot(s)
end
end
-- return a copy of a shot
-- which has been moved.
function moved_shot(s)
local s2 = copy_shot(s)
s2.pos = mod_pnt(
vadd(s.pos, s.vvec)
)
s2.ttl -= 1
return s2
end
-- return a moved copy of
-- the shots.
function moved_shots(shots)
local shots2 = {}
for s in all(shots) do
add(shots2, moved_shot(s))
end
return shots2
end
-- return a copy of the shots
-- with the expired shots
-- removed.
function rm_expired_shots(shots)
local shots2 = {}
for s in all(shots) do
if s.ttl > 0 then
add(shots2, s)
end
end
return shots2
end
-- return a shot fired from a
-- ship.
function spawn_shot(ship)
local shot = {}
-- the current bullet position
shot.pos = copy_pnt(ship.pos)
-- the bullet velocity vector
shot.vvec = vadd(
ship.vvec,
rot_pnt({x=kshot_spd, y=0}, ship.rot)
)
shot.ttl = kshot_ttl
return shot
end
--== asteroid functions ==--
-- return a copy of an asteroid.
function copy_roid(r)
local r2 = {}
r2.pos = r.pos
r2.vvec = r.vvec
r2.size = r.size
return r2
end
-- returns a copy of the
-- asteroids.
function copy_roids(rs)
local rs2 = {}
for r in all(rs) do
add(rs2, copy_roid(r))
end
return rs2
end
-- return a moved copy of an
-- asteroid.
function moved_roid(r)
local r2 = copy_roid(r)
r2.pos = mod_pnt(
vadd(r.pos, r.vvec)
)
return r2
end
-- return a moved copy of all
-- the asteroids.
function moved_roids(roids)
local roids2 = {}
for r in all(roids) do
add(roids2, moved_roid(r))
end
return roids2
end
-- return the radius of an
-- asteroid.
function roid_rad(r)
return r.size * 3
end
-- draw an asteroid.
function draw_roid(r)
local rad = roid_rad(r)
circfill(r.pos.x,r.pos.y,rad,7)
end
-- draw the asteroids.
function draw_roids(roids)
for r in all(roids) do
draw_roid(r)
end
end
--== collision detection ==--
-- return a copy of shots and
-- roids after colliding them.
function collided_shots(shots,roids,lvl)
local ss2 = copy_shots(shots)
local rs2 = copy_roids(roids)
local newrs = {}
for s in all(ss2) do
for r in all(rs2) do
local rad = roid_rad(r)
local d = dist(s.pos, r.pos)
if d <= rad then
del(ss2, s)
del(rs2, r)
-- break up the roid into
-- two smaller pieces.
if r.size > 1 then
newr = spawn_roid(r.size-1,lvl)
newr.pos = r.pos
add(newrs,newr)
newr = spawn_roid(r.size-1,lvl)
newr.pos = r.pos
add(newrs, newr)
end
end
end
end
for r in all(newrs) do
add(rs2,r)
end
return ss2, rs2
end
-- return a copy of the ship
-- after colliding it with the
-- asteroids.
function collided_ship(ship,roids)
local s2 = copy_ship(ship)
local shape = ship_shape(s2)
for r in all(roids) do
local rad = roid_rad(r)
for p in all(shp_points(shape)) do
local d = dist(r.pos, p)
if d < rad then
s2.alive = false
return s2
end
end
end
return s2
end
--== user input functions ==--
-- return an updated ship after
-- reading and processing the
-- arrow keys.
function process_dpad(ship)
local s2 = copy_ship(ship)
if btn(kleft) then
s2.rot += krot_mag
end
if btn(kright) then
s2.rot -= krot_mag
end
if btn(kup) then
s2 = fire_rthruster(s2)
end
if btn(kdown) then
s2 = fire_fthruster(s2)
end
return s2
end
-- return an updated ship after
-- reading and processing the
-- buttons.
function process_btns(ship,shots)
local shp2 = copy_ship(ship)
local shts2 = copy_shots(shots)
if btn(kbut1) then
if not gfire_ff then
gfire_ff = true
add(
shts2,
spawn_shot(shp2)
)
end
else
gfire_ff = false
end
if btn(kbut2) then
if not gtele_ff then
gtele_ff = true
shp2 = teleport(shp2)
end
else
gtele_ff = false
end
return shp2, shts2
end
--== spawning functions ==--
-- create and return a ship.
function spawn_ship()
local s = {}
s.pos = {x=63, y=63}
s.vvec = {x=0, y=0}
s.rot = 0.25
s.alive = true
return s
end
-- create a new asteroid of a
-- given size class (1-3) and a
-- given level.
function spawn_roid(size, level)
local r = {}
r.pos = mod_pnt({
-- try to avoid the center
x = 96 + rnd(63),
y = 96 + rnd(63)
})
r.vvec = {x=0,y=0}
-- the asteroid velocity is
-- a fixed component based on
-- size...
r.vvec.x = 0.3/size
-- ...plus a random component
-- based on size...
r.vvec.x += rnd(size) * 0.1/size
-- ...plus a random component
-- based on level (up to 20%
-- per level).
r.vvec.x *= (1 + rnd(20*level)/100.0)
-- pointed in random direction.
r.vvec = rot_pnt(
r.vvec,
rnd(360)/360.0
)
r.size = size
return r
end
-- respawn at level.
function respawn(level, ship)
if ship == nil or ship.alive == false then
ship = spawn_ship()
end
roids = {}
for i=1, level do
add(roids, spawn_roid(3, level))
add(roids, spawn_roid(2, level))
add(roids, spawn_roid(1, level))
end
return ship, roids
end
-- handle the dead case (msg and respawn).
function process_death(ship, dead_ttl, winner_ttl, level, roids)
local respawn = false
if not ship.alive
then
if dead_ttl < 0 then
dead_ttl = 90
level = 1
else
if dead_ttl > 0
then dead_ttl -= 1
else
if dead_ttl == 0 then
dead_ttl = -1
respawn = true
end
end
end
else
if #roids == 0 then
if winner_ttl < 0 then
winner_ttl = 150
level += 1
else
if winner_ttl > 0
then winner_ttl -= 1
else
if winner_ttl == 0 then
winner_ttl = -1
respawn = true
end
end
end
end
end
return dead_ttl, winner_ttl, level, respawn
end
--== message functions ==--
-- calculate the current message.
function calc_msg(dead_ttl, winner_ttl, level)
if dead_ttl > 0 then
return "wasted", 51, 61, 8
else
if winner_ttl > 60 then
return "a winner is you", 33, 61, 11
else
if winner_ttl > 0 then
return "level "..level, 48, 61, 9
end
end
end
return nil, nil, nil, nil
end
-- display messages to the user.
function draw_message(msg, x, y, color)
if msg ~= nil then
print(msg, x, y, color)
end
end
--== pico-8 functions ==--
function _init()
gship, groids = respawn(glevel, gship)
end
function _update()
local shp = copy_ship(gship)
local shts = copy_shots(gshots)
local r = copy_roids(groids)
if shp.alive then
shp = process_dpad(shp)
shp,shts = process_btns(shp,shts)
end
r = moved_roids(r)
shts = moved_shots(shts)
shts = rm_expired_shots(shts)
shts,r = collided_shots(shts,r,glevel)
if shp.alive then
shp = moved_ship(shp)
shp = collided_ship(shp, r)
end
gship = shp
gshots = shts
groids = r
local respwn = false
gdead_ttl, gwinner_ttl, glevel, respwn = process_death(gship, gdead_ttl, gwinner_ttl, glevel, groids)
if respwn == true then
gship, groids = respawn(glevel, gship)
end
gmsg, gmsg_x, gmsg_y, gmsg_color = calc_msg(gdead_ttl, gwinner_ttl, glevel)
end
function _draw()
cls()
rectfill(0,0,127,127,1)
if gship.alive then
draw_ship(gship)
end
draw_shots(gshots)
draw_roids(groids)
draw_message(gmsg, gmsg_x, gmsg_y, gmsg_color)
end