aboutsummaryrefslogtreecommitdiff
path: root/entities/weapons/hands.lua
blob: 7532104512228c9aa56d1393ff23ee6db4d82bb9 (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
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
function SWEP:DefaultPickup()
	if self.Pickup == nil then
		local tr = self.Owner:GetEyeTrace()
		local dist = self.Owner:GetPos():Distance(tr.HitPos)
		if dist < 100 and tr.Entity:GetMass() < 100 then
			self.Pickup = tr.Entity
			self.PickupAngles = tr.Entity:GetAngles()
			self.PickupOffset = dist
		end
	else
		self.Pickup = nil
	end
end

function SWEP:Tick()
	if self.Pickup ~= nil then
		
		--Set position
		local targetpos = (self.Owner:EyeAngles():Forward() * self.PickupOffset * 2) + self.Owner:GetPos() + Vector(0,0,64)
		local deltavel = targetpos - (self.Pickup:GetPos() + self.Pickup:GetVelocity())
		local clampvel = Vector(math.Clamp(deltavel.x,-100,100),math.Clamp(deltavel.y,-100,100),math.Clamp(deltavel.z,-100,100))
		self.Pickup:GetPhysicsObject():SetVelocity(clampvel)
		
		--Set angles
		local targetang = self.Owner:EyeAngles() + self.PickupAngles
		local angvel = self.Pickup:GetAngles() - targetang
		local deltaang = self.Pickup:GetPhysicsObject():GetAngleVelocity() - angvel:Forward()
		print("target",targetang,"vel",angvel,"delta",deltaang)
		print("Cur is", self.Pickup:GetPhysicsObject():GetAngleVelocity())
		self.Pickup:GetPhysicsObject():AddAngleVelocity(angvel:Forward())
	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

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