drop.lua 5.9 KB

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