Somepotato

Swiper

Jul 7th, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.01 KB | None | 0 0
  1. local swiper={combo=0}
  2. local w,h=16,16
  3. local board={}
  4. local colors={Color(235,75,75),Color(75,235,75),Color(75,75,235),Color(235,235,75),Color(75,235,235),Color(235,75,235)}
  5. colors[0]=Color(0,0,0)
  6. local difficulty=3
  7. function swiper.generateBoard()
  8.     for x=1,w do
  9.         for y=1,h do
  10.             board[y]=board[y] or {}
  11.             board[y][x]=0
  12.         end
  13.     end
  14. end
  15. function swiper.getBlock(x,y)
  16.     return board[y][x]
  17. end
  18. function swiper.onLose()
  19.     Derma_Message("Awe, you lost! Better luck next time!\nFinal Score:"..swiper.score..", Biggest combo: "..swiper.combo,"Awe!")
  20.     swiper.playing=false
  21. end
  22. local function merge(a,b)
  23.     local ret={}
  24.     for k,v in pairs(a) do
  25.         table.insert(ret,v)
  26.     end
  27.     for k,v in pairs(b) do
  28.         table.insert(ret,v)
  29.     end
  30.     return ret
  31. end
  32. local function haspos(tbl,x,y)
  33.     for k,v in pairs(tbl) do
  34.         if(v.x==x and v.y==y) then return true end
  35.     end
  36. end
  37. swiper.generateBoard()
  38. local vguiboard={}
  39. local dirs={{x=0,y=1},{x=1,y=0},{x=0,y=-1},{x=-1,y=0}}
  40. function swiper.getAround(xp,yp,ret)
  41.     local ret=ret or {}
  42.     local blockid=swiper.getBlock(xp,yp)
  43.     table.insert(ret,{x=xp,y=yp})
  44.     for k,v in pairs(dirs) do
  45.         local x,y=v.x,v.y
  46.         if(x+xp>0 and x+xp<=w and y+yp>0 and y+yp<=h and !haspos(ret,x+xp,y+yp)) then
  47.             if(swiper.getBlock(xp+x,yp+y)==blockid) then
  48.                 merge(ret,swiper.getAround(xp+x,yp+y,ret))
  49.             end
  50.         end
  51.     end
  52.     return ret
  53. end
  54. function swiper.pop(x,y)
  55.     if not swiper.playing then return end
  56.     --board[y][x]=0
  57.     --vguiboard[y][x].block=0
  58.     local around=swiper.getAround(x,y)
  59.     if(#around>1) then
  60.         for k,v in pairs(around) do
  61.             board[v.y][v.x]=0
  62.             vguiboard[v.y][v.x].block=0
  63.         end
  64.         swiper.score=swiper.score+2*#around
  65.         if(swiper.combo<#around) then swiper.combo=#around end
  66.         swiper.setInfo("Score: "..swiper.score.." Biggest Combo: "..swiper.combo.." Row: "..swiper.rows)
  67.        
  68.         --surface.PlaySound("swiper/pop.wav")
  69.         surface.PlaySound("ui/buttonclick.wav")
  70.         timer.Simple(0,swiper.simulate)
  71.     end
  72. end
  73. swiper.score=0
  74. swiper.rows=0
  75. function swiper.newRow()
  76.    
  77.     --determine the most empty row
  78.     local row=1
  79.     -- for y=1,h do
  80.         -- local found=true
  81.         -- for x=1,w do
  82.             -- if(board[y][x]!=0) then
  83.                 -- found=false
  84.                 -- break
  85.             -- end
  86.         -- end
  87.         -- if found then
  88.             -- row=y
  89.             -- break
  90.         -- end
  91.     -- end
  92.     for x=1,w do
  93.         if(board[row][x]!=0) then
  94.             swiper.onLose()
  95.             return
  96.         end
  97.     end
  98.     for x=1,w do
  99.         board[row][x]=math.random(1,difficulty)
  100.         vguiboard[row][x].block=board[row][x]
  101.     end
  102.     surface.PlaySound("ui/buttonrollover.wav")
  103.     swiper.simulate()
  104.     swiper.rows=swiper.rows+1
  105.     swiper.setInfo("Score: "..swiper.score.." Biggest Combo: "..swiper.combo.." Row: "..swiper.rows)
  106. end
  107.  
  108. function swiper.drawBlock(self)
  109.     local col=colors[self.block]
  110.     surface.SetDrawColor(col)
  111.     surface.DrawRect(0,0,32,32)
  112.     surface.SetDrawColor(col.r/1.5,col.g/1.5,col.b/1.5)
  113.     surface.DrawRect(8,8,16,16)
  114.    
  115. end
  116. local frame
  117. function swiper.vguiUpdate()
  118.     for y=1,h do
  119.         for x=1,w do
  120.             if(vguiboard[y] and vguiboard[y][x]) then vguiboard[y][x]:Remove() end
  121.             local block=vgui.Create("DPanel",frame)
  122.             function block:OnMousePressed()
  123.                 if(self.block!=0) then
  124.                     swiper.pop(self.blockx,self.blocky+1)
  125.                 end
  126.             end
  127.             block.block=0
  128.             block.blockx=x
  129.             block.blocky=y
  130.             function block:Paint()
  131.                 swiper.drawBlock(block)
  132.             end
  133.             block:SetSize(32,32)
  134.             block:SetPos((x-1)*32,(y-1)*32+23)
  135.            
  136.             vguiboard[y]=vguiboard[y] or {}
  137.             vguiboard[y][x]=block
  138.         end
  139.     end
  140. end
  141. local zpos=0
  142. function swiper.simulate()
  143.     for i=1,h do
  144.         swiper.dosimulate()
  145.     end
  146. end
  147. function swiper.dosimulate()
  148.     for y=1,h do
  149.         if(y==h) then break end
  150.         for x=1,w do
  151.             if(board[y][x]!=0 and board[y+1][x]==0) then
  152.                 board[y+1][x]=board[y][x]
  153.                 board[y][x]=0
  154.                 local topblock=vguiboard[y][x]
  155.                 local botblock=vguiboard[y+1][x]
  156.                 vguiboard[y][x]=botblock
  157.                 vguiboard[y+1][x]=topblock
  158.                 topblock.blockx=x
  159.                 topblock.blocky=y
  160.                 botblock.blockx=x
  161.                 botblock.blocky=y+1
  162.                
  163.                 zpos=zpos+1
  164.                 topblock:SetZPos(zpos)
  165.                 topblock:MoveTo((x-1)*32,(y)*32+23,0.2)
  166.                 botblock:SetPos((x-1)*32,(y-1)*32+23)
  167.                
  168.             end
  169.         end
  170.     end
  171.     --swiper.vguiUpdate()
  172. end
  173. function swiper.generateFrame()
  174.     frame=vgui.Create("DFrame")
  175.     frame:SetSize(w*32,h*32+23)
  176.     frame:SetTitle("Swiper!")
  177.     local info=vgui.Create("DLabel",frame)
  178.     function swiper.setInfo(inf)
  179.         info:SetText(inf)
  180.         info:SizeToContents()
  181.     end
  182.     function frame:Paint()
  183.         DFrame.Paint(self)
  184.         surface.SetDrawColor(0,0,0)
  185.         surface.DrawRect(0,22,self:GetSize())
  186.     end
  187.     info:SetPos(75,5)
  188.     info:SetText("Score: 0 Biggest Combo: 0 Row: 0")
  189.     info:SizeToContents()
  190.     frame:MakePopup()
  191.     swiper.vguiUpdate()
  192. end
  193. local delay=5
  194. local lastrow=0
  195. function swiper.think()
  196.     if not swiper.playing then return end
  197.    
  198.     if(lastrow<CurTime()) then
  199.         swiper.newRow()
  200.         lastrow=CurTime()+delay
  201.         delay=delay-0.09
  202.     end
  203. end
  204. concommand.Add("swiper",function(_,_,args)
  205.     w=tonumber(args[1]) or 16
  206.     h=tonumber(args[2]) or 16
  207.     difficulty=tonumber(args[3]) or 3
  208.     swiper.playing=true
  209.     swiper.generateFrame()
  210.    
  211. end)
  212. hook.Add("Think","Swiper!",swiper.think)
Advertisement
Add Comment
Please, Sign In to add comment