summaryrefslogtreecommitdiff
path: root/ftp_gmstranded/entities/weapons/gms_base_weapon.lua
blob: ec4124afb8737dfbaca42b6a8a70bb3d368871f7 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
AddCSLuaFile()

SWEP.PrintName = "GMS Base Weapon"
SWEP.Author = "Stranded Team"
SWEP.Contact = ""

SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false
SWEP.DrawAmmo = false
SWEP.ViewModelFOV = 54
SWEP.Slot = 0
SWEP.SlotPos = 1

SWEP.Spawnable = false

SWEP.Primary.Damage = 4
SWEP.Primary.Delay = 0.7
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = "none"

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = true
SWEP.Secondary.Ammo = "none"

SWEP.HoldType = "fist"
SWEP.Skin = 0
SWEP.HitDistance = 75

function SWEP:Initialize()
	self:SetWeaponHoldType( self.HoldType )
	if ( self.Skin ) then self:SetSkin( self.Skin ) end
end

function SWEP:SecondaryAttack()
end

function SWEP:Reload()
end

function SWEP:PreDrawViewModel( vm )
	if ( self.Skin && IsValid( vm ) ) then vm:SetSkin( self.Skin ) end
end

function SWEP:Deploy()
	self:SendWeaponAnim( ACT_VM_DRAW )
	
	self:Idle()

	return true
end

function SWEP:Holster()
	if ( self.Owner.InProcess || ProcessCompleteTime ) then return false end
	if ( self.Skin && self.Owner.GetViewModel && IsValid( self.Owner:GetViewModel() ) ) then self.Owner:GetViewModel():SetSkin( 0 ) end

	timer.Destroy( "rb655_idle" .. self:EntIndex() )

	return true
end

function SWEP:Equip( newOwner )
	SPropProtection.PlayerMakePropOwner( newOwner, self )
end

function SWEP:OnDrop()
	timer.Destroy( "rb655_idle" .. self:EntIndex() )
end

function SWEP:PrimaryAttack()

	local tr = util.TraceLine( {
		start = self.Owner:GetShootPos(),
		endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * self.HitDistance,
		filter = self.Owner,
		mask = self.Mask
	} )

	if ( !IsValid( tr.Entity ) && !self.NoTraceFix ) then 
		tr = util.TraceHull( {
			start = self.Owner:GetShootPos(),
			endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * self.HitDistance,
			filter = self.Owner,
			mask = self.Mask
		} )
	end

	if ( tr.Hit ) then
		self:OnHit( tr )
	elseif ( tr.HitWorld ) then
		self:PlayHitSound()
	end

	self:DoEffects( tr )

	self:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	self:SetNextSecondaryFire( CurTime() + self.Primary.Delay )

end

function SWEP:DoImpactEffects( tr )
	if ( !tr.MatType ) then return end
	if ( tr.MatType == MAT_GRATE ) then tr.Entity:EmitSound( "physics/metal/metal_chainlink_impact_hard" .. math.random( 1, 3 ) .. ".wav" ) return end

	local vecSrc = tr.StartPos
	local vecDirection = tr.Normal
	local pPlayer = self.Owner

	if ( pPlayer && pPlayer:IsPlayer() ) then
		vecSrc = pPlayer:GetShootPos()
		vecDirection = pPlayer:GetAimVector()
	else
		pPlayer = GetWorldEntity()
	end

	local bullet = {}
	bullet.Src = vecSrc
	bullet.Dir = vecDirection
	bullet.Num = 1
	bullet.Damage = 0
	bullet.Force = 0
	bullet.Tracer = 0
	bullet.Callback = function( attacker, tr, dmginfo )
		local doEffects = true
		if ( tr.HitPos:Distance( vecSrc ) > self.HitDistance ) then doEffects = false end
		if ( tr.HitPos:Distance( vecSrc ) > 96 ) then doEffects = false end

		return {
			damage = false,
			effects = doEffects
		}
	end

	pPlayer:FireBullets( bullet )
end

function SWEP:DoEffects( tr )
	if ( IsFirstTimePredicted() ) then
		self:PlaySwingAnimation( !tr.Hit )
		self:PlaySwingSound()
		self.Owner:SetAnimation( PLAYER_ATTACK1 )
		self:DoImpactEffects( tr )
	end
end

function SWEP:PlaySound( snd )
	if ( CLIENT ) then return end
	self.Owner:EmitSound( snd )
