--The connet the dots spell game print("Hi from connectthedots.lua") Game = Game or {} --Minigame is held in self.dots, each dot is a table, --dot[num] = {dot x, dot y, { -- {armor_start_angle, armor_end_angle}, -- {armor_start_angle, armor_end_angle}, -- ... --}, activated} --And in self.lines, each line is held as --line[num] = {{start x, start y},{end x, end y}} Game.AddRegDot = nil Game.dots = nil Game.lines = nil Game.Cast = function(spelltab,difficulty) print("Cast with dificulty: " .. difficulty) spelltab.Score = nil --Create a dummy panel so that the mouse is trapped, untrap it when it right clicks local scoreboard = vgui.Create("MBFrame") if(!scoreboard) then return end scoreboard:SetPos(0,0) scoreboard:SetSize(0,0) scoreboard:MakePopup() spelltab.scoreboard = scoreboard --Create the minigame spelltab.dots = {} spelltab.lines = {} --Create the first dot in the middle of the screen spelltab.dots[0] = {} spelltab.dots[0][0] = ScrW()/2 spelltab.dots[0][1] = ScrH()/2 spelltab.dots[0][2] = {} --Create the first line from the first dot local fline = {} fline[0] = {} fline[1] = {} fline[0][0] = ScrW()/2 fline[0][1] = ScrH()/2 fline[1][0] = ScrW()/2 fline[1][1] = ScrH()/2 spelltab.lines[0] = fline --Add some dots! spelltab:AddRegDot(spelltab) spelltab:AddRegDot(spelltab) spelltab:AddRegDot(spelltab) end Game.Draw = function(self) if not input.IsKeyTrapping() then input.StartKeyTrapping() end draw.DrawText( "Connect the dots by moveing you mouse!", "TargetID", ScrW() * 0.5, ScrH() * 0.25, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER ) for k,v in pairs(self.dots) do local tcolor = Color(255,0,0,255) if(v[3]) then tcolor = Color(0,255,0,255) end surface.DrawCircle(v[0],v[1],10,tcolor) --If our mouse is touching dot, add a line that connects that dot, and set the dot to "activated" if(math.Distance(gui.MouseX(),gui.MouseY(),v[0],v[1]) < 10 and not v[3]) then v[3] = true local finishedline = self.lines[#self.lines] finishedline[1][0] = v[0] finishedline[1][1] = v[1] --Create a new line local newline = {} newline[0] = {} newline[1] = {} newline[0][0] = v[0] newline[0][1] = v[1] newline[1][0] = v[0] newline[1][1] = v[1] self.lines[#self.lines+1]=newline --Check the number of dots remaining, if there are none, we're done! local done = true for i,j in pairs(self.dots) do if(not j[3]) then done = false break end end if(done) then self.Score = 100 self.scoreboard:Remove() end end end --Find the last line in the table local lastl = self.lines[#self.lines] --and set it's end to our mouse's position lastl[1][0] = gui.MouseX() lastl[1][1] = gui.MouseY() for k,v in pairs(self.lines) do if(v == lastl) then surface.SetDrawColor(Color(255,0,0,255)) else surface.SetDrawColor(Color(0,255,0,255)) end surface.DrawLine(v[0][0],v[0][1],v[1][0],v[1][1]) end --print("X:" .. gui.MouseX() .. " Y:" .. gui.MouseY()) if(input.IsMouseDown(MOUSE_RIGHT)) then self.Score = 0 self.scoreboard:Remove() end end --A basic dot, can be accessed from anywhere. Game.AddRegDot = function(spelltab) if(spelltab.dots == nil) then print("After calling, self is") PrintTable(spelltab) print("We have no starting dots! help!") return end --Find the most recently added dot local fd = spelltab.dots[#spelltab.dots] local armors = {} for k,v in pairs(fd[2]) do armors[k] = v end --Since armor can have multiple segments, pick all open segments, and find the surface area they occupy. local totalsurface = 360 for k,v in pairs(armors) do print("Found armor between " .. v[1] .. " and " .. v[2]) totalsurface = totalsurface - (v[1]-v[2]) end print("Looks like there's " .. totalsurface .. " of avaliable angle!") --Pick a number between 0 and totalsurface that represents how much un-armored space is before us local position = math.random(totalsurface) --Find the angles that we will add outselves between for k,v in pairs(fd[2]) do if(position > v[1]) then position = position + v[2] - v[1] end end print("Adding new dot at an angle of " .. position) print("Adding a new dot after " .. fd[0] .. "," .. fd[1]) --Pick a random distance from this dot to the screen edge, and put a dot there, but at least 20 pixels local rand = 0 local totop = fd[1] + 20 local toleft = fd[0] + 20 local tobot = ScrH() - totop - 40 local toright = ScrW() - toleft - 40 --If we're in the first quadrent if(position > 0 and position <= 90) then --pick a distnce between us and the closer edge local closer = math.min(totop,toright) print("Closer edge is " .. closer) rand = math.random(20,closer) elseif(position > 90 and position <= 180) then local closer = math.min(totop,toleft) print("Closer edge is " .. closer) rand = math.random(20,closer) elseif(position > 180 and position <= 270) then local closer = math.min(toleft,tobot) print("Closer edge is " .. closer) rand = math.random(20,closer) else local closer = math.min(tobot,toright) print("Closer edge is " .. closer) rand = math.random(20,closer) end print("And at a distance of " .. rand) local cosmul = math.cos(math.rad(position)) --Remember that screens are funny, with 0,0 in the UPPER left hand corner, this tricked me up for the longest time... local sinmul = -math.sin(math.rad(position)) print("Cosine mul is " .. cosmul) print("Sine mul is " .. sinmul) local xpos = (cosmul*rand)+fd[0] local ypos = (sinmul*rand)+fd[1] print("I think I should add a dot at " .. xpos .. "," .. ypos) local newdot = {} newdot[0] = xpos newdot[1] = ypos newdot[2] = {} spelltab.dots[#spelltab.dots + 1] = newdot end Game.AddDots = function(self) print("Add dots function called!") end Game.InstantFail = function(self) self.Score = 0 end