miniblaster.lua 13 KB

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