aboutsummaryrefslogtreecommitdiff
path: root/gamemode/shared/player_spellcast.lua
blob: 20a6cb554b7f59647a7610363a052f0e5692493f (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
local meta = FindMetaTable("Player")

--Number of bits used for networking spell casting success rate. Even though by default it shouldn't be more than 100, 8 lets it go to 256 in case you want to add buffs/bonuses.
local BITCOUNT = 256

if(SERVER) then
    util.AddNetworkString("CastSpell")
    util.AddNetworkString("FinishedCasting")

    function meta:Cast(spellname, callback, weapon)
        if(self.casting) then return end
        self.casting = callback
        self.wep = weapon
        print("Casting " .. spellname)
        net.Start("CastSpell")
			net.WriteString(spellname)
		net.Send(self)
    end

    net.Receive("FinishedCasting",function(len,pl)
        if(pl.casting) then
            print("Finished casting message received by server: " .. pl:Nick())
            local score = net.ReadInt(BITCOUNT)
            print("Calling callback with " .. score .. " and " .. pl:Nick())
            pl.casting(score,pl,pl.wep)
            pl.casting = nil
        end
    end)
else

    function finishedcasting(score)
        net.Start("FinishedCasting")
            net.WriteInt(score,BITCOUNT)
        net.SendToServer()
    end

    net.Receive("CastSpell",function()
        local spellname = net.ReadString()
		print("Request to cast spell" .. spellname)
        StartMinigame(spellname)
	end)
end

function meta:IsCasting()
    if(self.casting) then return false end
    return true
end