aboutsummaryrefslogtreecommitdiff
path: root/gamemode/core/combat/cl_weaponswing.lua
blob: 61c31b06219c59aaac45f8982232f52950179269 (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
local CLIENT_PAC_DIR = "artery/client/pacs"
local stt = nrequire("cl_state.lua")
local ball
function finddmgpoint(name)
	local filepath = string.format(CLIENT_PAC_DIR .. "/%s.txt",name)
	local filetext = file.Read(filepath,"DATA")
	local outfit = CompileString(string.format("return {%s}",filetext),name)()
	ball = LocalPlayer():FindPACPart(outfit, "wep_point")
end
function finddmgbox(who,name)
	local filepath = string.format(CLIENT_PAC_DIR .. "/%s.txt",name)
	local filetext = file.Read(filepath,"DATA")
	assert(filetext ~= nil, string.format("Could not find a pac file for name %q (%s)",name,filepath))
	local outfit = CompileString(string.format("return {%s}",filetext),name)()
	assert(outfit ~= nil, string.format("Failed to compile %q",filetext))
	ball = who:FindPACPart(outfit, "wep_box")
	assert(ball ~= pac.NULL, string.format("Could not find a prop called %q on pac %q","web_box",name))
end

local swingtbl = {}
local tracking = false
local animent

local swingtodraw = nil
net.Receive("artery_setswingdebug",function()
	swingtodraw = net.ReadTable()
end)

hook.Add("PreDrawTranslucentRenderables","draw_wepswing",function()
	local lpp = LocalPlayer():GetPos()
	if swingtodraw ~= nil then
		for i = 2, #swingtodraw do
			render.DrawLine( lpp + swingtodraw[i], lpp + swingtodraw[i-1], Color(255,0,0,255), false )
			render.DrawLine( lpp + swingtodraw[i], lpp + swingtodraw[i-1] + Vector(0,0,10), Color(0,255,0,255), false )
		end
	end
end)

net.Receive("artery_weaponswing",function()
	local animname = net.ReadString()
	local who = net.ReadEntity()
	local duration = net.ReadDouble()
	who:SetLuaAnimation(animname)
	timer.Simple(duration,function()
		who:StopLuaAnimation(animname)
	end)

end)

net.Receive("artery_doanimation",function()
	local animname = net.ReadString()
	local animtime = net.ReadDouble()
	local wepname = net.ReadString()
	local pacname = net.ReadString()
	local animdir = net.ReadString()
	local who = net.ReadEntity()
	local waittime = net.ReadDouble()

	swingtbl = {}
	--pac.SetupENT(who)
	finddmgbox(who,pacname)
	animent = who
	who:SetupBones()
	who:SetLuaAnimation(animname)
	stt.invopen = true
	timer.Simple(waittime,function()
		tracking = true
	end)
	timer.Simple(animtime,function()
		tracking = false
		stt.invopen = false
		who:StopLuaAnimation(animname)
		net.Start("artery_notifyserverofswing")
		net.WriteString(wepname)
		net.WriteString(animdir)
		net.WriteTable(swingtbl)
		net.SendToServer()
		swingtodraw = swingtbl
	end)
end)

---Force the player to do an animation.
-- The animation must have been already registered to animbonelib ![Requires admin](./req_admin)
--@usage artery_startanimation <animation name>
--@concommand artery_startanimation
concommand.Add("artery_startanimation",function(ply,cmd,args)
	if not ply:IsAdmin() then return end
	swingtbl = {}
	ply:SetLuaAnimation(args[1])
	timer.Simple(args[2],function()
		ply:StopLuaAnimation(args[1])
	end)
end)

local lastpos
hook.Add("Tick","trace_weppos",function()
	if not IsValid(ball) or not IsValid(ball.Entity) then return end
	if lastpos == nil then lastpos = ball.Entity:GetPos() end
	if  tracking then
		swingtbl[#swingtbl + 1] = ball.Entity:GetPos() - animent:GetPos()
		lastpos = ball.Entity:GetPos()
	end
end)