MatthewGB

BoxShooter 2

Feb 28th, 2016
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.39 KB | None | 0 0
  1. local function pastebin(sName, sFile, bOverride) --Gets pastebin file
  2.   tPaste = http.get("http://www.pastebin.com/raw/"..sName)
  3.   if bOverride or not fs.exists(sFile) then
  4.     local tInternal = fs.open(sFile,"w")
  5.     for line in tPaste.readLine do
  6.       tInternal.writeLine(line)
  7.     end
  8.     tInternal.close()
  9.   else
  10.     print("File already exists!")
  11.   end
  12. end
  13.  
  14. function length(t)
  15.   count = 0
  16.   if t ~= nil then
  17.       for _ in pairs(t) do count = count + 1 end
  18.       return count
  19.   else
  20.     print "Got nil table!"
  21.     return 1
  22.   end
  23. end
  24.  
  25. if not daemon then
  26.   if not fs.exists("daemon") then
  27.     pastebin("0zvqEG6N","daemon")
  28.   end
  29.   os.loadAPI("daemon")
  30. end
  31.  
  32. pcall(pastebin("RhggaU2g","Asset1", true))
  33. img_ingame = paintutils.loadImage("Asset1")
  34. img_upgrades = paintutils.loadImage("Asset2")
  35.  
  36. height = 19
  37.  
  38. rounds = {}
  39. curround = -1
  40. inround = false
  41. timeinround = 0
  42. nextenemy = 0
  43. enemyspawned = 0
  44.  
  45. nextshotupgrade = 1
  46.  
  47. rounds[0] = {1,1,1}
  48. rounds[1] = {1,2,1,2,1}
  49. rounds[2] = {3,1,1,2}
  50. rounds[3] = {1,1,2,2,3,3,2,2,1,1}
  51. rounds[4] = {3,2,1,2,3,3,3,2,1}
  52. rounds[5] = {1,2,3,4,5,1,2,3,4,5}
  53. rounds[6] = {6,6,6}
  54.  
  55.  
  56. local world = setmetatable({},{
  57.     __index = {
  58.       add = function(self, type, hp, y)
  59.         object = {}
  60.         object.alive = true
  61.         if type == "enemy" then
  62.           object.type = "enemy"
  63.           object.hp = hp or math.random(math.floor(1+score/8+0.5),math.floor(score/4+0.5))
  64.           object.x = 48
  65.           object.y = math.random(2,17)
  66.         elseif type == "bullet" then
  67.           object.type = "bullet"
  68.           object.hp = hp or 1
  69.           object.x = 5
  70.           object.y = y
  71.         end
  72.         rawset(self,#self+1,object)
  73.       end;
  74.     }
  75. })
  76.  
  77. function enemySpawner2()
  78.   while true do
  79.     if inround == true then
  80.       if nextenemy < 0 and enemyspawned ~= length(rounds[curround]) then
  81.         world:add("enemy", rounds[curround][enemyspawned])
  82.         enemyspawned = enemyspawned + 1
  83.         nextenemy = 20
  84.       end
  85.     else
  86.       --print "test"
  87.     end
  88.     sleep(0.025)
  89.   end
  90. end
  91.  
  92. function chargeTimer()
  93.   while true do
  94.     sleep(0.05) --20
  95.     if nextshot ~= 0 then
  96.       nextshot = nextshot - 1
  97.     end
  98.   end
  99. end
  100.  
  101. function tickWorld()
  102.   while true do
  103.     sleep(0.05)
  104.     nextenemy = nextenemy - 1
  105.     if inround then
  106.       timeinround = timeinround + 1
  107.     end
  108.     enemieson = 0
  109.     for _,object in ipairs(world) do
  110.       if object.alive then
  111.           if object.type == "enemy" then
  112.             enemieson = enemieson + 1
  113.             object.x = object.x - 1
  114.             if object.x == 4 then
  115.               lives = lives - 1
  116.             end
  117.           elseif object.type == "bullet" then
  118.             object.x = object.x + 1
  119.           end
  120.           if object.type == "bullet" then
  121.             for _,object2 in ipairs(world) do
  122.               if object2.type == "enemy" and object2.alive then
  123.                 if object2.x == object.x and object2.y == object.y or object2.x == object.x+1 and object2.y == object.y then
  124.                   object2.hp = object2.hp - object.hp
  125.                   object.alive = false
  126.                   score = score + 1
  127.                   if object2.hp == 0 then
  128.                     object2.alive = false
  129.                   end
  130.                 end
  131.               end
  132.             end
  133.           end
  134.       end
  135.     end
  136.     if inround then
  137.       if enemieson == 0 and enemyspawned == length(rounds[curround]) then
  138.         inround = false
  139.         enemieson = 0
  140.         enemyspawned = 0
  141.       end
  142.     end
  143.   end
  144. end
  145.  
  146. function mouseRead()
  147.   while true do
  148.     _, _, evtx, evty = os.pullEvent("mouse_click")
  149.     if inround then
  150.       if nextshot == 0 then
  151.         world:add("bullet",nil,evty)
  152.         nextshot = nextshottime
  153.       end
  154.     else
  155.       if evtx > 42 and evty == 1 then --Next Round
  156.         inround = true
  157.         curround = curround + 1
  158.         timeinround = 0
  159.       end
  160.       if evtx > 15 and evtx < 20 and evty == 5 then
  161.         if score > nextshotupgrade * 20 then
  162.             nextshottime = nextshottime * 0.75
  163.             score = score - nextshotupgrade * 20
  164.         end
  165.       end
  166.     end
  167.   end
  168. end
  169.  
  170. function paintToScreen()
  171.   while true do
  172.     sleep(0.05)
  173.     if inround then
  174.         paintutils.drawImage(img_ingame,1,1)
  175.         term.setCursorPos(42,1)
  176.         term.write(nextenemy)
  177.         term.setCursorPos(30,1)
  178.         term.write(enemyspawned)
  179.         term.setCursorPos(31,1)
  180.         term.write("/")
  181.         term.setCursorPos(32,1)
  182.         term.write(length(rounds[curround]))
  183.         for _,object in ipairs(world) do
  184.           if object.alive then
  185.               if object.type == "enemy" and object.hp > 0 then
  186.                 term.setCursorPos(object.x, object.y)
  187.                 term.write(object.hp)
  188.                 paintutils.drawPixel(object.x, object.y, object.hp)
  189.               elseif object.type == "bullet" then
  190.                 paintutils.drawPixel(object.x, object.y, colours.black)
  191.               end
  192.           end
  193.         end
  194.         percent = nextshot/nextshottime
  195.         bars = percent*height
  196.         i = 1
  197.         while i<bars do
  198.           paintutils.drawPixel(1, i, colours.red)
  199.           i = i + 1
  200.         end
  201.     else
  202.         paintutils.drawImage(img_upgrades,1,1)
  203.         term.setCursorPos(42,1)
  204.         term.setBackgroundColour(colours.orange)
  205.         term.write("Next Round")
  206.         term.setCursorPos(22,2)
  207.         term.setBackgroundColour(colours.red)
  208.         term.write("Upgrades")
  209.     end
  210.   end
  211. end
  212.  
  213. function debugger()
  214.   print(inround)
  215.   sleep(0.5)
  216. end
  217.  
  218. daemon.add(enemySpawner2,"Handles enemy spawning")
  219. daemon.add(chargeTimer,"Handles shot recharge")
  220. daemon.add(tickWorld,"Ticks the world")
  221. daemon.add(mouseRead,"Reads the mouse input")
  222. --daemon.add(debugger,"Debugs")
  223. daemon.add(paintToScreen,"Paints to screen")
  224.  
  225. nextshottime = 10
  226. nextshot = nextshottime
  227. lives_max = 3
  228. lives = 3
  229. score = 0
  230.  
  231. bx = 0
  232. by = 0
  233. ba = false
  234. enemy_timer = 0
  235.  
  236.  
  237.  
  238. while true do --Main loop
  239.   --print("Running")
  240.   sleep(0.5)
  241. end
Advertisement
Add Comment
Please, Sign In to add comment