aboutsummaryrefslogtreecommitdiff
path: root/gamemode/hud/games/connectthedots.lua
blob: 20cc6010907dea1e4572fbd64fd47fa5ce65f429 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
--The connet the dots spell game
print("Hi from connectthedots.lua")

--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