einhander.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. -- title: einhander
  2. -- author: pixelbath
  3. -- desc: one-handed
  4. -- script: lua
  5. seed=12345
  6. player={
  7. x=20, y=50,
  8. inv=0,
  9. show=1,
  10. fire=0,
  11. gun_down=0,
  12. gun_anim=0,
  13. gun_held=0,
  14. }
  15. gunsprites={
  16. 292, -- vulcan
  17. 294, -- cannon
  18. 296, -- spreader
  19. 298, -- wasp
  20. 300, -- grenade
  21. 302, -- hedgehog
  22. }
  23. gunnames={
  24. {324, "V", "ULCAN"},
  25. {325, "C", "ANNON"},
  26. {326, "S", "PREADER"},
  27. {327, "W", "ASP"},
  28. {328, "G", "RENADE"},
  29. {329, "H", "EDGEHOG"},
  30. }
  31. vulcanMuzzleSprites={
  32. 264, 266, 280
  33. }
  34. shellParticles={}
  35. -- particles are just white pixels by default
  36. for i=1,15 do
  37. table.insert(shellParticles, {x=0,y=0,vx=0,vy=0,age=0,dieAt=0,color=12,idx=0,isPixel=true})
  38. end
  39. particlePointer=1 -- points at the next particle to be spawned
  40. weaponBgSize=16--12
  41. weaponBgTimer=0
  42. weaponAmmo=3000
  43. p_bullets = {}
  44. p_big_bullets = {}
  45. multiplier = 12
  46. multiplier_sub = 0
  47. multiplier_tick = 0
  48. score = 25000
  49. t=0
  50. math.randomseed(seed)
  51. function drawParallelogram(x, y, w, h, skewpx, color)
  52. tri(x+skewpx, y, x+w, y+h, x, y+h, color)
  53. tri(x+skewpx, y, x+w, y+h, x+w+skewpx, y, color)
  54. end
  55. function spawnPixelParticle(x, y, vx, vy, age, dieAt, color)
  56. local p = shellParticles[particlePointer]
  57. p.x = x
  58. p.y = y
  59. p.vx = vx
  60. p.vy = vy
  61. p.age = age
  62. p.dieAt = dieAt
  63. p.color = color
  64. p.isPixel = true
  65. particlePointer = particlePointer + 1
  66. if particlePointer > 15 then particlePointer = 1 end
  67. end
  68. function spawnSpriteParticle(x, y, vx, vy, age, dieAt, index)
  69. local p = shellParticles[particlePointer]
  70. p.x = x
  71. p.y = y
  72. p.vx = vx
  73. p.vy = vy
  74. p.age = age
  75. p.dieAt = dieAt
  76. p.idx = index
  77. p.isPixel = false
  78. particlePointer = particlePointer + 1
  79. if particlePointer > 15 then particlePointer = 1 end
  80. end
  81. function drawPlayer()
  82. if (player.show) then
  83. -- arm
  84. if player.gun_down then
  85. spr(260, player.x+12, player.y+11, 0, 1, 0, 0, 2, 1)
  86. else
  87. spr(260, player.x+12, player.y-2, 0, 1, 2, 0, 2, 1)
  88. end
  89. -- ship
  90. spr(256, player.x, player.y, 0, 1, 0, 0, 4, 2)
  91. end
  92. -- draw regular bullets
  93. for key,val in ipairs(p_bullets) do
  94. spr(262, val.x, val.y, 0)
  95. end
  96. -- custom routines depending on gun held
  97. if player.gun_held > 0 then
  98. local gunsprite = gunsprites[player.gun_held]
  99. local gunpos = player.y - 2
  100. if player.gun_down then
  101. gunsprite = gunsprite + 16
  102. gunpos = gunpos + 14
  103. end
  104. spr(gunsprite, player.x+12, gunpos, 0, 1, 0, 0, 2, 1)
  105. -- vulcan
  106. if player.gun_held == 1 then
  107. local flipfire = 0
  108. local muzzleIdx = math.random(0,3)
  109. if math.random(1, 2) == 2 then
  110. flipfire = 2
  111. end
  112. if player.fire == 0 then
  113. player.fire = 2
  114. else
  115. player.fire = player.fire - 1
  116. end
  117. -- begin dakka
  118. if muzzleIdx > 0 then
  119. if player.gun_down then
  120. spr(vulcanMuzzleSprites[muzzleIdx], player.x + 27, player.y + 14, 0, 1, flipfire, 0, 2, 1)
  121. else
  122. spr(vulcanMuzzleSprites[muzzleIdx], player.x + 28, player.y - 2, 0, 1, flipfire, 0, 2, 1)
  123. end
  124. end
  125. if t % 10 == 0 then
  126. local randFactor = math.random(10,100)
  127. local spawnOffsetY = 1
  128. local lineStart = math.random(player.x+20, 260)
  129. if player.gun_down then
  130. spawnOffsetY = 18
  131. -- vulcan tilts downward so we get to do trig! yay!
  132. lineStart = lineStart - player.x
  133. local angle = 8
  134. local radius2 = lineStart+38
  135. local c = math.cos(angle * math.pi / 180)
  136. local s = math.sin(angle * math.pi / 180)
  137. line(c * lineStart + player.x + 18, s * lineStart + player.y + 17, c * radius2 + player.x + 18, s * radius2 + player.y + 17, 14)
  138. else
  139. line(lineStart, player.y+1, lineStart+40, player.y+1, 14)
  140. end
  141. -- shells
  142. spawnPixelParticle(player.x+14, player.y+spawnOffsetY, -0.2, -0.6 + (randFactor / 100), 0, 50, 15)
  143. end
  144. -- update shells with light gravity
  145. for i=1,#shellParticles do
  146. local p = shellParticles[i]
  147. p.vy = p.vy + 0.1
  148. end
  149. end
  150. end
  151. for i=1,#shellParticles do
  152. local p = shellParticles[i]
  153. if p.age < p.dieAt then
  154. if p.isPixel then
  155. pix(p.x, p.y, p.color)
  156. else
  157. spr(p.idx, p.x, p.y)
  158. end
  159. end
  160. end
  161. end
  162. function drawUI()
  163. -- weapon bg
  164. -- spr(322, 4, 114, 0, 1, 0, 0, 1, 2)
  165. -- rect(12, 114, 8, 16, 6)
  166. -- spr(323, 20, 114, 0, 1, 0, 0, 1, 2)
  167. drawParallelogram(6, 115, weaponBgSize, 12, 6, 6)
  168. -- selected weapon
  169. if player.gun_held > 0 then
  170. local lettersprite = gunnames[player.gun_held][1]
  171. local guntext = gunnames[player.gun_held][3]
  172. spr(lettersprite, 14, 118, 0, 1)
  173. print(guntext, 24, 122, 12, true, 1, true)
  174. print(weaponAmmo, 12, 129, 12, true, 1, true)
  175. end
  176. -- multiplier
  177. -- TODO: need to do multipliers over 10, so the X needs to move left when that happens
  178. spr(340, 164, 114, 0)
  179. --spr(340 + multiplier, 172, 113, 0)
  180. spr(330, 180, 114, 0, 1, 0, 0, 4, 1)
  181. spr(331, 212, 114, 0, 1, 0, 0, 3, 1)
  182. spr(334, 236, 114, 0, 1)
  183. if multiplier_sub > 0 then
  184. for i = 1, multiplier_sub do
  185. spr(335, 177 + (i * 6), 114, 0)
  186. end
  187. end
  188. -- score
  189. local scorestring = tostring(score)
  190. for i = 1, #scorestring do
  191. local char = scorestring:sub(i, i)
  192. local numsprite = 340 + tonumber(char)
  193. -- this is a hack because I ordered the numbers weirdly
  194. if char == "0" then numsprite = 350 end
  195. spr(numsprite, (230 - (8 * #scorestring)) + (i * 8), 123, 0)
  196. end
  197. end
  198. -- 1xxxxxxxxxxx
  199. -- 0xxxxxxxxxxx
  200. -- 0xxxxxxxxxxx
  201. -- 1xxxxxxxxxxx
  202. function drawWater(waterHeight)
  203. for i=0,waterHeight-1 do
  204. local row = (135 - i - waterHeight) * 120
  205. local rowDest = (135 + i - waterHeight + 1) * 120
  206. memcpy(rowDest, row, 120)
  207. end
  208. end
  209. function drawDebug()
  210. -- print("p.gdown: " .. tostring(player.gun_down), 5, 5, 12, true, 1, true)
  211. -- print("spr: " .. tostring(vulcanMuzzleSprites[player.fire + 1]), 5, 13, 12, true, 1, true)
  212. end
  213. function update()
  214. if btn(0) then
  215. if player.y > 0 then
  216. player.y=player.y-2
  217. end
  218. end
  219. if btn(1) then
  220. if player.y < 120 then
  221. player.y=player.y+2
  222. end
  223. end
  224. if btn(2) then
  225. if player.x > 1 then
  226. player.x=player.x-2
  227. end
  228. end
  229. if btn(3) then
  230. if player.x < 216 then
  231. player.x=player.x+2
  232. end
  233. end
  234. if btnp(4) then
  235. player.gun_held = 1
  236. player.gun_down = not player.gun_down
  237. end
  238. if btn(5) then
  239. --if player.gun_held == 0 then
  240. if player.fire == 0 then
  241. player.fire = 5
  242. table.insert(p_bullets, {x=player.x+10, y=player.y+2})
  243. else
  244. player.fire = player.fire - 1
  245. end
  246. --end
  247. end
  248. for key,val in ipairs(p_bullets) do
  249. p_bullets[key].x = p_bullets[key].x + 8
  250. if val.x > 240 then
  251. table.remove(p_bullets, key)
  252. end
  253. end
  254. for i=1,#shellParticles do
  255. local p = shellParticles[i]
  256. p.x = p.x + p.vx
  257. p.y = p.y + p.vy
  258. p.age = p.age + 1
  259. end
  260. t=t+1
  261. end
  262. function TIC()
  263. update()
  264. cls(0)
  265. drawPlayer()
  266. -- drawWater(10)
  267. drawUI()
  268. drawDebug()
  269. end