shmup.lua 5.3 KB

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