Advertisement
Guest User

ipc demo A

a guest
Jul 8th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.45 KB | None | 0 0
  1. -- the adventures of jelpi
  2. -- by zep
  3.  
  4. -- to do:
  5. -- levels and monsters
  6. -- title / restart logic
  7. -- block loot
  8. -- top-solid ground
  9. -- better duping
  10.  
  11. --ipc sync a
  12. while time()<4 do
  13. flip()
  14. end
  15. serial(0x805,0x4300,1)
  16.  
  17. -- config: num_players 1 or 2
  18. num_players = 1
  19. corrupt_mode = false
  20. max_actors = 128
  21.  
  22. music(0, 0, 3)
  23.  
  24.  
  25. function make_actor(k,x,y,d)
  26. local a = {}
  27. a.kind = k
  28. a.life = 1
  29. a.x=x a.y=y a.dx=0 a.dy=0
  30. a.ddy = 0.06 -- gravity
  31. a.w=0.3 a.h=0.5 -- half-width
  32. a.d=d a.bounce=0.8
  33. a.frame = 1 a.f0 = 0
  34. a.t=0
  35. a.standing = false
  36. if (count(actor) < max_actors) then
  37. add(actor, a)
  38. end
  39. return a
  40. end
  41.  
  42. function make_sparkle(x,y,frame,col)
  43. local s = {}
  44. s.x=x
  45. s.y=y
  46. s.frame=frame
  47. s.col=col
  48. s.t=0 s.max_t = 8+rnd(4)
  49. s.dx = 0 s.dy = 0
  50. s.ddy = 0
  51. add(sparkle,s)
  52. return s
  53. end
  54.  
  55. function make_player(x, y, d)
  56. pl = make_actor(1, x, y, d)
  57. pl.charge = 0
  58. pl.super = 0
  59. pl.score = 0
  60. pl.bounce = 0
  61. pl.delay = 0
  62. pl.id = 0 -- player 1
  63. pl.pal = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
  64.  
  65. return pl
  66. end
  67.  
  68. -- called at start by pico-8
  69. function _init()
  70. actor = {}
  71. sparkle = {}
  72.  
  73. -- spawn player
  74. for y=0,63 do for x=0,127 do
  75. if (mget(x,y) == 48) then
  76. player = make_player(x,y+1,1)
  77.  
  78. if (num_players==2) then
  79. player2 = make_player(x+2,y+1,1)
  80. player2.id = 1
  81. player2.pal = {1,3,3,4,5,6,7,11,9,10,11,12,13,15,7}
  82. end
  83.  
  84. end
  85. end end
  86. t = 0
  87.  
  88. death_t = 0
  89. end
  90.  
  91. -- clear_cel using neighbour val
  92. -- prefer empty, then non-ground
  93. -- then left neighbour
  94. function clear_cel(x, y)
  95. val0 = mget(x-1,y)
  96. val1 = mget(x+1,y)
  97. if (val0 == 0 or val1 == 0) then
  98. mset(x,y,0)
  99. elseif (not fget(val1,1)) then
  100. mset(x,y,val1)
  101. else
  102. mset(x,y,val0)
  103. end
  104. end
  105.  
  106.  
  107. function move_spawns(x0, y0)
  108.  
  109. -- spawn stuff close to x0,y0
  110.  
  111. for y=0,32 do
  112. for x=x0-10,x0+10 do
  113. val = mget(x,y)
  114. m = nil
  115.  
  116. -- pickup
  117. if (fget(val, 5)) then
  118. m = make_actor(2,x+0.5,y+1,1)
  119. m.f0 = val
  120. m.frame = val
  121. if (fget(val,4)) then
  122. m.ddy = 0 -- zero gravity
  123. end
  124. end
  125.  
  126. -- monster
  127. if (fget(val, 3)) then
  128. m = make_actor(3,x+0.5,y+1,-1)
  129. m.f0=val
  130. m.frame=val
  131. end
  132.  
  133. -- clear cel if spawned something
  134. if (m ~= nil) then
  135. clear_cel(x,y)
  136. end
  137. end
  138. end
  139.  
  140. end
  141.  
  142. -- test if a point is solid
  143. function solid (x, y)
  144. if (x < 0 or x >= 128 ) then
  145. return true end
  146.  
  147. val = mget(x, y)
  148. return fget(val, 1)
  149. end
  150.  
  151. function move_pickup(a)
  152. a.frame = a.f0
  153. -- if (flr((t/4) % 2) == 0) then
  154. -- a.frame = a.f0+1
  155. -- end
  156. end
  157.  
  158. function move_player(pl)
  159.  
  160. local b = pl.id
  161.  
  162. if (pl.life == 0) then
  163. death_t = 1
  164. for i=1,32 do
  165. s=make_sparkle(
  166. pl.x, pl.y-0.6, 96, 0)
  167. s.dx = cos(i/32)/2
  168. s.dy = sin(i/32)/2
  169. s.max_t = 30
  170. s.ddy = 0.01
  171. s.frame=96+rnd(3)
  172. s.col = 7
  173. end
  174.  
  175. del(actor,pl)
  176.  
  177. sfx(16)
  178. music(-1)
  179. sfx(5)
  180.  
  181. return
  182. end
  183.  
  184.  
  185. accel = 0.05
  186. if (pl.charge > 10) then
  187. accel = 0.08
  188. end
  189.  
  190. if (not pl.standing) then
  191. accel = accel / 2
  192. end
  193.  
  194. -- player control
  195. if (btn(0,b)) then
  196. pl.dx = pl.dx - accel; pl.d=-1 end
  197. if (btn(1,b)) then
  198. pl.dx = pl.dx + accel; pl.d=1 end
  199.  
  200. if ((btn(4,b) or btn(2,b)) and
  201. -- solid(pl.x,pl.y)) then
  202. pl.standing) then
  203. pl.dy = -0.7
  204. sfx(8)
  205. end
  206.  
  207. -- charge
  208.  
  209. if (btn(5,b) and pl.charge == 0
  210. and pl.delay == 0) then
  211. pl.charge = 15
  212.  
  213. pl.dx = pl.dx + pl.d * 0.4
  214.  
  215. if (not pl.standing) then
  216. pl.dy = pl.dy - 0.2
  217. end
  218.  
  219. sfx(11)
  220.  
  221. end
  222.  
  223. -- charging
  224.  
  225. if (pl.charge > 0 or
  226. pl.super > 0) then
  227. pl.frame = 53
  228.  
  229. if (abs(pl.dx) > 0.4 or
  230. abs(pl.dy) > 0.2
  231. ) then
  232.  
  233. for i=1,3 do
  234. local s = make_sparkle(
  235. pl.x+pl.dx*i/3,
  236. pl.y+pl.dy*i/3 - 0.3,
  237. 96+rnd(3), (pl.t*3+i)%9+7)
  238. if (rnd(2) < 1) then
  239. s.col = 7
  240. end
  241. s.dx = -pl.dx*0.1
  242. s.dy = -0.05*i/4
  243. s.x = s.x + rnd(0.6)-0.3
  244. s.y = s.y + rnd(0.6)-0.3
  245. end
  246. end
  247. end
  248.  
  249. pl.charge = max(0, pl.charge-1)
  250. if (pl.charge > 0) then
  251. pl.delay = 10
  252. else
  253. pl.delay = max(0,pl.delay-1)
  254. end
  255.  
  256. pl.super = max(0, pl.super-1)
  257.  
  258. -- frame
  259.  
  260. if (pl.standing) then
  261. pl.f0 = (pl.f0+abs(pl.dx)*2+4) % 4
  262. else
  263. pl.f0 = (pl.f0+abs(pl.dx)/2+4) % 4
  264. end
  265.  
  266. if (abs(pl.dx) < 0.1)
  267. then
  268. pl.frame=48 pl.f0=0
  269. else
  270. pl.frame = 49+flr(pl.f0)
  271. end
  272.  
  273. if (pl == player2) then
  274. pl.frame = pl.frame +75-48
  275. end
  276.  
  277. end
  278.  
  279. function move_monster(m)
  280. m.dx = m.dx + m.d * 0.02
  281.  
  282. m.f0 = (m.f0+abs(m.dx)*3+4) % 4
  283. m.frame = 112 + flr(m.f0)
  284.  
  285. if (false and m.standing and rnd(100) < 1)
  286. then
  287. m.dy = -1
  288. end
  289.  
  290. end
  291.  
  292. function move_actor(pl)
  293.  
  294. -- to do: replace with callbacks
  295.  
  296. if (pl.kind == 1) then
  297. move_player(pl)
  298. end
  299.  
  300. if (pl.kind == 2) then
  301. move_pickup(pl)
  302. end
  303.  
  304. if (pl.kind == 3) then
  305. move_monster(pl)
  306. end
  307.  
  308. pl.standing=false
  309.  
  310. -- x movement
  311.  
  312. x1 = pl.x + pl.dx +
  313. sgn(pl.dx) * 0.3
  314.  
  315. local broke_block = false
  316.  
  317. if(not solid(x1,pl.y-0.5)) then
  318. pl.x = pl.x + pl.dx
  319. else -- hit wall
  320.  
  321. -- search for contact point
  322. while (not solid(pl.x + sgn(pl.dx)*0.3, pl.y-0.5)) do
  323. pl.x = pl.x + sgn(pl.dx) * 0.1
  324. end
  325.  
  326. -- if charging, break block
  327. if (pl.charge ~= nil) then
  328.  
  329. if (pl.charge > 0 or
  330. pl.super > 0) then
  331. val = mget(x1, pl.y-0.5,0)
  332. if (fget(val,4)) then
  333. clear_cel(x1,pl.y-0.5)
  334. sfx(10)
  335. broke_block = true
  336.  
  337. -- make debris
  338.  
  339. for by=0,1 do
  340. for bx=0,1 do
  341. s=make_sparkle(
  342. 0.25+flr(x1) + bx*0.5,
  343. 0.25+flr(pl.y-0.5) + by*0.5,
  344. 22, 0)
  345. s.dx = (bx-0.5)/4
  346. s.dy = (by-0.5)/4
  347. s.max_t = 30
  348. s.ddy = 0.02
  349. end
  350. end
  351.  
  352. else
  353. if (abs(pl.dx) > 0.2) then
  354. sfx(12) -- thump
  355. end
  356. end
  357.  
  358. -- bumping kills charge
  359. if (pl.charge < 20) then
  360. pl.charge = 0
  361. end
  362.  
  363. end
  364. end
  365.  
  366. -- bounce
  367. if (pl.super == 0 or
  368. not broke_block) then
  369. pl.dx = pl.dx * -0.5
  370. end
  371.  
  372. if (pl.kind == 3) then
  373. pl.d = pl.d * -1
  374. pl.dx=0
  375. end
  376.  
  377. end
  378.  
  379. -- y movement
  380.  
  381. if (pl.dy < 0) then
  382. -- going up
  383.  
  384. if (solid(pl.x-0.2, pl.y+pl.dy-1) or
  385. solid(pl.x+0.2, pl.y+pl.dy-1))
  386. then
  387. pl.dy=0
  388.  
  389. -- search up for collision point
  390. while ( not (
  391. solid(pl.x-0.2, pl.y-1) or
  392. solid(pl.x+0.2, pl.y-1)))
  393. do
  394. pl.y = pl.y - 0.01
  395. end
  396.  
  397. else
  398. pl.y = pl.y + pl.dy
  399. end
  400.  
  401. else
  402.  
  403. -- going down
  404. if (solid(pl.x-0.2, pl.y+pl.dy) or
  405. solid(pl.x+0.2, pl.y+pl.dy)) then
  406.  
  407. -- bounce
  408. if (pl.bounce > 0 and
  409. pl.dy > 0.2)
  410. then
  411. pl.dy = pl.dy * -pl.bounce
  412. else
  413.  
  414. pl.standing=true
  415. pl.dy = 0
  416.  
  417. end
  418.  
  419. --snap down
  420. while (not (
  421. solid(pl.x-0.2,pl.y) or
  422. solid(pl.x+0.2,pl.y)
  423. ))
  424. do pl.y = pl.y + 0.05 end
  425.  
  426. --pop up even if bouncing
  427. while(solid(pl.x-0.2,pl.y-0.1)) do
  428. pl.y = pl.y - 0.05 end
  429. while(solid(pl.x+0.2,pl.y-0.1)) do
  430. pl.y = pl.y - 0.05 end
  431.  
  432. else
  433. pl.y = pl.y + pl.dy
  434. end
  435.  
  436. end
  437.  
  438.  
  439. -- gravity and friction
  440. pl.dy = pl.dy + pl.ddy
  441. pl.dy = pl.dy * 0.95
  442.  
  443. -- x friction
  444. if (pl.standing) then
  445. pl.dx = pl.dx * 0.8
  446. else
  447. pl.dx = pl.dx * 0.9
  448. end
  449.  
  450. -- counters
  451. pl.t = pl.t + 1
  452. end
  453.  
  454. function collide_event(a1, a2)
  455. if(a1.kind==1) then
  456. if(a2.kind==2) then
  457.  
  458. if (a2.frame==64) then
  459. a1.super = 120
  460. a1.dx = a1.dx * 2
  461. --a1.dy = a1.dy-0.1
  462. -- a1.standing = false
  463. sfx(13)
  464. end
  465.  
  466. -- gem
  467. if (a2.frame==80) then
  468. a1.score = a1.score + 1
  469. sfx(9)
  470. end
  471.  
  472. del(actor,a2)
  473.  
  474. end
  475.  
  476. -- charge or dupe monster
  477.  
  478. if(a2.kind==3) then -- monster
  479. if(a1.charge > 0 or
  480. a1.super > 0 or
  481. (a1.y-a1.dy) < a2.y-0.7) then
  482. -- slow down player
  483. a1.dx = a1.dx * 0.7
  484. a1.dy = a1.dy * -0.7-- - 0.2
  485.  
  486. -- explode
  487. for i=1,16 do
  488. s=make_sparkle(
  489. a2.x, a2.y-0.5, 96+rnd(3), 7)
  490. s.dx = s.dx + rnd(0.4)-0.2
  491. s.dy = s.dy + rnd(0.4)-0.2
  492. s.max_t = 30
  493. s.ddy = 0.01
  494.  
  495. end
  496.  
  497. -- kill monster
  498. -- to do: in move_monster
  499. sfx(14)
  500. del(actor,a2)
  501.  
  502. else
  503.  
  504. -- player death
  505. a1.life=0
  506.  
  507.  
  508. end
  509. end
  510.  
  511. end
  512. end
  513.  
  514. function move_sparkle(sp)
  515. if (sp.t > sp.max_t) then
  516. del(sparkle,sp)
  517. end
  518.  
  519. sp.x = sp.x + sp.dx
  520. sp.y = sp.y + sp.dy
  521. sp.dy= sp.dy+ sp.ddy
  522. sp.t = sp.t + 1
  523. end
  524.  
  525.  
  526. function collide(a1, a2)
  527. if (a1==a2) then return end
  528. local dx = a1.x - a2.x
  529. local dy = a1.y - a2.y
  530. if (abs(dx) < a1.w+a2.w) then
  531. if (abs(dy) < a1.h+a2.h) then
  532. collide_event(a1, a2)
  533. end
  534. end
  535. end
  536.  
  537. function collisions()
  538.  
  539. for a1 in all(actor) do
  540. collide(player,a1)
  541. end
  542.  
  543. if (player2 ~= nil) then
  544. for a1 in all(actor) do
  545. collide(player2,a1)
  546. end
  547. end
  548.  
  549. end
  550.  
  551. function outgame_logic()
  552.  
  553. if (death_t > 0) then
  554. death_t = death_t + 1
  555. if (death_t > 30 and
  556. btn(4) or btn(5))
  557. then
  558. music(-1)
  559. sfx(-1)
  560. sfx(0)
  561. dpal={0,1,1, 2,1,13,6,
  562. 4,4,9,3, 13,1,13,14}
  563.  
  564. -- palette fade
  565. for i=0,40 do
  566. for j=1,15 do
  567. col = j
  568. for k=1,((i+(j%5))/4) do
  569. col=dpal[col]
  570. end
  571. pal(j,col,1)
  572. end
  573. flip()
  574. end
  575.  
  576. -- restart cart end of slice
  577. run()
  578. end
  579. end
  580. end
  581.  
  582. function _update()
  583. --ipc a
  584. serial(0x805,0x5f4c,8)
  585.  
  586. foreach(actor, move_actor)
  587. foreach(sparkle, move_sparkle)
  588. collisions()
  589. move_spawns(player.x, player.y)
  590.  
  591. outgame_logic()
  592.  
  593. if (corrupt_mode) then
  594. for i=1,5 do
  595. poke(rnd(0x8000),rnd(0x100))
  596. end
  597. end
  598.  
  599. t=t+1
  600. end
  601.  
  602. function draw_sparkle(s)
  603.  
  604. if (s.col > 0) then
  605. for i=1,15 do
  606. pal(i,s.col)
  607. end
  608. end
  609.  
  610. spr(s.frame, s.x*8-4, s.y*8-4)
  611.  
  612. pal()
  613. end
  614.  
  615. function draw_actor(pl)
  616.  
  617. if (pl.pal ~= nil) then
  618. for i=1,15 do
  619. -- pal(i, pl.pal[i])
  620. end
  621. end
  622.  
  623. if (pl.charge ~= nil and
  624. pl.charge > 0) then
  625.  
  626. for i=2,15 do
  627. pal(i,7+((pl.t/2) % 8))
  628. end
  629. -- pal(2,7)
  630.  
  631. end
  632.  
  633. if (pl.super ~= nil and
  634. pl.super > 0) then
  635.  
  636. for i=2,15 do
  637. pal(i,6+((pl.t/2) % 2))
  638. end
  639.  
  640. end
  641.  
  642. spr(pl.frame,
  643. pl.x*8-4, pl.y*8-8,
  644. 1, 1, pl.d < 0)
  645.  
  646. pal()
  647. end
  648.  
  649. function _draw()
  650.  
  651. -- sky
  652. camera (0, 0)
  653. rectfill (0,0,127,127,12)
  654. --for y=1,7 do
  655. -- rect(0,63-y*2.5,127,63-y*2.5,6) end
  656.  
  657. -- background
  658.  
  659. -- sspr(88,0,8,8,0,0,128,128)
  660.  
  661. -- sky gradient
  662. if (false) then
  663. for y=0,127 do
  664. col=sget(88,(y+(y%4)*6) / 16)
  665. line(0,y,127,y,col)
  666. end
  667. end
  668.  
  669. -- clouds behind mountains
  670. local x = t / 8
  671. x = x % 128
  672. local y=0
  673. mapdraw(16, 32, -x, y, 16, 16, 0)
  674. mapdraw(16, 32, 128-x, y, 16, 16, 0)
  675.  
  676.  
  677. local bgcol = 13 -- mountains
  678. pal(5,bgcol) pal(2,bgcol)
  679. pal(13,6) -- highlights
  680. y = 0
  681. mapdraw (0, 32, 0, y, 16, 16, 0)
  682. pal()
  683.  
  684.  
  685. -- map and actors
  686. cam_x = mid(0,player.x*8-64,1024-128)
  687.  
  688. if (player2 ~= nil) then
  689. cam_x =
  690. mid(0,player2.x*8-64,1024-128) / 2 +
  691. cam_x / 2
  692. end
  693.  
  694. --cam_y = mid(0,player.y*6-40,128)
  695. cam_y = 84
  696. camera (cam_x,cam_y)
  697. pal(12,0)
  698. mapdraw (0,0,0,0,128,64,1)
  699. pal()
  700. foreach(sparkle, draw_sparkle)
  701. foreach(actor, draw_actor)
  702.  
  703. -- forground map
  704. -- mapdraw (0,0,0,0,128,64,2)
  705.  
  706. -- player score
  707. camera(0,0)
  708. color(7)
  709.  
  710. if (death_t > 60) then
  711. print("press button to restart",
  712. 18-1,10-0,8+(t/4)%2)
  713. print("press button to restart",
  714. 18,10,7)
  715. end
  716.  
  717. if (false) then
  718. cursor(0,2)
  719. print("actors:"..count(actor))
  720. print("score:"..player.score)
  721. print(stat(1))
  722. end
  723. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement