drop.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. -- title: drop
  2. -- author: pixelbath
  3. -- desc: 99 balls / wallsmash
  4. -- script: lua
  5. t=0
  6. x=96
  7. y=24
  8. num_rows=10
  9. num_cols=10
  10. rows_start=4
  11. balls={}
  12. lasers=0
  13. blocks={}
  14. blockWidth = 24
  15. blockHeight = 13
  16. previewDotCount = 31
  17. -- starting player stuff
  18. playerBalls = 1
  19. playerLasers = 0
  20. launchBalls = 0
  21. xpos=120
  22. ypos=130
  23. angle=-90
  24. initialAngleSpeed=1
  25. angleMin = -170
  26. angleMax = -10
  27. ballVel = 2.5
  28. laserJitter = 2 -- degrees
  29. -- launch control
  30. ballGapTime = 50
  31. laserGapTime = 20 -- todo: random delay?
  32. launchMode = false
  33. launchBallTimer = 0
  34. launchLaserTimer = 0
  35. launchBalls = playerBalls
  36. launchLasers = playerLasers
  37. function generate_grid()
  38. for row=0,num_rows-1 do
  39. blocks[row] = {}
  40. for col=0,num_cols-1 do
  41. if row < 5 and math.random() > 0.5 then
  42. blocks[row][col] = 5
  43. else
  44. blocks[row][col] = 0
  45. end
  46. end
  47. end
  48. end
  49. function launch_ball()
  50. theta=math.pi * angle / 180
  51. table.insert(balls, {
  52. x = xpos,
  53. y = ypos,
  54. vx = ballVel * math.cos(theta),
  55. vy = ballVel * math.sin(theta),
  56. })
  57. launchBalls = launchBalls - 1
  58. end
  59. function draw_preview()
  60. theta=math.pi * angle / 180
  61. xtarget=xpos + (160 * math.cos(theta))
  62. ytarget=ypos + (160 * math.sin(theta))
  63. dotGapX = (xtarget - xpos) / previewDotCount
  64. dotGapY = (ytarget - ypos) / previewDotCount
  65. -- line(xpos, ypos, xtarget, ytarget, 8)
  66. dotx=xpos
  67. doty=ypos
  68. for i=0,previewDotCount do
  69. pix(dotx, doty, 5)
  70. dotx = dotx + dotGapX
  71. doty = doty + dotGapY
  72. end
  73. print(playerBalls, xpos-8, 130, 12, false, 1, true)
  74. print(playerLasers, xpos+5, 130, 10, false, 1, true)
  75. end
  76. function draw_balls_and_lasers()
  77. for k,ball in pairs(balls) do
  78. circ(ball.x, ball.y, 1, 12)
  79. ball.x = ball.x + ball.vx
  80. ball.y = ball.y + ball.vy
  81. check_ball_collisions(ball)
  82. -- wall bounce
  83. if ball.x <= 0 or ball.x >= 240 then
  84. ball.vx = ball.vx * -1
  85. end
  86. if ball.y <= 0 then
  87. ball.vy = ball.vy * -1
  88. end
  89. -- prune the fallen boyz
  90. if ball.y >= ypos + 2 then
  91. table.remove(balls, k)
  92. end
  93. end
  94. end
  95. function check_ball_collisions(ball)
  96. for row=0,num_rows-1 do
  97. for col=0,num_cols-1 do
  98. blockVal = blocks[row][col]
  99. if blockVal > 0 then
  100. if ball.x >= col * blockWidth and ball.x <= col * blockWidth + blockWidth then
  101. if ball.y >= row * blockHeight and ball.y <= row * blockHeight + blockHeight then
  102. if row * blockHeight - ball.y <= 1 or row * blockHeight + blockHeight - ball.y <= 1 then
  103. ball.vy = ball.vy * -1
  104. blocks[row][col] = blockVal - 1
  105. else
  106. ball.vx = ball.vx * -1
  107. blocks[row][col] = blockVal - 1
  108. end
  109. end
  110. end
  111. end
  112. end
  113. end
  114. end
  115. function draw_blocks()
  116. for row=0,num_rows-1 do
  117. for col=0,num_cols-1 do
  118. blockVal = blocks[row][col]
  119. colorCursor = 1
  120. printPos = col * blockWidth + 8
  121. if blockVal > 0 then
  122. if blockVal < 18 then
  123. colorCursor = colorCursor + 1
  124. end
  125. if blockVal < 14 then
  126. colorCursor = colorCursor + 1
  127. end
  128. if blockVal < 9 then
  129. colorCursor = colorCursor + 1
  130. printPos = col * blockWidth + 10
  131. end
  132. rect(col * blockWidth, row * blockHeight, blockWidth, blockHeight, colorCursor)
  133. print(blockVal, printPos, row * blockHeight + 4, 0, false, 1, true)
  134. end
  135. end
  136. end
  137. end
  138. function draw_debug()
  139. print(angle, 2, 130, 5, false, 1, true)
  140. print(playerBalls, 18, 130, 5, false, 1, true)
  141. print(#balls, 24, 130, 5, false, 1, true)
  142. print(launchMode, 34, 130, 5, false, 1, true)
  143. print(launchBallTimer, 64, 130, 5, false, 1, true)
  144. end
  145. -- P1 starts at 0; for players up to P4 add 8 to the ID value
  146. -- U D L R, A B X Y -> 0 1 2 3, 4 5 6 7
  147. function handle_input()
  148. -- move faster
  149. if btn(5) then
  150. angleSpeed=initialAngleSpeed + initialAngleSpeed * 0.5
  151. else
  152. angleSpeed=initialAngleSpeed
  153. end
  154. -- move slower
  155. if btn(6) then
  156. angleSpeed=initialAngleSpeed * 0.3
  157. else
  158. angleSpeed=initialAngleSpeed
  159. end
  160. if btnp(4) then
  161. launchMode = true
  162. end
  163. if btn(2) then
  164. angle=angle-angleSpeed
  165. end
  166. if btn(3) then
  167. angle=angle+angleSpeed
  168. end
  169. -- clamp angle values
  170. if angle < angleMin then
  171. angle=angleMin-angleSpeed
  172. end
  173. if angle > angleMax then
  174. angle=angleMax
  175. end
  176. end
  177. -- run this stuff for init
  178. generate_grid()
  179. function TIC()
  180. cls(0)
  181. if launchMode then
  182. if launchBalls > 0 then
  183. if launchBallTimer == 0 then
  184. launchBallTimer = ballGapTime
  185. launch_ball()
  186. end
  187. launchBallTimer = launchBallTimer - 1
  188. end
  189. if launchLasers > 0 then
  190. if launchBallTimer == 0 then
  191. launchLaserTimer = ballGapTime
  192. launch_laser()
  193. end
  194. launchLaserTimer = launchLaserTimer - 1
  195. end
  196. draw_balls_and_lasers()
  197. -- if we're outta balls, turn off launchMode
  198. if #balls == 0 then
  199. launchMode = false
  200. launchBallTimer = 0
  201. launchLaserTimer = 0
  202. -- temp
  203. launchBalls = playerBalls
  204. end
  205. else
  206. handle_input()
  207. draw_preview()
  208. end
  209. draw_blocks()
  210. draw_debug()
  211. t=t+1
  212. end
  213. -- <TILES>
  214. -- 001:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  215. -- 002:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  216. -- 003:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  217. -- 004:0000000000ccc0000ccccc000ccccc000ccccc0000ccc0000000000000000000
  218. -- </TILES>
  219. -- <WAVES>
  220. -- 000:00000000ffffffff00000000ffffffff
  221. -- 001:0123456789abcdeffedcba9876543210
  222. -- 002:0123456789abcdef0123456789abcdef
  223. -- </WAVES>
  224. -- <SFX>
  225. -- 000:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000304000000000
  226. -- </SFX>
  227. -- <PALETTE>
  228. -- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
  229. -- </PALETTE>