Advertisement
Guest User

Untitled

a guest
Apr 4th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.62 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"})
  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.                 end
  82.                
  83.                 local newtable = {}
  84.                 for k = 1, i*5 do
  85.                     table.insert(newtable, self.lasertable[k])
  86.                 end
  87.                
  88.                 newtable[i+3] = obj.x - newtable[i+1] + obj.width+1
  89.                
  90.                 self.lasertable = newtable
  91.                
  92.                 self:updateoutputs()
  93.                 break
  94.             end
  95.            
  96.         elseif self.lasertable[i] == "right" then
  97.            
  98.             x = self.lasertable[i+1]-1
  99.             y = self.lasertable[i+2]-0.5625
  100.             width = self.lasertable[i+3]+1
  101.             height = 2/16
  102.            
  103.             local rectcol = checkrect(x, y, width, height, {"player", "box", "goomba", "koopa"})
  104.            
  105.             if #rectcol > 0 then
  106.                 col = true
  107.                 self.blocked = true
  108.            
  109.                 local smallestx = mapwidth+1
  110.                 local smallesti
  111.            
  112.                 for j = 1, #rectcol, 2 do
  113.                     if objects[rectcol[j]][rectcol[j+1]].x < smallestx then
  114.                         smallestx = objects[rectcol[j]][rectcol[j+1]].x
  115.                         smallesti = j
  116.                     end
  117.                 end
  118.                
  119.                 obj = objects[rectcol[smallesti]][rectcol[smallesti+1]]
  120.                
  121.                 if obj.laser then
  122.                     obj:laser("left")
  123.                 end
  124.                
  125.                 local newtable = {}
  126.                 for k = 1, i*5 do
  127.                     table.insert(newtable, self.lasertable[k])
  128.                 end
  129.                
  130.                 newtable[i+3] = obj.x - newtable[i+1]
  131.                
  132.                 self.lasertable = newtable
  133.                
  134.                 self:updateoutputs()
  135.                 break
  136.             end
  137.            
  138.         elseif self.lasertable[i] == "up" then
  139.            
  140.             x = self.lasertable[i+1]-0.5625
  141.             y = self.lasertable[i+2]+self.lasertable[i+4]-1
  142.             width = 2/16
  143.             height = -self.lasertable[i+4]+1
  144.            
  145.             local rectcol = checkrect(x, y, width, height, {"player", "box", "goomba", "koopa"})
  146.            
  147.             if #rectcol > 0 then
  148.                 col = true
  149.                 self.blocked = true
  150.            
  151.                 local biggesty = 0
  152.                 local biggesti
  153.            
  154.                 for j = 1, #rectcol, 2 do
  155.                     if objects[rectcol[j]][rectcol[j+1]].y > biggesty then
  156.                         biggesty = objects[rectcol[j]][rectcol[j+1]].y
  157.                         biggesti = j
  158.                     end
  159.                 end
  160.                
  161.                 obj = objects[rectcol[biggesti]][rectcol[biggesti-1]]
  162.                
  163.  
  164.                
  165.                 local newtable = {}
  166.                 for k = 1, i*5 do
  167.                     table.insert(newtable, self.lasertable[k])
  168.                 end
  169.                
  170.                 newtable[i+4] = obj.y - newtable[i+2]+1
  171.                
  172.                 self.lasertable = newtable
  173.                
  174.                 self:updateoutputs()
  175.                 break
  176.             end
  177.            
  178.         elseif self.lasertable[i] == "down" then
  179.            
  180.             x = self.lasertable[i+1]-0.5625
  181.             y = self.lasertable[i+2]-1
  182.             width = 2/16
  183.             height = self.lasertable[i+4]+1
  184.            
  185.             local rectcol = checkrect(x, y, width, height, {"player", "box", "goomba", "koopa"})
  186.            
  187.             if #rectcol > 0 then
  188.                 col = true
  189.                 self.blocked = true
  190.            
  191.                 local smallesty = 16
  192.                 local smallesti
  193.            
  194.                 for j = 1, #rectcol, 2 do
  195.                     if objects[rectcol[j]][rectcol[j+1]].y < smallesty then
  196.                         smallesty = objects[rectcol[j]][rectcol[j+1]].y
  197.                         smallesti = j
  198.                     end
  199.                 end
  200.                
  201.                 obj = objects[rectcol[smallesti]][rectcol[smallesti+1]]
  202.                
  203.                 if obj.laser then
  204.                     obj:laser("up")
  205.                 end
  206.                
  207.                 local newtable = {}
  208.                 for k = 1, i*5 do
  209.                     table.insert(newtable, self.lasertable[k])
  210.                 end
  211.                
  212.                 newtable[i+4] = obj.y - newtable[i+2] + 1 - obj.height+1-1
  213.                
  214.                 self.lasertable = newtable
  215.                
  216.                 self:updateoutputs()
  217.                 break
  218.             end
  219.         end
  220.     end
  221.    
  222.     self:updateoutputs()
  223.    
  224.     if col == false and self.blocked == true then
  225.         self.blocked = false
  226.         self:updaterange()
  227.     end
  228. end
  229.  
  230. function laser:updateoutputs()
  231.     for i = 1, #self.lasertable, 5 do
  232.         if self.lasertable[i] == "left" then
  233.             for x = self.lasertable[i+1], math.ceil(self.lasertable[i+1]+self.lasertable[i+3])-1, -1 do
  234.                 local y = self.lasertable[i+2]
  235.                 self:checktile(x, y)
  236.             end
  237.         elseif self.lasertable[i] == "right" then
  238.             for x = self.lasertable[i+1], math.floor(self.lasertable[i+1]+self.lasertable[i+3])+1 do
  239.                 local y = self.lasertable[i+2]
  240.                 self:checktile(x, y)
  241.             end
  242.         elseif self.lasertable[i] == "up" then
  243.             for y = self.lasertable[i+2], self.lasertable[i+2]+self.lasertable[i+4]-1, -1 do
  244.                 local x = self.lasertable[i+1]
  245.                 self:checktile(x, y)
  246.             end
  247.         elseif self.lasertable[i] == "down" then
  248.             for y = self.lasertable[i+2], self.lasertable[i+2]+self.lasertable[i+4]+1 do
  249.                 local x = self.lasertable[i+1]
  250.                 self:checktile(x, y)
  251.             end
  252.         end
  253.     end
  254.    
  255.     for i, v in pairs(self.outtable) do
  256.         v:clear()
  257.     end
  258. end
  259.  
  260. function laser:checktile(x, y)
  261.     --check if block is a detector
  262.     for i, v in pairs(objects["laserdetector"]) do
  263.         if x == v.cox and y == v.coy and (v.cox ~= self.r[4] or v.coy ~= self.r[5]) then
  264.             table.insert(self.outtable, v)
  265.             v:input("on")
  266.         end
  267.     end
  268. end
  269.  
  270. function laser:draw()  
  271.     for i = 1, #self.lasertable, 5 do
  272.         if self.lasertable[i] == "left" then
  273.             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)
  274.             for x = self.lasertable[i+1], math.floor(self.lasertable[i+1]+self.lasertable[i+3])-1, -1 do
  275.                 love.graphics.draw(laserimg, math.floor((x-xscroll-1)*16*scale), (self.lasertable[i+2]-20/16)*16*scale, 0, scale, scale)
  276.             end
  277.         elseif self.lasertable[i] == "right" then
  278.             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)
  279.             for x = self.lasertable[i+1], math.ceil(self.lasertable[i+1]+self.lasertable[i+3])+1 do
  280.                 love.graphics.draw(laserimg, math.floor((x-xscroll-1)*16*scale), (self.lasertable[i+2]-20/16)*16*scale, 0, scale, scale)
  281.             end
  282.         elseif self.lasertable[i] == "up" then
  283.             for y = self.lasertable[i+2], self.lasertable[i+2]+self.lasertable[i+4], -1 do
  284.                 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)
  285.             end
  286.         elseif self.lasertable[i] == "down" then
  287.             for y = self.lasertable[i+2], self.lasertable[i+2]+self.lasertable[i+4] do
  288.                 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)
  289.             end
  290.         end
  291.        
  292.         love.graphics.setScissor()
  293.     end
  294.    
  295.     local rot = 0
  296.     if self.dir == "up" then
  297.         rot = math.pi*1.5
  298.     elseif self.dir == "down" then
  299.         rot = math.pi*0.5
  300.     elseif self.dir == "left" then
  301.         rot = math.pi
  302.     end
  303.  
  304.     love.graphics.draw(lasersideimg, math.floor((self.cox-xscroll-.5)*16*scale), (self.coy-1)*16*scale, rot, scale, scale, 8, 8)
  305. end
  306.  
  307. function laser:updaterange()
  308.     self.lasertable = {}
  309.     if self.enabled == false then
  310.         self:updateoutputs()
  311.         return
  312.     end
  313.    
  314.     local dir = self.dir
  315.     local startx, starty = self.cox, self.coy
  316.     local rangex, rangey = 0, 0
  317.     local x, y = self.cox, self.coy
  318.    
  319.     local firstcheck = true
  320.     local quit = false
  321.     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
  322.         firstcheck = false
  323.        
  324.         if dir == "right" then
  325.             x = x + 1
  326.             rangex = rangex + 1
  327.         elseif dir == "left" then
  328.             x = x - 1
  329.             rangex = rangex - 1
  330.         elseif dir == "up" then
  331.             y = y - 1
  332.             rangey = rangey - 1
  333.         elseif dir == "down" then
  334.             y = y + 1
  335.             rangey = rangey + 1
  336.         end
  337.        
  338.         --check if current block is a portal
  339.         local portalx, portaly, portalfacing, infacing = getPortal(x, y)
  340.        
  341.         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
  342.            
  343.             if dir == "right" then
  344.                 x = x - 1
  345.                 rangex = rangex - 1
  346.             elseif dir == "left" then
  347.                 x = x + 1
  348.                 rangex = rangex + 1
  349.             elseif dir == "up" then
  350.                 y = y + 1
  351.                 rangey = rangey + 1
  352.             elseif dir == "down" then
  353.                 y = y - 1
  354.                 rangey = rangey - 1
  355.             end
  356.            
  357.             table.insert(self.lasertable, dir)
  358.             table.insert(self.lasertable, x-rangex)
  359.             table.insert(self.lasertable, y-rangey)
  360.             table.insert(self.lasertable, rangex)
  361.             table.insert(self.lasertable, rangey)
  362.            
  363.             x, y = portalx, portaly
  364.             dir = portalfacing
  365.            
  366.             rangex, rangey = 0, 0
  367.            
  368.             if dir == "right" then
  369.                 x = portalx + 1
  370.             elseif dir == "left" then
  371.                 x = portalx - 1
  372.                 rangex = 0
  373.             elseif dir == "up" then
  374.                 y = portaly - 1
  375.             elseif dir == "down" then
  376.                 y = portaly + 1
  377.             end
  378.         end
  379.        
  380.         --doors
  381.         for i, v in pairs(objects["door"]) do
  382.             if v.active then
  383.                 if v.dir == "ver" then
  384.                     if x == v.cox and (y == v.coy or y == v.coy-1) then
  385.                         quit = true
  386.                     end
  387.                 elseif v.dir == "hor" then
  388.                     if y == v.coy and (x == v.cox or x == v.cox+1) then
  389.                         quit = true
  390.                     end
  391.                 end
  392.             end
  393.         end
  394.     end
  395.        
  396.     if dir == "right" then
  397.         x = x - 1
  398.         rangex = rangex - 1
  399.     elseif dir == "left" then
  400.         x = x + 1
  401.         rangex = rangex + 1
  402.     elseif dir == "up" then
  403.         y = y + 1
  404.         rangey = rangey + 1
  405.     elseif dir == "down" then
  406.         y = y - 1
  407.         rangey = rangey - 1
  408.     end
  409.    
  410.     table.insert(self.lasertable, dir)
  411.     table.insert(self.lasertable, x-rangex)
  412.     table.insert(self.lasertable, y-rangey)
  413.     table.insert(self.lasertable, rangex)
  414.     table.insert(self.lasertable, rangey)
  415.    
  416.     self:update()
  417. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement