Advertisement
Guest User

ipc demo B

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