shmup.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. -- title: untitled shmup
  2. -- author: game developer
  3. -- desc: something like Gradius/Nemesis/Salamander/Life Force
  4. -- script: lua
  5. t=0
  6. local bullet_speed = 6
  7. local bullet_timer = 0
  8. local bullet_timer_start = 10
  9. -- props: x, y, vx, vy, type
  10. local player_bullets = {}
  11. local player = {
  12. x=20,y=60,
  13. vx=0,vy=0,
  14. speed=1.5,
  15. sp=256,
  16. fire_t=0,
  17. fire_held=false,
  18. gun_type=1,
  19. gun_level=1,
  20. gunpods={},
  21. }
  22. local grad_palettes = {
  23. { 1, 2, 3, 4, 12 },
  24. { 7, 6, 5, 4, 12 },
  25. { 8, 9, 10, 11, 12 },
  26. }
  27. local enemies = {}
  28. function add_enemy(en_type, xpos, ypos)
  29. table.insert(enemies, {
  30. x=xpos, y=ypos,
  31. type=en_type,
  32. t=0,
  33. })
  34. end
  35. function mod(a, b)
  36. return math.floor(a-(math.floor(a/b)*b))
  37. end
  38. function draw_gradient(xpos, ypos, width, height, startindex, gradient)
  39. local idx = startindex or 1
  40. local grad = gradient or 1
  41. local band_w = width / 5 --# of colors in each gradient
  42. local cur_band = 0
  43. for i=0,width-1 do
  44. if cur_band >= band_w then
  45. idx = idx + 1
  46. cur_band = 0
  47. end
  48. cur_band = cur_band + 1
  49. if idx > 5 then idx = 1 end
  50. line(xpos + i, ypos, xpos + i - height, ypos + height, grad_palettes[grad][idx])
  51. end
  52. end
  53. -- TODO: various types
  54. function add_bullet()
  55. sfx(0, 'D-6', 10, 0, 11, 4)
  56. table.insert(player_bullets, {
  57. x=player.x + 10,
  58. y=player.y,
  59. vx = 0,
  60. vy = 0,
  61. })
  62. if #player.gunpods > 0 then
  63. for _,pod in ipairs(player.gunpods) do
  64. table.insert(player_bullets, {
  65. x=pod.x + 4,
  66. y=pod.y,
  67. vx = 0,
  68. vy = 0,
  69. })
  70. end
  71. end
  72. end
  73. function add_gunpod()
  74. if #player.gunpods < 5 then
  75. table.insert(player.gunpods, {
  76. x=0,y=0,vx=0,vy=0,
  77. sprs={ 259,260,261,262,263 },
  78. fr=1,t=0,frametime=5,
  79. })
  80. end
  81. end
  82. function draw_player()
  83. local engine_spr = 274
  84. if player.vx > 0 then
  85. engine_spr = 306
  86. elseif player.vx < 0 then
  87. engine_spr = 290
  88. end
  89. if player.vy > 0 then
  90. player.sp = 272
  91. elseif player.vy < 0 then
  92. player.sp = 288
  93. else
  94. player.sp = 256
  95. end
  96. -- draw engine fire then player
  97. spr(engine_spr, player.x-8, player.y+1)
  98. spr(player.sp, player.x, player.y, 0, 1, 0, 0, 2, 1)
  99. -- draw gunpods
  100. if #player.gunpods > 0 then
  101. for key,pod in ipairs(player.gunpods) do
  102. pod.t = pod.t + 1
  103. if mod(pod.t, pod.frametime) == 0 then
  104. pod.fr = pod.fr + 1
  105. if pod.fr > #pod.sprs then pod.fr = 1 end
  106. end
  107. spr(pod.sprs[pod.fr], pod.x, pod.y)
  108. -- TODO: move this into an update
  109. local targx = player.x + 4 - pod.x
  110. local targy = player.y - 10 - pod.y
  111. pod.vx = targx / 3
  112. pod.vy = targy / 3
  113. pod.x = pod.x + pod.vx
  114. pod.y = pod.y + pod.vy
  115. end
  116. end
  117. end
  118. add_gunpod()
  119. function draw_bullets()
  120. for key,val in ipairs(player_bullets) do
  121. spr(258, val.x, val.y, 0)
  122. end
  123. end
  124. -- also handle powerups since they're colliders
  125. function draw_enemies()
  126. end
  127. function handle_input()
  128. player.vx = 0
  129. player.vy = 0
  130. if btn(0) then
  131. player.vy=-player.speed
  132. end
  133. if btn(1) then
  134. player.vy=player.speed
  135. end
  136. if btn(2) then
  137. player.vx=-player.speed
  138. end
  139. if btn(3) then
  140. player.vx=player.speed
  141. end
  142. -- slow down diagonal movements
  143. if player.vx * player.vy ~= 0 then
  144. player.vx = player.vx * 0.707
  145. player.vy = player.vy * 0.707
  146. end
  147. player.x = player.x + player.vx
  148. player.y = player.y + player.vy
  149. if player.x < 5 then player.x = 5 end
  150. if player.x > 220 then player.x = 220 end
  151. if player.y < 5 then player.y = 5 end
  152. if player.y > 128 then player.y = 128 end
  153. player.fire_held = false
  154. if btn(4) then
  155. if player.fire_t <= 0 then
  156. player.fire_t = bullet_timer_start
  157. add_bullet()
  158. else
  159. player.fire_t = player.fire_t - 1
  160. end
  161. player.fire_held = true
  162. else
  163. player.fire_t = 0
  164. end
  165. -- debug
  166. if btnp(5) then
  167. player.gun_type = mod(player.gun_type + 1, 3) + 1
  168. end
  169. end
  170. function TIC()
  171. handle_input()
  172. for key,val in ipairs(player_bullets) do
  173. val.x = val.x + bullet_speed
  174. if val.x > 240 then
  175. table.remove(player_bullets, key)
  176. end
  177. end
  178. cls(0)
  179. draw_player()
  180. draw_bullets()
  181. draw_gradient(10, 40, 10, 5, mod(t/3, 5) + 1, player.gun_type)
  182. dbg("type:"..tostring(player.gun_type), 0)
  183. dbg(tostring(player.fire_held), 1)
  184. dbg(tostring(mod(t/3, 5) + 1), 2)
  185. -- dbg(tostring(player.vy), 3)
  186. t=t+1
  187. end
  188. function dbg(msg, line_no)
  189. print(msg, 5, line_no * 7, 5, true, 1, true)
  190. end
  191. -- <SPRITES>
  192. -- 000:00000000eccef0000edddefa0fcccc8bcf9deededfb8cddd00db8cdd00008cce
  193. -- 001:000000000000000090000000aa9deecc99eedcddddcccdf0ccf0000000000000
  194. -- 002:0000000000000000000000004040440030303300000000000000000000000000
  195. -- 003:00dd00000eeedddd023eeee02344320023443200023eddd00feefeee00ff0000
  196. -- 004:000000000f1eeddd02331ee023443200234eddd00feefeee00f1000000000000
  197. -- 005:000000000022000002332eef23dd31f0231ddddd0fe1eee00022000000000000
  198. -- 006:000000000022000002dd2000231ddddd23e1eeee02ee20000022000000000000
  199. -- 007:0000000000de00000f1ddddd2341eee02344320002331ee00f1eeddd00000000
  200. -- 016:dccefeed0ddddefa0fcccc8acf9deededfbbcddd0000bcdd00008ccc00000dd0
  201. -- 017:d00000009eedd000aaaaeecc9aa9dccdddcccdf0ccd00000d000000000000000
  202. -- 018:0000000000000880000889ab08899abc000889ab000008800000000000000000
  203. -- 032:ecdf00000edddf0000edddc00bbcfeeddcc99feeee8888880bc99f99000bfeee
  204. -- 033:000000000000000009aa0000cdc99e00eeedddcc88888eef9eeeedd0df000000
  205. -- 034:00000000000000000000089a000089ab0000089a000000000000000000000000
  206. -- 050:00000000000099a000889abb8889accc00889abb000099a00000000000000000
  207. -- </SPRITES>
  208. -- <WAVES>
  209. -- 000:123345667889abbcdeff1fedcba86642
  210. -- </WAVES>
  211. -- <SFX>
  212. -- 000:00d050808060c040d020e020f010f010f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000502000000000
  213. -- </SFX>
  214. -- <PALETTE>
  215. -- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
  216. -- </PALETTE>