miniblaster.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. -- title: miniblaster
  2. -- author: pixelbath
  3. -- desc: short description
  4. -- site: website link
  5. -- license: MIT License (change this to your license of choice)
  6. -- version: 0.1
  7. -- script: lua
  8. t=0
  9. x=96
  10. y=24
  11. entities={}
  12. walls={
  13. [1]=true,
  14. [2]=true,
  15. [16]=true
  16. }
  17. config={
  18. jump_spd=2.2,
  19. walk_spd=1.0,
  20. slide_spd=2.0,
  21. }
  22. player={
  23. x=96, y=24,
  24. vx=0, vy=0,
  25. gx=0, gy=0,
  26. is_jmp=false,
  27. flip=0,
  28. spr=256,
  29. w_spr={257,258,256},
  30. j_spr=259,
  31. w_spr_idx=0,
  32. w_timer=0,
  33. is_wlk=0,
  34. dir=0, -- 0=horizontal, 1=diagonal, 2=vertical
  35. slide_timer=-1,
  36. bullets={},
  37. }
  38. debug={
  39. pline=0,
  40. px=2, py=2,
  41. }
  42. function solid(x, y)
  43. return walls[mget((x)//8,(y)//8)]
  44. end
  45. function debug:print(msg)
  46. rect(self.px, self.py + (self.pline * 7), #msg * 4, 0, 1)
  47. print(msg, self.px, self.py + (self.pline * 7), 4, true, 1, true)
  48. self.pline = self.pline + 1
  49. end
  50. function debug:draw()
  51. self.pline = 0
  52. -- draw player shoot direction
  53. if false then
  54. local dx, dy = player.x+4, player.y+4
  55. local dist = 100
  56. if player.flip == 1 then dist = -100 end
  57. if player.dir == 0 then
  58. dx = player.x + dist
  59. elseif player.dir == 1 then
  60. dx = player.x + dist
  61. dy = player.y - 100
  62. elseif player.dir == 2 then
  63. dy = player.y - 100
  64. end
  65. line(player.x+4, player.y+4, dx, dy, 1)
  66. end
  67. debug:print("player: "..player.x..", "..player.y)
  68. debug:print("vy: "..tostring(player.vy))
  69. -- controller
  70. local cont_x, cont_y = 2, 110
  71. rect(cont_x, cont_y, 10, 5, 0)
  72. line(cont_x+1, cont_y+2, cont_x+3, cont_y+2, 8)
  73. line(cont_x+2, cont_y+1, cont_x+2, cont_y+3, 8)
  74. pix(cont_x+6, cont_y+3, 8)
  75. pix(cont_x+8, cont_y+3, 8)
  76. pix(cont_x+6, cont_y+1, 8)
  77. pix(cont_x+8, cont_y+1, 8)
  78. if btn(0) then pix(cont_x+2, cont_y+1, 10) end
  79. if btn(1) then pix(cont_x+2, cont_y+3, 10) end
  80. if btn(2) then pix(cont_x+1, cont_y+2, 10) end
  81. if btn(3) then pix(cont_x+3, cont_y+2, 10) end
  82. if btn(4) then pix(cont_x+6, cont_y+3, 10) end
  83. if btn(5) then pix(cont_x+8, cont_y+3, 10) end
  84. if btn(6) then pix(cont_x+6, cont_y+1, 10) end
  85. if btn(7) then pix(cont_x+8, cont_y+1, 10) end
  86. end
  87. function player:update()
  88. local dir_held = false
  89. local up_held = false
  90. if btn(0) then
  91. player.dir = 2
  92. up_held = true
  93. end
  94. -- left
  95. if btn(2) then
  96. player.vx = -config.walk_spd
  97. player.flip = 1
  98. dir_held = true
  99. if up_held then
  100. player.dir = 1
  101. else
  102. player.dir = 0
  103. end
  104. end
  105. -- right
  106. if btn(3) then
  107. player.vx = config.walk_spd
  108. player.flip = 0
  109. dir_held = true
  110. if up_held then
  111. player.dir = 1
  112. else
  113. player.dir = 0
  114. end
  115. end
  116. -- down + jump to do a slide
  117. if btn(1) then
  118. -- slide
  119. if btnp(4) then
  120. if player.vy == 0 and player.slide_timer == -1 then
  121. player.slide_timer = 10
  122. player.vx = config.slide_spd
  123. if player.flip == 1 then player.vx = -config.slide_spd end
  124. player.is_wlk = false
  125. end
  126. end
  127. else
  128. -- jump
  129. if btn(4) then
  130. if player.vy == 0 and not player.is_jmp then
  131. player.vy = -config.jump_spd
  132. player.is_wlk = 0
  133. player.is_jmp = true
  134. end
  135. else
  136. if player.is_jmp and player.vy <= 0 then
  137. player.vy = 0
  138. player.is_jmp = false
  139. end
  140. end
  141. end
  142. if btnp(5) then
  143. player:spawnBullet()
  144. end
  145. if player.slide_timer == -1 then
  146. player.vx = math.max(-1.5, math.min(1.5, player.vx))
  147. end
  148. -- set flag if walking, clamp if not
  149. if math.abs(player.vx) < 0.01 then
  150. player.vx = 0
  151. player.is_wlk = 0
  152. else
  153. player.is_wlk = 1
  154. end
  155. if solid(player.x+player.vx,player.y+player.vy) or
  156. solid(player.x+4+player.vx,player.y+player.vy) or
  157. solid(player.x+player.vx,player.y+4+player.vy) or
  158. solid(player.x+4+player.vx,player.y+4+player.vy) then
  159. player.vx = 0
  160. end
  161. if solid(player.x,player.y+8+player.vy) or
  162. solid(player.x+7,player.y+8+player.vy) then
  163. player.vy = 0
  164. else
  165. -- gravity, terminal velocity
  166. player.vy = math.min(6.0, player.vy + 0.12)
  167. player.spr = player.j_spr
  168. end
  169. player.x = player.x + player.vx
  170. player.y = player.y + player.vy
  171. if not dir_held and player.slide_timer == -1 then
  172. player.vx = player.vx * 0.8
  173. end
  174. -- ground fx
  175. if player.vy == 0 then
  176. -- do slidey stuff, otherwise walk
  177. if player.slide_timer > 0 then
  178. player.slide_timer = player.slide_timer - 1
  179. elseif player.slide_timer == 0 then
  180. player.vx = 0
  181. player.slide_timer = -1
  182. else
  183. if player.is_wlk == 1 then
  184. player.w_timer = player.w_timer + 1
  185. if player.w_timer > 5 then
  186. player.w_spr_idx = (player.w_spr_idx + 1) % 3 + 1
  187. player.spr = player.w_spr[player.w_spr_idx]
  188. player.w_timer = 0
  189. end
  190. else
  191. player.spr = 256
  192. end
  193. end
  194. end
  195. -- bulletsch
  196. for key, bullet in ipairs(player.bullets) do
  197. if bullet.x < -8 or bullet.x > 248 then
  198. table.remove(player.bullets, key)
  199. goto bullet_dead
  200. end
  201. if bullet.y < -8 or bullet.y > 144 then
  202. table.remove(player.bullets, key)
  203. goto bullet_dead
  204. end
  205. bullet.x = bullet.x + bullet.vx
  206. bullet.y = bullet.y + bullet.vy
  207. ::bullet_dead::
  208. end
  209. end
  210. function player:spawnBullet()
  211. local testspd = 5
  212. local bx, by = self.x + 4, self.y + 4
  213. local lvx, lvy, dist = 0, 0, testspd
  214. if self.flip == 1 then dist = -testspd end
  215. if self.dir == 0 then
  216. lvx = dist
  217. elseif self.dir == 1 then
  218. lvx = dist
  219. lvy = -math.abs(dist)
  220. elseif self.dir == 2 then
  221. lvy = -math.abs(dist)
  222. end
  223. -- correct diagonal movements
  224. if lvx * lvy ~= 0 then
  225. lvx = lvx * 0.707
  226. lvy = lvy * 0.707
  227. end
  228. table.insert(self.bullets, {
  229. x=bx, y=by, vx=lvx, vy=lvy,
  230. })
  231. end
  232. function player:draw()
  233. spr(player.spr, player.x, player.y, 0,1, player.flip,0,1,1)
  234. for key, bullet in ipairs(player.bullets) do
  235. circ(bullet.x, bullet.y, 1, 4)
  236. end
  237. end
  238. function TIC()
  239. cls(13)
  240. map()
  241. player:update()
  242. player:draw()
  243. debug:draw()
  244. t=t+1
  245. end
  246. -- <TILES>
  247. -- 001:ccccccccddddddddeeeeeeeeffffffff88e99e888e9889e8e988889e88888888
  248. -- 002:dce88edcdc9e8edcdc89eedcdc889edcdc88eedcdc8e9edcdce98edcdc988edc
  249. -- 004:00000000f00f0f0f00f000ff00000000000000000000000f000000000000000f
  250. -- 005:00000000e000ddeefe0deeffff0eef0fff0eff0f0f0ef0f0fe0ef0f00e0f0f00
  251. -- 006:00000000f0fff0fe0f000fff0000000f0000000f000000f00000000f000000f0
  252. -- 007:00000000000ddeefe0deeff0f0eef0f0f0eff0f0f0ef0f00e0ef0f00e0f0f000
  253. -- 008:e0000000ef000000e0f00000ff000000f0000000fff00000feff00000fefffff
  254. -- 009:000000f000000ff000000fe00000f0f0000f0f00f000f0f0000f0f00fff0ff00
  255. -- 010:0000000008089189800000080000000880000009000809088000000800000008
  256. -- 011:000000000000000000000000000000000000000000000088000800f000000080
  257. -- 016:dceeeccdedeeeedced88899ced888ffded88888ded88888dfe88888efffffffe
  258. -- 020:000000000000000000000000000000f0000f000f000000f00f0fff0f00000000
  259. -- 021:ff0ef000fe0e0f00ff0ff000f00f00000f0fff00f00feff0f000feff00000000
  260. -- 022:0000000f0000000f0000000f00000f0f00f000f000000f0ffffff0ff00000000
  261. -- 023:f0ef0000e0e0f000f0ff000000f00000f0fff00000feff00000fefff00000000
  262. -- 024:000000000ddeeff0deeff00feef0f000eff0f000ef0f0000ef0f0000f0f00000
  263. -- 025:00000000fff0fe00000fffe000000ff000000ff00000f0f000000fe00000f0e0
  264. -- 026:0000010980000008000800080000000800000000000000080000000000000000
  265. -- 027:f888881000000080000000000008808000000000000000800000000000000080
  266. -- 032:2232232233333333132321211021121102102101101010100100000000000000
  267. -- 033:2232222323233332322123121012112101010210001001001000000000000000
  268. -- 034:0423433331121022312301112301301023102120213211003110102102220202
  269. -- 035:3223443321221022001000111020201020102020100000002100102102220202
  270. -- 036:3332243322121210110000002010202010101010100010000222022220012020
  271. -- 037:3334234022011114111123030103013202131022001132121201011320202220
  272. -- 048:01111111142244441c44cccc1322333314334444132233331211222201111111
  273. -- 049:1111111144444444cccccccc3333333344444444333333332222222211111111
  274. -- 050:1111111044442241cccc44c13333223144443341333322312222112111111110
  275. -- 051:13c332211444443113c332211444443113c332211444443113c3322114444431
  276. -- </TILES>
  277. -- <SPRITES>
  278. -- 000:008558000085580008aaa80008aaaa8008a88a8008a80a8008aa8aa808880880
  279. -- 001:008558000085580008aaa80008aaaa8008a88a808a800a808aa80aa888800880
  280. -- 002:0085580000855800008aa80008aaa80008a8880008a8a80008aa8a8008888800
  281. -- 003:008558000085580008aaa80008aaaa8008a88a8008aa8aa80888088000000000
  282. -- </SPRITES>
  283. -- <MAP>
  284. -- 005:000000200000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  285. -- 006:000000200000000010101000000000001010000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  286. -- 007:000000200000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  287. -- 008:000000200000000000000000000000000000000000101010809020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  288. -- 009:000000200000000000000000000000000010000000000000819120000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  289. -- 010:000000200040000000000000001010000000003040506070809020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  290. -- 011:000000203141000000100000000000000000003141516171819120000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  291. -- 012:000000011010101010101010101010101010101010101010101001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  292. -- </MAP>
  293. -- <WAVES>
  294. -- 000:00000000ffffffff00000000ffffffff
  295. -- 001:0123456789abcdeffedcba9876543210
  296. -- 002:0123456789abcdef0123456789abcdef
  297. -- </WAVES>
  298. -- <SFX>
  299. -- 000:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000304000000000
  300. -- </SFX>
  301. -- <TRACKS>
  302. -- 000:100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  303. -- </TRACKS>
  304. -- <PALETTE>
  305. -- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
  306. -- </PALETTE>