Advertisement
Guest User

Untitled

a guest
Aug 28th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.73 KB | None | 0 0
  1. laser = class:new()
  2.  
  3. function laser:init(x, y, dir, r)
  4.     self.cox = x
  5.     self.coy = y
  6.     self.dir = dir
  7.     self.r = r
  8.    
  9.     self.blocked = false
  10.     self.lasertable = {}
  11.     self.outtable = {}
  12.    
  13.     self.framestart = 0
  14.    
  15.     self.enabled = true
  16. end
  17.  
  18. function laser:link()
  19.     self.outtable = {}
  20.     if #self.r > 2 then
  21.         for j, w in pairs(outputs) do
  22.             for i, v in pairs(objects[w]) do
  23.                 if tonumber(self.r[4]) == v.cox and tonumber(self.r[5]) == v.coy then
  24.                     v:addoutput(self)
  25.                     self.enabled = false
  26.                 end
  27.             end
  28.         end
  29.     end
  30. end
  31.  
  32. function laser:input(t)
  33.     if t == "on" then
  34.         self.enabled = true
  35.         self:updaterange()
  36.     elseif t == "off" then
  37.         self.enabled = false
  38.         self:updaterange()
  39.     else
  40.         self.enabled = not self.enabled
  41.         self:updaterange()
  42.     end
  43. end
  44.  
  45. function laser:update(dt)
  46.     if self.framestart < 2 then
  47.         self.framestart = self.framestart + 1
  48.         return
  49.     end
  50.     local x, y, width, height
  51.     local col = false
  52.    
  53.     for i = 1, #self.lasertable, 5 do
  54.         if self.lasertable[i] == "left" then
  55.            
  56.             x = self.lasertable[i+1]-1+self.lasertable[i+3]
  57.             y = self.lasertable[i+2]-0.5625
  58.             width = -self.lasertable[i+3]+1
  59.             height = 2/16
  60.            
  61.             local rectcol = checkrect(x, y, width, height, {"player", "box", "goomba", "koopa", "laserbox"})
  62.            
  63.             if #rectcol > 0 then
  64.                 col = true
  65.                 self.blocked = true
  66.            
  67.                 local biggestx = -1
  68.                 local biggesti
  69.            
  70.                 for j = 1, #rectcol, 2 do
  71.                     if objects[rectcol[j]][rectcol[j+1]].x > biggestx then
  72.                         biggestx = objects[rectcol[j]][rectcol[j+1]].x
  73.                         biggesti = j
  74.                     end
  75.                 end
  76.                
  77.                 obj = objects[rectcol[biggesti]][rectcol[biggesti+1]]
  78.                
  79.                 if obj.laser then
  80.                     obj:laser("right")
  81.                 elseif obj.laserbox then
  82.                     obj:laser("up")
  83.                 end
  84.                
  85.                 local newtable = {}
  86.                 for k = 1, i*5 do
  87.                     table.insert(newtable, self.lasertable[k])
  88.                 end
  89.                
  90.                 newtable[i+3] = obj.x - newtable[i+1] + obj.width+1
  91.                
  92.                 self.lasertable = newtable
  93.                
  94.                 self:updateoutputs()
  95.                 break
  96.             end
  97.            
  98.         elseif self.lasertable[i] == "right" then
  99.            
  100.             x = self.lasertable[i+1]-1
  101.             y = self.lasertable[i+2]-0.5625
  102.             width = self.lasertable[i+3]+1
  103.             height = 2/16
  104.            
  105.             local rectcol = checkrect(x, y, width, height, {"player", "box", "goomba", "koopa", "laserbox"})
  106.            
  107.             if #rectcol > 0 then
  108.                 col = true
  109.                 self.blocked = true
  110.            
  111.                 local smallestx = mapwidth+1
  112.                 local smallesti
  113.            
  114.                 for j = 1, #rectcol, 2 do
  115.                     if objects[rectcol[j]][rectcol[j+1]].x < smallestx then
  116.                         smallestx = objects[rectcol[j]][rectcol[j+1]].x
  117.                         smallesti = j
  118.                     end
  119.                 end
  120.                
  121.                 obj = objects[rectcol[smallesti]][rectcol[smallesti+1]]
  122.                
  123.                 if obj.laser then
  124.                     obj:laser("left")
  125.                 elseif obj.laserbox then
  126.                     obj:laser("up")
  127.                 end
  128.                
  129.                 local newtable = {}
  130.                 for k = 1, i*5 do
  131.                     table.insert(newtable, self.lasertable[k])
  132.                 end
  133.                
  134.                 newtable[i+3] = obj.x - newtable[i+1]
  135.                
  136.                 self.lasertable = newtable
  137.                
  138.                 self:updateoutputs()
  139.                 break
  140.             end
  141.            
  142.         elseif self.lasertable[i] == "up" then
  143.            
  144.             x = self.lasertable[i+1]-0.5625
  145.             y = self.lasertable[i+2]+self.lasertable[i+4]-1
  146.             width = 2/16
  147.             height = -self.lasertable[i+4]+1
  148.            
  149.             local rectcol = checkrect(x, y, width, height, {"player", "box", "goomba", "koopa", "laserbox"})
  150.            
  151.             if #rectcol > 0 then
  152.                 col = true
  153.                 self.blocked = true
  154.            
  155.                 local biggesty = 0
  156.                 local biggesti
  157.            
  158.                 for j = 1, #rectcol, 2 do
  159.                     if objects[rectcol[j]][rectcol[j+1]].y > biggesty then
  160.                         biggesty = objects[rectcol[j]][rectcol[j+1]].y
  161.                         biggesti = j
  162.                     end
  163.                 end
  164.                
  165.                 obj = objects[rectcol[biggesti]][rectcol[biggesti+1]]
  166.                
  167.                 if obj.laser then
  168.                     obj:laser("down")
  169.                 elseif obj.laserbox then
  170.                     if objects["player"][i].x > objects["laserbox"][i].x then
  171.                         obj:laser("left")
  172.                     elseif objects["player"][i].x < objects["laserbox"][i].x then
  173.                         obj:laser("right")
  174.                     elseif objects["player"][i].x == objects["laserbox"][i].x then
  175.                         if objects["player"][i].pointingangle < 0 then
  176.                         obj:laser("left")
  177.                         elseif objects["player"][i].pointingangle > 0 then
  178.                         obj:laser("right")
  179.                         end
  180.                     end    
  181.                 end
  182.                
  183.                 local newtable = {}
  184.                 for k = 1, i*5 do
  185.                     table.insert(newtable, self.lasertable[k])
  186.                 end
  187.                
  188.                 newtable[i+4] = obj.y - newtable[i+2]+1
  189.                
  190.                 self.lasertable = newtable
  191.                
  192.                 self:updateoutputs()
  193.                 break
  194.             end
  195.            
  196.         elseif self.lasertable[i] == "down" then
  197.            
  198.             x = self.lasertable[i+1]-0.5625
  199.             y = self.lasertable[i+2]-1
  200.             width = 2/16
  201.             height = self.lasertable[i+4]+1
  202.            
  203.             local rectcol = checkrect(x, y, width, height, {"player", "box", "goomba", "koopa", "laserbox"})
  204.            
  205.             if #rectcol > 0 then
  206.                 col = true
  207.                 self.blocked = true
  208.            
  209.                 local smallesty = 16
  210.                 local smallesti
  211.            
  212.                 for j = 1, #rectcol, 2 do
  213.                     if objects[rectcol[j]][rectcol[j+1]].y < smallesty then
  214.                         smallesty = objects[rectcol[j]][rectcol[j+1]].y
  215.                         smallesti = j
  216.                     end
  217.                 end
  218.                
  219.                 obj = objects[rectcol[smallesti]][rectcol[smallesti+1]]
  220.                
  221.                 if obj.laser then
  222.                     obj:laser("up")
  223.                 elseif obj.laserbox then
  224.                     if objects["player"][i].x > objects["laserbox"][i].x then
  225.                         obj:laser("left")
  226.                     elseif objects["player"][i].x < objects["laserbox"][i].x then
  227.                         obj:laser("right")
  228.                     elseif objects["player"][i].x == objects["laserbox"][i].x then
  229.                         if objects["player"][i].pointingangle < 0 then
  230.                         obj:laser("left")
  231.                         elseif objects["player"][i].pointingangle > 0 then
  232.                         obj:laser("right")
  233.                         end
  234.                     end
  235.                 end
  236.                
  237.                 local newtable = {}
  238.                 for k = 1, i*5 do
  239.                     table.insert(newtable, self.lasertable[k])
  240.                 end
  241.                
  242.                 newtable[i+4] = obj.y - newtable[i+2] + 1 - obj.height+1-1
  243.                
  244.                 self.lasertable = newtable
  245.                
  246.                 self:updateoutputs()
  247.                 break
  248.             end
  249.         end
  250.     end
  251.    
  252.     self:updateoutputs()
  253.    
  254.     if col == false and self.blocked == true then
  255.         self.blocked = false
  256.         self:updaterange()
  257.     end
  258. end
  259.  
  260. function laser:updateoutputs()
  261.     for i = 1, #self.lasertable, 5 do
  262.         if self.lasertable[i] == "left" then
  263.             for x = self.lasertable[i+1], math.ceil(self.lasertable[i+1]+self.lasertable[i+3])-1, -1 do
  264.                 local y = self.lasertable[i+2]
  265.                 self:checktile(x, y)
  266.             end
  267.         elseif self.lasertable[i] == "right" then
  268.             for x = self.lasertable[i+1], math.floor(self.lasertable[i+1]+self.lasertable[i+3])+1 do
  269.                 local y = self.lasertable[i+2]
  270.                 self:checktile(x, y)
  271.             end
  272.         elseif self.lasertable[i] == "up" then
  273.             for y = self.lasertable[i+2], self.lasertable[i+2]+self.lasertable[i+4]-1, -1 do
  274.                 local x = self.lasertable[i+1]
  275.                 self:checktile(x, y)
  276.             end
  277.         elseif self.lasertable[i] == "down" then
  278.             for y = self.lasertable[i+2], self.lasertable[i+2]+self.lasertable[i+4]+1 do
  279.                 local x = self.lasertable[i+1]
  280.                 self:checktile(x, y)
  281.             end
  282.         end
  283.     end
  284.    
  285.     for i, v in pairs(self.outtable) do
  286.         v:clear()
  287.     end
  288. end
  289.  
  290. function laser:checktile(x, y)
  291.     --check if block is a detector
  292.     for i, v in pairs(objects["laserdetector"]) do
  293.         if x == v.cox and y == v.coy and (v.cox ~= self.r[4] or v.coy ~= self.r[5]) then
  294.             table.insert(self.outtable, v)
  295.             v:input("on")
  296.         end
  297.     end
  298. end
  299.  
  300. function laser:draw()  
  301.     for i = 1, #self.lasertable, 5 do
  302.         if self.lasertable[i] == "left" then
  303.             love.graphics.setScissor(math.floor((self.lasertable[i+1]+self.lasertable[i+3]-xscroll-1)*16*scale), (self.lasertable[i+2]-1.5)*16*scale, math.max((-self.lasertable[i+3]+1)*16*scale, 0), 16*scale)
  304.             for x = self.lasertable[i+1], math.floor(self.lasertable[i+1]+self.lasertable[i+3])-1, -1 do
  305.                 love.graphics.draw(laserimg, math.floor((x-xscroll-1)*16*scale), (self.lasertable[i+2]-20/16)*16*scale, 0, scale, scale)
  306.             end
  307.         elseif self.lasertable[i] == "right" then
  308.             love.graphics.setScissor(math.floor((self.lasertable[i+1]-xscroll-1)*16*scale), (self.lasertable[i+2]-1.5)*16*scale, math.max((self.lasertable[i+3]+1)*16*scale, 0), 16*scale)
  309.             for x = self.lasertable[i+1], math.ceil(self.lasertable[i+1]+self.lasertable[i+3])+1 do
  310.                 love.graphics.draw(laserimg, math.floor((x-xscroll-1)*16*scale), (self.lasertable[i+2]-20/16)*16*scale, 0, scale, scale)
  311.             end
  312.         elseif self.lasertable[i] == "up" then
  313.             for y = self.lasertable[i+2], self.lasertable[i+2]+self.lasertable[i+4], -1 do
  314.                 love.graphics.draw(laserimg, math.floor((self.lasertable[i+1]-xscroll-5/16)*16*scale), (y-1)*16*scale, math.pi/2, scale, scale, 8, 1)
  315.             end
  316.         elseif self.lasertable[i] == "down" then
  317.             for y = self.lasertable[i+2], self.lasertable[i+2]+self.lasertable[i+4] do
  318.                 love.graphics.draw(laserimg, math.floor((self.lasertable[i+1]-xscroll-5/16)*16*scale), (y-1)*16*scale, math.pi/2, scale, scale, 8, 1)
  319.             end
  320.         end
  321.        
  322.         love.graphics.setScissor()
  323.     end
  324.    
  325.     local rot = 0
  326.     if self.dir == "up" then
  327.         rot = math.pi*1.5
  328.     elseif self.dir == "down" then
  329.         rot = math.pi*0.5
  330.     elseif self.dir == "left" then
  331.         rot = math.pi
  332.     end
  333.  
  334.     love.graphics.draw(lasersideimg, math.floor((self.cox-xscroll-.5)*16*scale), (self.coy-1)*16*scale, rot, scale, scale, 8, 8)
  335. end
  336.  
  337. function laser:updaterange()
  338.     self.lasertable = {}
  339.     if self.enabled == false then
  340.         self:updateoutputs()
  341.         return
  342.     end
  343.    
  344.     local dir = self.dir
  345.     local startx, starty = self.cox, self.coy
  346.     local rangex, rangey = 0, 0
  347.     local x, y = self.cox, self.coy
  348.    
  349.     local firstcheck = true
  350.     local quit = false
  351.     while x >= 1 and x <= mapwidth and y >= 1 and y <= 15 and tilequads[map[x][y][1]].collision == false and (x ~= startx or y ~= starty or dir ~= self.dir or firstcheck == true) and quit == false do
  352.         firstcheck = false
  353.        
  354.         if dir == "right" then
  355.             x = x + 1
  356.             rangex = rangex + 1
  357.         elseif dir == "left" then
  358.             x = x - 1
  359.             rangex = rangex - 1
  360.         elseif dir == "up" then
  361.             y = y - 1
  362.             rangey = rangey - 1
  363.         elseif dir == "down" then
  364.             y = y + 1
  365.             rangey = rangey + 1
  366.         end
  367.        
  368.         --check if current block is a portal
  369.         local portalx, portaly, portalfacing, infacing = getPortal(x, y)
  370.        
  371.         if portalx ~= false and ((dir == "left" and infacing == "right") or (dir == "right" and infacing == "left") or (dir == "up" and infacing == "down") or (dir == "down" and infacing == "up")) then
  372.            
  373.             if dir == "right" then
  374.                 x = x - 1
  375.                 rangex = rangex - 1
  376.             elseif dir == "left" then
  377.                 x = x + 1
  378.                 rangex = rangex + 1
  379.             elseif dir == "up" then
  380.                 y = y + 1
  381.                 rangey = rangey + 1
  382.             elseif dir == "down" then
  383.                 y = y - 1
  384.                 rangey = rangey - 1
  385.             end
  386.            
  387.             table.insert(self.lasertable, dir)
  388.             table.insert(self.lasertable, x-rangex)
  389.             table.insert(self.lasertable, y-rangey)
  390.             table.insert(self.lasertable, rangex)
  391.             table.insert(self.lasertable, rangey)
  392.            
  393.             x, y = portalx, portaly
  394.             dir = portalfacing
  395.            
  396.             rangex, rangey = 0, 0
  397.            
  398.             if dir == "right" then
  399.                 x = portalx + 1
  400.             elseif dir == "left" then
  401.                 x = portalx - 1
  402.                 rangex = 0
  403.             elseif dir == "up" then
  404.                 y = portaly - 1
  405.             elseif dir == "down" then
  406.                 y = portaly + 1
  407.             end
  408.         end
  409.        
  410.         --doors
  411.         for i, v in pairs(objects["door"]) do
  412.             if v.active then
  413.                 if v.dir == "ver" then
  414.                     if x == v.cox and (y == v.coy or y == v.coy-1) then
  415.                         quit = true
  416.                     end
  417.                 elseif v.dir == "hor" then
  418.                     if y == v.coy and (x == v.cox or x == v.cox+1) then
  419.                         quit = true
  420.                     end
  421.                 end
  422.             end
  423.         end
  424.     end
  425.        
  426.     if dir == "right" then
  427.         x = x - 1
  428.         rangex = rangex - 1
  429.     elseif dir == "left" then
  430.         x = x + 1
  431.         rangex = rangex + 1
  432.     elseif dir == "up" then
  433.         y = y + 1
  434.         rangey = rangey + 1
  435.     elseif dir == "down" then
  436.         y = y - 1
  437.         rangey = rangey - 1
  438.     end
  439.    
  440.     table.insert(self.lasertable, dir)
  441.     table.insert(self.lasertable, x-rangex)
  442.     table.insert(self.lasertable, y-rangey)
  443.     table.insert(self.lasertable, rangex)
  444.     table.insert(self.lasertable, rangey)
  445.    
  446.     self:update()
  447. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement