aboutsummaryrefslogtreecommitdiff
path: root/gamemode/hud/draw_spell.lua
blob: 96440e4b478b78cdc52a545bacea905992e51439 (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
CASTING_SPELL = nil

--AddLUACSFolder("spellminigames")

local Folder  	= GM.Folder:gsub("gamemodes/","").."/gamemode/hud/games"

--A table of spells to functions?
Spells = {
    ["Fireball"] = {
        ["file"] = "connectthedots.lua",
        ["minigame"] = "Cast",
        ["interupt"] = "AddDots",
        ["draw"] = "Draw",
        ["difficulty"] = 1,
    },
}

Games = {}

function loadgames()
    includedfiles = {}
    print("Looking for what minigames we need...")
    for k,v in pairs(Spells) do
        local filename = v["file"]
        if (includedfiles[filename] == nil) then
            print("Spell " .. k .. " depends on " .. filename)
            includedfiles[filename] = true
        end
    end
    Game = {}
    print("Loading in minigames")
    for k,v in pairs(includedfiles) do
        AddCSLuaFile(Folder .. "/" .. k)
        Game = {}
        include(Folder .. "/" .. k)
        Games[k] = Game
    end
    print("Resolveing minigame names")
    for k,v in pairs(Spells) do
        local mgname = v["minigame"]
        local mginame = v["interupt"]
        local mgdname = v["draw"]
        for i,j in pairs(Games) do
            if i == v["file"] then
                print("Resolved " .. k)
                local castfunc = j[mgname]
                local drawfunc = j[mgdname]
                local interuptfunc = j[mginame]
                if(castfunc) then
                    v["minigame"] = castfunc
                else
                    print("Spell " .. k .. " does not have a cast func! This may be an error!")
                end
                if(drawfunc) then
                    v["draw"] = drawfunc
                else
                    print("Spell " .. k .. " does not have a draw func! This may be an error!")
                end
                if(interuptfunc) then
                    v["interupt"] = interuptfunc
                else
                    print("Spell " .. k .. " does not have an interupt func! This may be an error!")
                end
            end
        end
    end
end

hook.Add("Initialize","Loadspells",function()
	loadgames()
end)

concommand.Add("reloadgames",function(ply,cmd,args)
    loadgames()
end)

function StartMinigame(spell)
    print("starting " .. spell)
    if not spell then return end
    --If we're already casting, and are told to cast again do someting.
    if CASTING_SPELL != nil then
        CASTING_SPELL["interupt"](CASTING_SPELL)
        return
    end

    if Spells[spell] then
        CASTING_SPELL = Spells[spell]
        Spells[spell]["minigame"](CASTING_SPELL, Spells[spell]["difficulty"])
    else
        print("Could not find spell " .. spell)
    end
end

function DrawSpellOverlay()
    if(CASTING_SPELL) then
        CASTING_SPELL["draw"](CASTING_SPELL)
        if(CASTING_SPELL.Score != nil) then
            finishedcasting(CASTING_SPELL.Score)
            CASTING_SPELL = nil
        end
    end
end

concommand.Add("startminigame",function(ply,cmd,args)
    print("Starting " .. args[1])
    StartMinigame(args[1])
end)