LDDestroier

top down test (CC)

Jan 21st, 2017
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.86 KB | None | 0 0
  1. --[[
  2. Test of top-down shooter
  3. pastebin get QVDcNiud test
  4. --]]
  5.  
  6. local channel = 1990
  7. local modem = peripheral.find("modem")
  8. if not modem then error("Get a modem.") end
  9. modem.open(channel)
  10.  
  11. --local tsv = term.current().setVisible
  12. local scr_x, scr_y = term.getSize()
  13. local keysDown = {}
  14. local netKeysDown = {}
  15. local miceDown = {}
  16. local netMiceDown = {}
  17. local p --network player
  18.  
  19. control = {
  20.     UP = keys.w,
  21.     DOWN = keys.s,
  22.     LEFT = keys.a,
  23.     RIGHT = keys.d,
  24.     SHOOTUP = keys.up,
  25.     SHOOTDOWN = keys.down,
  26.     SHOOTLEFT = keys.left,
  27.     SHOOTRIGHT = keys.right,
  28.     BLOCKPUT = keys.space,
  29. }
  30.  
  31. chVis = function(vis)
  32.     if tsv then tsv(vis) end
  33. end
  34.  
  35. bulletMap = {} --map of all bullets onscreen
  36. backMap = {}   --the background prettyness
  37. solidMap = {}  --the actual hitbox
  38. blockMap = {}  --blocks that block bullets, but not players
  39.  
  40. makePlayer = function(name,x,y,color,number)
  41.     return {
  42.         name = name,
  43.         x = x,
  44.         y = y,
  45.         color = color or colors.gray,
  46.         health = 99,
  47.         maxhealth = 99,
  48.         number = number or 1,
  49.         cooldown = 0,
  50.     }
  51. end
  52.  
  53. makeBullet = function(x,y,xspeed,yspeed,char)
  54.     local boolet = {
  55.         x = x,
  56.         y = y,
  57.         xspeed = xspeed,
  58.         yspeed = yspeed,
  59.         char = char or "o",
  60.     }
  61.     bulletMap[#bulletMap+1] = boolet
  62. end
  63.  
  64. placeBlock = function(x,y,char,txt,bg)
  65.     local block = {
  66.         x = x,
  67.         y = y,
  68.         char = char or "%",
  69.         txt = txt or colors.white,
  70.         bg = bg or colors.cyan,
  71.     }
  72.     blockMap[#blockMap+1] = block
  73. end
  74.  
  75. deleteBlock = function(x,y)
  76.     for a = 1, #blockMap do
  77.         if blockMap[a].x == x and blockMap[a].y == y then
  78.             table.remove(blockMap,a)
  79.             break
  80.         end
  81.     end
  82. end
  83.  
  84. scrollX,scrollY = 0,0
  85.  
  86. makeMap = function(preset)
  87.     preset = preset or 1
  88.     local output = {}
  89.     presets = {
  90.         [1] = function()
  91.             solidMap = {{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0},{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0},{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0},{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},{0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1},{0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,1,1,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0}}
  92.             backMap = {{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,1,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,1,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0},{0,1,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,1,1,1,1,0,0},{0,1,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,1,1,0},{0,1,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,32,32,32,32,32,32,1,1},{0,1,1,1,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,1},{0,0,0,1,256,256,256,256,256,256,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,1},{0,256,0,1,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,1},{0,256,0,1,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,1,1,1,256,256,256,256,256,256,256,256,256,64,64,1},{0,256,0,1,1,1,1,1,1,256,256,256,256,256,256,256,256,256,256,256,256,256,256,1,1,1,0,1,1,256,256,256,256,256,256,256,256,256,256,1},{0,256,0,0,0,0,0,0,1,1,1,1,128,128,128,128,256,256,256,256,256,1,1,1,0,0,0,1,256,256,256,256,256,256,256,256,256,256,256,1},{0,256,0,256,0,256,0,0,0,0,0,1,1,1,1,128,128,128,128,1,1,1,0,0,0,0,1,1,256,256,256,256,256,256,256,256,256,256,256,1},{0,256,0,256,0,256,0,256,0,256,0,0,0,0,1,1,1,1,1,0,0,0,0,256,0,0,1,128,128,128,128,128,128,128,256,256,256,256,1,1},{0,256,0,256,0,256,0,256,0,256,0,256,0,0,0,0,0,0,0,0,0,256,0,256,0,0,1,1,128,128,128,128,128,128,128,1,1,1,1,0},{0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,0,0,1,1,1,1,128,128,1,1,1,0,0,0,0},{0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,0,0,0,1,1,1,1,0,0,0,256,0,256},{0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,0,0,0,0,0,0,256,0,256,0,256},{0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256,0,256}}
  93.         end
  94.     }
  95.     presets[preset]()
  96. end
  97. makeMap(1)
  98.  
  99. P = 1 --player one, so far...
  100. pys = { --p' yourself
  101.     makePlayer("Lapi'wun",3,3,colors.blue,1),
  102.     makePlayer("Anpi'tew",5,3,colors.orange,2),
  103. }
  104.  
  105. renderMap = function()
  106.     chVis(false)
  107.     term.setBackgroundColor(colors.black)
  108.     term.clear()
  109.     paintutils.drawImage(backMap,(-scrollX)+1,(-scrollY)+1)
  110.     for k,v in pairs(blockMap) do
  111.         if (v.x-scrollX >= 1) and (v.x-scrollX <= scr_x) and (v.y-scrollY >= 1) and (v.y-scrollY <= scr_y) then
  112.             term.setCursorPos(v.x-scrollX,v.y-scrollY)
  113.             term.setBackgroundColor(v.bg)
  114.             term.setTextColor(v.txt)
  115.             term.write("%")
  116.         end
  117.     end
  118.     for k,v in pairs(bulletMap) do
  119.         if (v.x-scrollX >= 1) and (v.x-scrollX <= scr_x) and (v.y-scrollY >= 1) and (v.y-scrollY <= scr_y) then
  120.             term.setCursorPos(v.x-scrollX,v.y-scrollY)
  121.             local beegee = backMap[math.floor(v.y)][math.floor(v.x)]
  122.             if beegee == 0 then beegee = colors.black end
  123.             term.setBackgroundColor(beegee or colors.pink)
  124.             term.setTextColor(colors.white)
  125.             term.write(v.char)
  126.         end
  127.     end
  128.     for k,v in pairs(pys) do
  129.         if (v.x-scrollX >= 1) and (v.x-scrollX <= scr_x) and (v.y-scrollY >= 1) and (v.y-scrollY <= scr_y) then
  130.             term.setCursorPos(v.x-scrollX,v.y-scrollY)
  131.             term.setBackgroundColor(v.color or colors.white)
  132.             term.setTextColor(colors.black)
  133.             term.write(v.number)
  134.         end
  135.     end
  136.     chVis(true)
  137. end
  138.  
  139. getInput = function()
  140.     while true do
  141.         local evt = {os.pullEvent()}
  142.         if evt[1] == "key" then
  143.             keysDown[evt[2]] = true
  144.         elseif evt[1] == "key_up" then
  145.             keysDown[evt[2]] = false
  146.         elseif evt[1] == "mouse_click" then
  147.             miceDown[evt[2]] = true
  148.         elseif evt[1] == "mouse_up" then
  149.             miceDown[evt[2]] = false
  150.         end
  151.     end
  152. end
  153.  
  154. between = function(num,min,max)
  155.     if num > max then
  156.         return max
  157.     elseif num < min then
  158.         return min
  159.     else
  160.         return num
  161.     end
  162. end
  163.  
  164. checkIfInMap = function(x,y)
  165.     if solidMap[y] then
  166.         if (solidMap[y][x]) and (solidMap[y][x] ~= 0) then
  167.             return true
  168.         end
  169.     end
  170.     return false
  171. end
  172.  
  173. checkIfInPlayer = function(x,y,exceptionP) --exceptionP is so that bullets aren't stopped by those who shoot them
  174.     for k,v in pairs(pys) do
  175.         if v.x == x and v.y == y and v.number ~= (exceptionP or 0) then
  176.             return true
  177.         end
  178.     end
  179.     return false
  180. end
  181.  
  182. checkIfInBlock = function(x,y)
  183.     for k,v in pairs(blockMap) do
  184.         if v.x == x and v.y == y then
  185.             return true
  186.         end
  187.     end
  188.     return false
  189. end
  190.  
  191. doMovementTick = function()
  192.     for a = #bulletMap, 1, -1 do
  193.         for b = 1, math.max(math.abs(bulletMap[a].xspeed),math.abs(bulletMap[a].yspeed)) do
  194.             if checkIfInMap(bulletMap[a].x,bulletMap[a].y) or checkIfInPlayer(bulletMap[a].x,bulletMap[a].y,P) or checkIfInBlock(bulletMap[a].x,bulletMap[a].y) then
  195.                 if checkIfInBlock(bulletMap[a].x,bulletMap[a].y) then
  196.                     deleteBlock(bulletMap[a].x,bulletMap[a].y)
  197.                 end
  198.                 table.remove(bulletMap,a)
  199.                 break
  200.             else
  201.                 bulletMap[a].x = bulletMap[a].x + between(bulletMap[a].xspeed, -1, 1)
  202.                 bulletMap[a].y = bulletMap[a].y + between(bulletMap[a].yspeed, -1, 1)
  203.             end
  204.         end
  205.     end
  206.     --manual camera controls
  207.     if keysDown[keys.t] then
  208.         scrollY = scrollY - 1
  209.     elseif keysDown[keys.g] then
  210.         scrollY = scrollY + 1
  211.     end
  212.     if keysDown[keys.f] then
  213.         scrollX = scrollX - 1
  214.     elseif keysDown[keys.h] then
  215.         scrollX = scrollX + 1
  216.     end
  217.    
  218.     --player movement
  219.     if keysDown[control.UP] then
  220.         if not checkIfInMap(pys[P].x, pys[P].y - 1) then
  221.             pys[P].y = pys[P].y - 1
  222.         end
  223.     end
  224.     if keysDown[control.DOWN] then
  225.         if not checkIfInMap(pys[P].x, pys[P].y + 1) then
  226.             pys[P].y = pys[P].y + 1
  227.         end
  228.     end
  229.     if keysDown[control.LEFT] then
  230.         if not checkIfInMap(pys[P].x - 1, pys[P].y) then
  231.             pys[P].x = pys[P].x - 1
  232.         end
  233.     end
  234.     if keysDown[control.RIGHT] then
  235.         if not checkIfInMap(pys[P].x + 1, pys[P].y) then
  236.             pys[P].x = pys[P].x + 1
  237.         end
  238.     end
  239.     if keysDown[control.BLOCKPUT] then
  240.         if not checkIfInBlock(pys[P].x,pys[P].y) then
  241.             placeBlock(pys[P].x,pys[P].y)
  242.         end
  243.     end
  244.    
  245.     --making projectiles
  246.     if pys[P].cooldown > 0 then
  247.         pys[P].cooldown = pys[P].cooldown - 1
  248.         if pys[P].cooldown < 0 then pys[P].cooldown = 0 end
  249.     elseif keysDown[control.SHOOTUP] or keysDown[control.SHOOTDOWN] or keysDown[control.SHOOTLEFT] or keysDown[control.SHOOTRIGHT] then
  250.         pys[P].cooldown = 2
  251.         local xspeed,yspeed
  252.         local speedMod = 2
  253.         if keysDown[control.SHOOTUP] then
  254.             yspeed = -1
  255.         elseif keysDown[control.SHOOTDOWN] then
  256.             yspeed = 1
  257.         else
  258.             yspeed = 0
  259.         end
  260.         if keysDown[control.SHOOTLEFT] then
  261.             xspeed = -1
  262.         elseif keysDown[control.SHOOTRIGHT] then
  263.             xspeed = 1
  264.         else
  265.             xspeed = 0
  266.         end
  267.         xspeed = xspeed * speedMod
  268.         yspeed = yspeed * speedMod
  269.         makeBullet(pys[P].x,pys[P].y,xspeed,yspeed,"o")
  270.     end
  271. end
  272.  
  273. doIt = function()
  274.     while true do
  275.         doMovementTick()
  276.         doNetMovement(p)
  277.         renderMap()
  278.         sleep(0)
  279.     end
  280. end
  281.  
  282. local companionID
  283.  
  284. getNetInput = function()
  285.     while true do
  286.         local event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
  287.         if frequency == channel and replyFrequency == channel and type(message) == "table" then
  288.             --if message[1] == companionID and type(message[2]) == "table" then
  289.                 netKeysDown = message[2] or {}
  290.             --end
  291.         end
  292.     end
  293. end
  294.  
  295. doNetMovement = function(P)
  296.     --player movement
  297.     if netKeysDown[control.UP] then
  298.         if not checkIfInMap(pys[P].x, pys[P].y - 1) then
  299.             pys[P].y = pys[P].y - 1
  300.         end
  301.     end
  302.     if netKeysDown[control.DOWN] then
  303.         if not checkIfInMap(pys[P].x, pys[P].y + 1) then
  304.             pys[P].y = pys[P].y + 1
  305.         end
  306.     end
  307.     if netKeysDown[control.LEFT] then
  308.         if not checkIfInMap(pys[P].x - 1, pys[P].y) then
  309.             pys[P].x = pys[P].x - 1
  310.         end
  311.     end
  312.     if netKeysDown[control.RIGHT] then
  313.         if not checkIfInMap(pys[P].x + 1, pys[P].y) then
  314.             pys[P].x = pys[P].x + 1
  315.         end
  316.     end
  317.     if netKeysDown[control.BLOCKPUT] then
  318.         if not checkIfInBlock(pys[P].x,pys[P].y) then
  319.             placeBlock(pys[P].x,pys[P].y)
  320.         end
  321.     end
  322.        
  323.     --making projectiles
  324.     if pys[P].cooldown > 0 then
  325.         pys[P].cooldown = pys[P].cooldown - 1
  326.         if pys[P].cooldown < 0 then pys[P].cooldown = 0 end
  327.     elseif netKeysDown[control.SHOOTUP] or netKeysDown[control.SHOOTDOWN] or netKeysDown[control.SHOOTLEFT] or netKeysDown[control.SHOOTRIGHT] then
  328.         pys[P].cooldown = 2
  329.         local xspeed,yspeed
  330.         local speedMod = 2
  331.         if netKeysDown[control.SHOOTUP] then
  332.             yspeed = -1
  333.         elseif netKeysDown[control.SHOOTDOWN] then
  334.             yspeed = 1
  335.         else
  336.             yspeed = 0
  337.         end
  338.         if netKeysDown[control.SHOOTLEFT] then
  339.             xspeed = -1
  340.         elseif netKeysDown[control.SHOOTRIGHT] then
  341.             xspeed = 1
  342.         else
  343.             xspeed = 0
  344.         end
  345.         xspeed = xspeed * speedMod
  346.         yspeed = yspeed * speedMod
  347.         makeBullet(pys[P].x,pys[P].y,xspeed,yspeed,"o")
  348.     end
  349. end
  350.    
  351. sendNetOutput = function()
  352.     while true do
  353.         modem.transmit(channel,channel,{os.getComputerID(),keysDown})
  354.         os.queueEvent("yield")
  355.         os.pullEvent("yield")
  356.     end
  357. end
  358.  
  359. term.clear()
  360. term.setCursorPos(1,1)
  361. local ctime = os.time()
  362. modem.transmit(channel,channel,{"JOIN",os.getComputerID(),ctime})
  363. --[[term.write("Waiting for player...")
  364. local event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
  365. while true do
  366.     event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
  367.     if frequency == channel and replyFrequency == channel then
  368.         if type(message) == "table" then
  369.             if message == "JOIN" and type(message[2]) == "number" and type(message[3]) == "number" then
  370.                 if message[3] > ctime then
  371.                     P = 1
  372.                 else
  373.                     P = 2
  374.                 end
  375.                 companionID = message[2]
  376.                 break
  377.             end
  378.         end
  379.     end
  380. end
  381. --]]
  382. print("player:")
  383. P = tonumber(read())
  384.  
  385. if P == 1 then p = 2 else p = 1 end
  386.  
  387. parallel.waitForAny(doIt,getInput,sendNetOutput, getNetInput)
Add Comment
Please, Sign In to add comment