end

function SWEP:PlaySwingAnimation( missed )
	timer.Destroy( "rb655_idle" .. self:EntIndex() )
	self:DoAnimation( missed )
	self:Idle()
end

function SWEP:DoAnimation( missed )
	if ( missed ) then self:SendWeaponAnim( ACT_VM_MISSCENTER ) return end
	self:SendWeaponAnim( ACT_VM_HITCENTER )
end

function SWEP:OnHit( tr )
	local ent = tr.Entity
	if ( !IsValid( ent ) ) then self:PlayHitSound() return end
	if ( CLIENT ) then return end

	if ( ent:Health() > 0 ) then
		if ( ent:IsNPC() || ( ent:IsPlayer() && GetConVarNumber( "gms_PVPDamage" ) > 0 ) ) then ent:TakeDamage( self.Primary.Damage, self.Owner, self ) end
		self:PlayHitSound()
	else
		self:DoToolHit( ent )
	end
end

function SWEP:DoToolHit( ent )
	if ( ent:IsTreeModel() ) then
		self.Owner:DoProcess( "WoodCutting", 3, {
			Entity = ent,
			Chance = 33,
			MinAmount = 1,
			MaxAmount = 3
		} )
	elseif ( ent:IsRockModel() ) then
		self.Owner:DoProcess( "Mining", 3, {
			Entity = ent,
			Chance = 33,
			MinAmount = 1,
			MaxAmount = 2
		} )
	else
		self:PlayHitSound()
	end
end

function SWEP:PlaySwingSound()
	self:PlaySound( "weapons/slam/throw.wav" )
end

function SWEP:PlayHitSound()
	self:PlaySound( "Flesh.ImpactHard" )
end

function SWEP:DoIdleAnimation()
	self:SendWeaponAnim( ACT_VM_IDLE )
end

--------------------- IDLE ANIMS ---------------------

function SWEP:DoIdle()
	self:DoIdleAnimation()
	timer.Adjust( "rb655_idle" .. self:EntIndex(), self:GetAnimationTime(), 0, function()
		if ( !IsValid( self ) ) then timer.Destroy( "rb655_idle" .. self:EntIndex() ) return end
		self:DoIdleAnimation()
	end )
end

function SWEP:GetAnimationTime()
	local time = self:SequenceDuration()
	if ( time == 0 ) then time = self.Owner:GetViewModel():SequenceDuration() end
	return time
end

function SWEP:Idle()
	if ( CLIENT ) then return end
	timer.Create( "rb655_idle" .. self:EntIndex(), self:GetAnimationTime(), 1, function()
		if ( !IsValid( self ) ) then return end
		self:DoIdle()
	end )
end

if ( SERVER ) then return end

SWEP.FixWorldModel = false
SWEP.FixWorldModelPos = Vector( 0, 0, 0 )
SWEP.FixWorldModelAng = Angle( 0, 0, 0 )
SWEP.FixWorldModelScale = 1

function SWEP:RevertModel()
	self:SetRenderOrigin( self:GetNetworkOrigin() )
	self:SetRenderAngles( self:GetNetworkAngles() )
end

function SWEP:DoFixWorldModel()
	if ( !self.FixWorldModel ) then return end
	if ( !IsValid( self.Owner ) ) then self:RevertModel() return end

	local bone = self.Owner:LookupBone( "ValveBiped.Bip01_R_Hand" )
	if ( !bone ) then self:RevertModel() return end

	local pos, ang = self.Owner:GetBonePosition( bone )
	ang:RotateAroundAxis( ang:Forward(), 180 )

	ang:RotateAroundAxis( ang:Forward(), self.FixWorldModelAng.p )
	ang:RotateAroundAxis( ang:Right(), self.FixWorldModelAng.y )
	ang:RotateAroundAxis( ang:Up(), self.FixWorldModelAng.r )

	pos = pos + ang:Forward() * self.FixWorldModelPos.x + ang:Right() * self.FixWorldModelPos.y + ang:Up() * self.FixWorldModelPos.z

	self:SetModelScale( self.FixWorldModelScale, 0 )

	self:SetRenderOrigin( pos )
	self:SetRenderAngles( ang )
end

function SWEP:DrawWorldModel()
	self:DoFixWorldModel()
	self:DrawModel()
end

function SWEP:DrawWorldModelTranslucent()
	self:DrawWorldModel()
end