shmup.lua 6.0 KB

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