aboutsummaryrefslogtreecommitdiff
path: root/entities/weapons/hands.lua
blob: 9c7059e4cd4bc99af604d59e79a74110efbbfbbb (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
AddCSLuaFile("hands.lua")

SWEP.ViewModel				= ""--"models/error.mdl"
SWEP.WorldModel				= ""--"models/error.mdl"

SWEP.HoldType 				= "normal"

SWEP.Primary.ClipSize       = -1
SWEP.Primary.DefaultClip    = -1
SWEP.Primary.Automatic      = false
SWEP.Primary.Ammo           = "none"

SWEP.Secondary.Clipsize    	= -1
SWEP.Secondary.DefaultClip  = -1
SWEP.Secondary.Automatic   	= false
SWEP.Secondary.Ammo         = "none"

SWEP.DrawAmmo				= false
SWEP.DrawCrosshair			= false

function SWEP:Initialize()
	self:SetWeaponHoldType(self.HoldType)
	self:DrawShadow(false)
	self:SetNoDraw(true)

	--[[
	if (CLIENT) then
		self.MOB = ClientsideModel("error.mdl")
		self.MOB:SetNoDraw(true)
		self.MOB:DrawShadow(false)
	end
	]]
end

SWEP.Pickup = nil
SWEP.PickupAngles = Angle(0,0,0)
SWEP.PickupOffset = 100

local buildzones = {
	["artery_shipyard"] = true,
}
function SWEP:DefaultPickup()
	print("Default pickup fired, self.pickup is", self.Pickup)
	local zc = self.Owner:GetCurrentZone()
	local building = zc != nil and buildzones[zc] != nil
	if self.Pickup == nil then
		local tr = self.Owner:GetEyeTrace()
		local dist = self.Owner:GetPos():Distance(tr.HitPos)
		local mass = -1
		pcall(function()
			mass = tr.Entity:GetPhysicsObject():GetMass()
		end,function()
			print("Failed to get mass")
		end)

		if building or (dist < 100 and mass != -1 and mass < 400) then
			self.OldMass = mass
			self.Pickup = tr.Entity
			self.PickupAngles = tr.Entity:GetAngles() - self.Owner:EyeAngles()
			self.PickupOffset = dist
			tr.Entity:GetPhysicsObject():SetMass(3)
			tr.Entity:GetPhysicsObject():EnableCollisions(false)
		else
			print("dist is:" ,dist)
			if tr.Entity.GetMass then
				print("Mass is", tr.Entity:GetMass())
			else
				print("Ent does not have mass!", mass)
			end
			print("Could not pick that up")
		end
	else
		if IsValid(self.Pickup) and zc != nil and buildzones[zc.class] then
			--self.Pickup:GetPhysicsObject():EnableMotion(false)
		end
		self.Pickup:GetPhysicsObject():SetMass(self.OldMass)
		self.Pickup:GetPhysicsObject():EnableCollisions(true)
		self.Pickup = nil
	end
end

function SWEP:Tick()
	if self.Pickup ~= nil and IsValid(self.Pickup) then
		local crouchadd
		if self.Owner:Crouching() then
			crouchadd = Vector(0,0,32)
		else
			crouchadd = Vector(0,0,64)
		end
		local targetpos = (self.Owner:EyeAngles():Forward() * self.PickupOffset * 2) + self.Owner:GetPos() + crouchadd
		local targetang = self.Owner:EyeAngles() + self.PickupAngles
		--Check if we're inside a build zone
		local zc = self.Owner:GetCurrentZone()
		if zc != nil and zc.class == "artery_shipyard" then
			self.Pickup:SetPos(targetpos)
			self.Pickup:SetAngles(targetang)
		else

			--Set position
			local pow = 100
			local deltavel = targetpos - (self.Pickup:GetPos() + (self.Pickup:GetVelocity() / pow))
			local vlim = 100000
			local clampvel = Vector(math.Clamp(deltavel.x,-vlim,vlim),math.Clamp(deltavel.y,-vlim,vlim),math.Clamp(deltavel.z,-vlim,vlim))
			local speed = 20
			self.Pickup:GetPhysicsObject():SetVelocity(clampvel * speed)

			--Set angles
			local angvel = self.Pickup:GetAngles() - targetang
			local deltaang = self.Pickup:GetPhysicsObject():GetAngleVelocity() - angvel:Forward()
			self.Pickup:SetAngles(targetang)
			--self.Pickup:GetPhysicsObject():AddAngleVelocity(angvel:Forward())
		end
	end
end

function SWEP:ShouldDropOnDie()
	return false
end

function SWEP:PrimaryAttack()
	if CLIENT then return end

	--Make sure we have an equipment inventory
	if not self.Owner then return end
	if not self.Owner.data then return end
	if not self.Owner.data.inventories then return end
	if not self.Owner.data.inventories[1] then return end
	local eqpd = self.Owner.data.inventories[1]

	--Get the weapon we want to fire, and fire it!
	local weapon = eqpd:Get({"Left Hand"}) or eqpd:Get({"Dual"})
	if not weapon then
		self:DefaultPickup()
	elseif weapon.onClick ~= nil then
		weapon:onClick(self.Owner)
	end --Make sure we have a weapon
end

function SWEP:SecondaryAttack()
	if CLIENT then return end

	--Make sure we have an equipment inventory
	if not self.Owner then return end
	if not self.Owner.data then return end
	if not self.Owner.data.inventories then return end
	if not self.Owner.data.inventories[1] then return end
	local eqpd = self.Owner.data.inventories[1]

	--Get the weapon we want to fire, and fire it!
	local weapon = eqpd:Get({"Right Hand"}) or eqpd:Get({"Dual"})
	if not weapon then
		self:DefaultPickup()
	elseif weapon.onClick ~= nil then
		weapon:onClick(self.Owner)
	end --Make sure we have a weapon
end

--Prevent switching away from the hands weapon
hook.Add("PlayerSwitchWeapon","prevent_switch",function(ply,oldwep,newwep)
	return false
end)

if CLIENT then
	hook.Add( "HUDShouldDraw", "HideWeaponSelHUD", function( name )
	    if name == "CHudWeaponSelection" then return false end
	end )
end

hook.Add("PlayerSpawn","give_hands",function(ply)
	ply:Give("hands")
end)