Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local swiper={combo=0}
- local w,h=16,16
- local board={}
- 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)}
- colors[0]=Color(0,0,0)
- local difficulty=3
- function swiper.generateBoard()
- for x=1,w do
- for y=1,h do
- board[y]=board[y] or {}
- board[y][x]=0
- end
- end
- end
- function swiper.getBlock(x,y)
- return board[y][x]
- end
- function swiper.onLose()
- Derma_Message("Awe, you lost! Better luck next time!\nFinal Score:"..swiper.score..", Biggest combo: "..swiper.combo,"Awe!")
- swiper.playing=false
- end
- local function merge(a,b)
- local ret={}
- for k,v in pairs(a) do
- table.insert(ret,v)
- end
- for k,v in pairs(b) do
- table.insert(ret,v)
- end
- return ret
- end
- local function haspos(tbl,x,y)
- for k,v in pairs(tbl) do
- if(v.x==x and v.y==y) then return true end
- end
- end
- swiper.generateBoard()
- local vguiboard={}
- local dirs={{x=0,y=1},{x=1,y=0},{x=0,y=-1},{x=-1,y=0}}
- function swiper.getAround(xp,yp,ret)
- local ret=ret or {}
- local blockid=swiper.getBlock(xp,yp)
- table.insert(ret,{x=xp,y=yp})
- for k,v in pairs(dirs) do
- local x,y=v.x,v.y
- if(x+xp>0 and x+xp<=w and y+yp>0 and y+yp<=h and !haspos(ret,x+xp,y+yp)) then
- if(swiper.getBlock(xp+x,yp+y)==blockid) then
- merge(ret,swiper.getAround(xp+x,yp+y,ret))
- end
- end
- end
- return ret
- end
- function swiper.pop(x,y)
- if not swiper.playing then return end
- --board[y][x]=0
- --vguiboard[y][x].block=0
- local around=swiper.getAround(x,y)
- if(#around>1) then
- for k,v in pairs(around) do
- board[v.y][v.x]=0
- vguiboard[v.y][v.x].block=0
- end
- swiper.score=swiper.score+2*#around
- if(swiper.combo<#around) then swiper.combo=#around end
- swiper.setInfo("Score: "..swiper.score.." Biggest Combo: "..swiper.combo.." Row: "..swiper.rows)
- --surface.PlaySound("swiper/pop.wav")
- surface.PlaySound("ui/buttonclick.wav")
- timer.Simple(0,swiper.simulate)
- end
- end
- swiper.score=0
- swiper.rows=0
- function swiper.newRow()
- --determine the most empty row
- local row=1
- -- for y=1,h do
- -- local found=true
- -- for x=1,w do
- -- if(board[y][x]!=0) then
- -- found=false
- -- break
- -- end
- -- end
- -- if found then
- -- row=y
- -- break
- -- end
- -- end
- for x=1,w do
- if(board[row][x]!=0) then
- swiper.onLose()
- return
- end
- end
- for x=1,w do
- board[row][x]=math.random(1,difficulty)
- vguiboard[row][x].block=board[row][x]
- end
- surface.PlaySound("ui/buttonrollover.wav")
- swiper.simulate()
- swiper.rows=swiper.rows+1
- swiper.setInfo("Score: "..swiper.score.." Biggest Combo: "..swiper.combo.." Row: "..swiper.rows)
- end
- function swiper.drawBlock(self)
- local col=colors[self.block]
- surface.SetDrawColor(col)
- surface.DrawRect(0,0,32,32)
- surface.SetDrawColor(col.r/1.5,col.g/1.5,col.b/1.5)
- surface.DrawRect(8,8,16,16)
- end
- local frame
- function swiper.vguiUpdate()
- for y=1,h do
- for x=1,w do
- if(vguiboard[y] and vguiboard[y][x]) then vguiboard[y][x]:Remove() end
- local block=vgui.Create("DPanel",frame)
- function block:OnMousePressed()
- if(self.block!=0) then
- swiper.pop(self.blockx,self.blocky+1)
- end
- end
- block.block=0
- block.blockx=x
- block.blocky=y
- function block:Paint()
- swiper.drawBlock(block)
- end
- block:SetSize(32,32)
- block:SetPos((x-1)*32,(y-1)*32+23)
- vguiboard[y]=vguiboard[y] or {}
- vguiboard[y][x]=block
- end
- end
- end
- local zpos=0
- function swiper.simulate()
- for i=1,h do
- swiper.dosimulate()
- end
- end
- function swiper.dosimulate()
- for y=1,h do
- if(y==h) then break end
- for x=1,w do
- if(board[y][x]!=0 and board[y+1][x]==0) then
- board[y+1][x]=board[y][x]
- board[y][x]=0
- local topblock=vguiboard[y][x]
- local botblock=vguiboard[y+1][x]
- vguiboard[y][x]=botblock
- vguiboard[y+1][x]=topblock
- topblock.blockx=x
- topblock.blocky=y
- botblock.blockx=x
- botblock.blocky=y+1
- zpos=zpos+1
- topblock:SetZPos(zpos)
- topblock:MoveTo((x-1)*32,(y)*32+23,0.2)
- botblock:SetPos((x-1)*32,(y-1)*32+23)
- end
- end
- end
- --swiper.vguiUpdate()
- end
- function swiper.generateFrame()
- frame=vgui.Create("DFrame")
- frame:SetSize(w*32,h*32+23)
- frame:SetTitle("Swiper!")
- local info=vgui.Create("DLabel",frame)
- function swiper.setInfo(inf)
- info:SetText(inf)
- info:SizeToContents()
- end
- function frame:Paint()
- DFrame.Paint(self)
- surface.SetDrawColor(0,0,0)
- surface.DrawRect(0,22,self:GetSize())
- end
- info:SetPos(75,5)
- info:SetText("Score: 0 Biggest Combo: 0 Row: 0")
- info:SizeToContents()
- frame:MakePopup()
- swiper.vguiUpdate()
- end
- local delay=5
- local lastrow=0
- function swiper.think()
- if not swiper.playing then return end
- if(lastrow<CurTime()) then
- swiper.newRow()
- lastrow=CurTime()+delay
- delay=delay-0.09
- end
- end
- concommand.Add("swiper",function(_,_,args)
- w=tonumber(args[1]) or 16
- h=tonumber(args[2]) or 16
- difficulty=tonumber(args[3]) or 3
- swiper.playing=true
- swiper.generateFrame()
- end)
- hook.Add("Think","Swiper!",swiper.think)
Advertisement
Add Comment
Please, Sign In to add comment