· 8 years ago · Mar 04, 2018, 01:52 PM
1function SWEP:Initialize()
2
3 if ( SERVER ) then
4 self:SetNPCMinBurst( 3 )
5 self:SetNPCMaxBurst( 30 )
6 self:SetNPCFireRate( 0.07 )
7 end
8 self.Weapon:SetDeploySpeed( self.WeaponDeploySpeed )
9 self:SetWeaponHoldType( self.HoldType )
10 self.Primary.Cone = self.HipCone
11 self.Weapon:SetNetworkedBool( "Ironsights", false )
12 gap = 50 mm = 20
13
14 if CLIENT then
15
16 // Create a new table for every weapon instance
17 self.VElements = table.FullCopy( self.VElements )
18 self.WElements = table.FullCopy( self.WElements )
19 self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )
20
21 self:CreateModels(self.VElements) // create viewmodels
22 self:CreateModels(self.WElements) // create worldmodels
23
24 // init view model bone build function
25 if IsValid(self.Owner) then
26 local vm = self.Owner:GetViewModel()
27 if IsValid(vm) then
28 self:ResetBonePositions(vm)
29
30 // Init viewmodel visibility
31 if (self.ShowViewModel == nil or self.ShowViewModel) then
32 vm:SetColor(Color(255,255,255,255))
33 else
34 // we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
35 vm:SetColor(Color(255,255,255,1))
36 // ^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in
37 // however for some reason the view model resets to render mode 0 every frame so we just apply a debug material to prevent it from drawing
38 vm:SetMaterial("Debug/hsv")
39 end
40 end
41 end
42
43 end
44
45end
46
47function SWEP:Holster()
48
49 if CLIENT and IsValid(self.Owner) then
50 local vm = self.Owner:GetViewModel()
51 if IsValid(vm) then
52 self:ResetBonePositions(vm)
53 end
54 end
55
56 return true
57end
58
59function SWEP:OnRemove()
60 self:Holster()
61end
62
63if CLIENT then
64
65 SWEP.vRenderOrder = nil
66 function SWEP:ViewModelDrawn()
67
68 local vm = self.Owner:GetViewModel()
69 if !IsValid(vm) then return end
70
71 if (!self.VElements) then return end
72
73 self:UpdateBonePositions(vm)
74
75 if (!self.vRenderOrder) then
76
77 // we build a render order because sprites need to be drawn after models
78 self.vRenderOrder = {}
79
80 for k, v in pairs( self.VElements ) do
81 if (v.type == "Model") then
82 table.insert(self.vRenderOrder, 1, k)
83 elseif (v.type == "Sprite" or v.type == "Quad") then
84 table.insert(self.vRenderOrder, k)
85 end
86 end
87
88 end
89
90 for k, name in ipairs( self.vRenderOrder ) do
91
92 local v = self.VElements[name]
93 if (!v) then self.vRenderOrder = nil break end
94 if (v.hide) then continue end
95
96 local model = v.modelEnt
97 local sprite = v.spriteMaterial
98
99 if (!v.bone) then continue end
100
101 local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
102
103 if (!pos) then continue end
104
105 if (v.type == "Model" and IsValid(model)) then
106
107 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
108 ang:RotateAroundAxis(ang:Up(), v.angle.y)
109 ang:RotateAroundAxis(ang:Right(), v.angle.p)
110 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
111
112 model:SetAngles(ang)
113 //model:SetModelScale(v.size)
114 local matrix = Matrix()
115 matrix:Scale(v.size)
116 model:EnableMatrix( "RenderMultiply", matrix )
117
118 if (v.material == "") then
119 model:SetMaterial("")
120 elseif (model:GetMaterial() != v.material) then
121 model:SetMaterial( v.material )
122 end
123
124 if (v.skin and v.skin != model:GetSkin()) then
125 model:SetSkin(v.skin)
126 end
127
128 if (v.bodygroup) then
129 for k, v in pairs( v.bodygroup ) do
130 if (model:GetBodygroup(k) != v) then
131 model:SetBodygroup(k, v)
132 end
133 end
134 end
135
136 if (v.surpresslightning) then
137 render.SuppressEngineLighting(true)
138 end
139
140 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
141 render.SetBlend(v.color.a/255)
142 model:DrawModel()
143 render.SetBlend(1)
144 render.SetColorModulation(1, 1, 1)
145
146 if (v.surpresslightning) then
147 render.SuppressEngineLighting(false)
148 end
149
150 elseif (v.type == "Sprite" and sprite) then
151
152 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
153 render.SetMaterial(sprite)
154 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
155
156 elseif (v.type == "Quad" and v.draw_func) then
157
158 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
159 ang:RotateAroundAxis(ang:Up(), v.angle.y)
160 ang:RotateAroundAxis(ang:Right(), v.angle.p)
161 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
162
163 cam.Start3D2D(drawpos, ang, v.size)
164 v.draw_func( self )
165 cam.End3D2D()
166
167 end
168
169 end
170
171 end
172
173 SWEP.wRenderOrder = nil
174 function SWEP:DrawWorldModel()
175
176
177
178 if (self.ShowWorldModel == nil or self.ShowWorldModel) then
179 self:DrawModel()
180 end
181
182 if (!self.WElements) then return end
183
184 if (!self.wRenderOrder) then
185
186 self.wRenderOrder = {}
187
188 for k, v in pairs( self.WElements ) do
189 if (v.type == "Model") then
190 table.insert(self.wRenderOrder, 1, k)
191 elseif (v.type == "Sprite" or v.type == "Quad") then
192 table.insert(self.wRenderOrder, k)
193 end
194 end
195
196 end
197
198 if (IsValid(self.Owner)) then
199 bone_ent = self.Owner
200
201 else
202
203 bone_ent = self
204 end
205
206
207
208 for k, name in pairs( self.wRenderOrder ) do
209
210 local v = self.WElements[name]
211 if (!v) then self.wRenderOrder = nil break end
212 if (v.hide) then continue end
213
214 local pos, ang
215
216 if (v.bone) then
217 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
218 else
219 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
220 end
221
222 if (!pos) then continue end
223
224 local model = v.modelEnt
225 local sprite = v.spriteMaterial
226
227 if (v.type == "Model" and IsValid(model)) then
228
229 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
230 ang:RotateAroundAxis(ang:Up(), v.angle.y)
231 ang:RotateAroundAxis(ang:Right(), v.angle.p)
232 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
233
234 model:SetAngles(ang)
235 //model:SetModelScale(v.size)
236 local matrix = Matrix()
237 matrix:Scale(v.size)
238 model:EnableMatrix( "RenderMultiply", matrix )
239
240 if (v.material == "") then
241 model:SetMaterial("")
242 elseif (model:GetMaterial() != v.material) then
243 model:SetMaterial( v.material )
244 end
245
246 if (v.skin and v.skin != model:GetSkin()) then
247 model:SetSkin(v.skin)
248 end
249
250 if (v.bodygroup) then
251 for k, v in pairs( v.bodygroup ) do
252 if (model:GetBodygroup(k) != v) then
253 model:SetBodygroup(k, v)
254 end
255 end
256 end
257
258 if (v.surpresslightning) then
259 render.SuppressEngineLighting(true)
260 end
261
262 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
263 render.SetBlend(v.color.a/255)
264 model:DrawModel()
265 render.SetBlend(1)
266 render.SetColorModulation(1, 1, 1)
267
268 if (v.surpresslightning) then
269 render.SuppressEngineLighting(false)
270 end
271
272 elseif (v.type == "Sprite" and sprite) then
273
274 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
275 render.SetMaterial(sprite)
276 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
277
278 elseif (v.type == "Quad" and v.draw_func) then
279
280 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
281 ang:RotateAroundAxis(ang:Up(), v.angle.y)
282 ang:RotateAroundAxis(ang:Right(), v.angle.p)
283 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
284
285 cam.Start3D2D(drawpos, ang, v.size)
286 v.draw_func( self )
287 cam.End3D2D()
288
289 end
290
291 end
292
293 end
294
295 function SWEP:GetBoneOrientation( basetab, tab, ent, bone_override )
296
297 local bone, pos, ang
298 if (tab.rel and tab.rel != "") then
299
300 local v = basetab[tab.rel]
301
302 if (!v) then return end
303
304 // Technically, if there exists an element with the same name as a bone
305 // you can get in an infinite loop. Let's just hope nobody's that stupid.
306 pos, ang = self:GetBoneOrientation( basetab, v, ent )
307
308 if (!pos) then return end
309
310 pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
311 ang:RotateAroundAxis(ang:Up(), v.angle.y)
312 ang:RotateAroundAxis(ang:Right(), v.angle.p)
313 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
314
315 else
316
317 bone = ent:LookupBone(bone_override or tab.bone)
318
319 if (!bone) then return end
320
321 pos, ang = Vector(0,0,0), Angle(0,0,0)
322 local m = ent:GetBoneMatrix(bone)
323 if (m) then
324 pos, ang = m:GetTranslation(), m:GetAngles()
325 end
326
327 if (IsValid(self.Owner) and self.Owner:IsPlayer() and
328 ent == self.Owner:GetViewModel() and self.ViewModelFlip) then
329 ang.r = -ang.r // Fixes mirrored models
330 end
331
332 end
333
334 return pos, ang
335 end
336
337 function SWEP:CreateModels( tab )
338
339 if (!tab) then return end
340
341 // Create the clientside models here because Garry says we can't do it in the render hook
342 for k, v in pairs( tab ) do
343 if (v.type == "Model" and v.model and v.model != "" and (!IsValid(v.modelEnt) or v.createdModel != v.model) and
344 string.find(v.model, ".mdl") and file.Exists (v.model, "GAME") ) then
345
346 v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)
347 if (IsValid(v.modelEnt)) then
348 v.modelEnt:SetPos(self:GetPos())
349 v.modelEnt:SetAngles(self:GetAngles())
350 v.modelEnt:SetParent(self)
351 v.modelEnt:SetNoDraw(true)
352 v.createdModel = v.model
353 else
354 v.modelEnt = nil
355 end
356
357 elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite)
358 and file.Exists ("materials/"..v.sprite..".vmt", "GAME")) then
359
360 local name = v.sprite.."-"
361 local params = { ["$basetexture"] = v.sprite }
362 // make sure we create a unique name based on the selected options
363 local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }
364 for i, j in pairs( tocheck ) do
365 if (v[j]) then
366 params["$"..j] = 1
367 name = name.."1"
368 else
369 name = name.."0"
370 end
371 end
372
373 v.createdSprite = v.sprite
374 v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)
375
376 end
377 end
378
379 end
380
381 local allbones
382 local hasGarryFixedBoneScalingYet = false
383
384 function SWEP:UpdateBonePositions(vm)
385
386 if self.ViewModelBoneMods then
387
388 if (!vm:GetBoneCount()) then return end
389
390 // !! WORKAROUND !! //
391 // We need to check all model names :/
392 local loopthrough = self.ViewModelBoneMods
393 if (!hasGarryFixedBoneScalingYet) then
394 allbones = {}
395 for i=0, vm:GetBoneCount() do
396 local bonename = vm:GetBoneName(i)
397 if (self.ViewModelBoneMods[bonename]) then
398 allbones[bonename] = self.ViewModelBoneMods[bonename]
399 else
400 allbones[bonename] = {
401 scale = Vector(1,1,1),
402 pos = Vector(0,0,0),
403 angle = Angle(0,0,0)
404 }
405 end
406 end
407
408 loopthrough = allbones
409 end
410 // !! ----------- !! //
411
412 for k, v in pairs( loopthrough ) do
413 local bone = vm:LookupBone(k)
414 if (!bone) then continue end
415
416 // !! WORKAROUND !! //
417 local s = Vector(v.scale.x,v.scale.y,v.scale.z)
418 local p = Vector(v.pos.x,v.pos.y,v.pos.z)
419 local ms = Vector(1,1,1)
420 if (!hasGarryFixedBoneScalingYet) then
421 local cur = vm:GetBoneParent(bone)
422 while(cur >= 0) do
423 local pscale = loopthrough[vm:GetBoneName(cur)].scale
424 ms = ms * pscale
425 cur = vm:GetBoneParent(cur)
426 end
427 end
428
429 s = s * ms
430 // !! ----------- !! //
431
432 if vm:GetManipulateBoneScale(bone) != s then
433 vm:ManipulateBoneScale( bone, s )
434 end
435 if vm:GetManipulateBoneAngles(bone) != v.angle then
436 vm:ManipulateBoneAngles( bone, v.angle )
437 end
438 if vm:GetManipulateBonePosition(bone) != p then
439 vm:ManipulateBonePosition( bone, p )
440 end
441 end
442 else
443 self:ResetBonePositions(vm)
444 end
445
446 end
447
448 function SWEP:ResetBonePositions(vm)
449
450 if (!vm:GetBoneCount()) then return end
451 for i=0, vm:GetBoneCount() do
452 vm:ManipulateBoneScale( i, Vector(1, 1, 1) )
453 vm:ManipulateBoneAngles( i, Angle(0, 0, 0) )
454 vm:ManipulateBonePosition( i, Vector(0, 0, 0) )
455 end
456
457 end
458
459 /**************************
460 Global utility code
461 **************************/
462
463 // Fully copies the table, meaning all tables inside this table are copied too and so on (normal table.Copy copies only their reference).
464 // Does not copy entities of course, only copies their reference.
465 // WARNING: do not use on tables that contain themselves somewhere down the line or you'll get an infinite loop
466 function table.FullCopy( tab )
467
468 if (!tab) then return nil end
469
470 local res = {}
471 for k, v in pairs( tab ) do
472 if (type(v) == "table") then
473 res[k] = table.FullCopy(v) // recursion ho!
474 elseif (type(v) == "Vector") then
475 res[k] = Vector(v.x, v.y, v.z)
476 elseif (type(v) == "Angle") then
477 res[k] = Angle(v.p, v.y, v.r)
478 else
479 res[k] = v
480 end
481 end
482
483 return res
484
485 end
486
487end
488
489if ( SERVER ) then
490
491 AddCSLuaFile( "shared.lua" )
492 SWEP.Weight = 5
493 SWEP.AutoSwitchTo = false
494 SWEP.AutoSwitchFrom = false
495
496end
497
498if ( CLIENT ) then
499
500 SWEP.DrawAmmo = true
501 SWEP.DrawCrosshair = false
502 SWEP.ViewModelFOV = 70
503 SWEP.ViewModelFlip = false
504 SWEP.CSMuzzleFlashes = true
505 SWEP.WepSelectIcon = surface.GetTextureID("vgui/gfx/vgui/deserteagle.vtf")
506 SWEP.BounceWeaponIcon = false
507 local icol = Color( 255, 255, 255, 255 )
508 killicon.Add( "deserteagle.vtf", "vgui/gfx/vgui", icol )
509
510end
511SWEP.Category = "test"
512SWEP.PrintName = "test"
513SWEP.Purpose = "test"
514SWEP.HoldType = "pistol"
515SWEP.Slot = 1
516SWEP.SlotPos = 2
517
518SWEP.WElements = {
519 ["deagle"] = { type = "Model", model = "models/weapons/w_pist_deagle.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(0.455, 0, 0), angle = Angle(0, 0, 180), size = Vector(0.947, 0.947, 0.947), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
520}
521
522SWEP.Author = "Shekelstein"
523
524SWEP.ViewModel = "models/weapons/v_pist_deagle.mdl"
525SWEP.WorldModel = "models/weapons/w_pist_deagle.mdl"
526SWEP.Spawnable = true
527SWEP.AdminSpawnable = true
528SWEP.ShowViewModel = true
529SWEP.ShowWorldModel = true
530
531SWEP.Primary.Sound = Sound( "weapons/deagle/shoot.mp3" )
532SWEP.Primary.Damage = 31
533SWEP.Primary.NumShots = 1
534SWEP.Primary.Delay = 0.2
535SWEP.Primary.Tracer = 1
536SWEP.Primary.Force = 200
537SWEP.Primary.ClipSize = 7
538SWEP.Primary.DefaultClip = 50
539SWEP.Primary.Automatic = false
540SWEP.Primary.Ammo = "pistol"
541SWEP.ViewKick = 0.2
542SWEP.ShellDelay = 0
543SWEP.ShellEffect = "none"
544SWEP.MuzzleEffect = "none"
545SWEP.MuzzleAttachment = "1"
546SWEP.ShellEjectAttachment = "1"
547SWEP.abletosprintpos = true
548
549
550SWEP.Secondary.ClipSize = -1
551SWEP.Secondary.DefaultClip = -1
552SWEP.Secondary.Automatic = true
553SWEP.Secondary.Ammo = "none"
554
555SWEP.IronFov = 60
556SWEP.pistol = true
557SWEP.Rifle = false
558SWEP.Shotgun = false
559SWEP.WeaponDeploySpeed = 1
560SWEP.Sniper = false
561SWEP.AimCone = 0.0002
562SWEP.HipCone = 0.03
563
564SWEP.IronSightsPos = Vector(-1.3, -4, 2)
565SWEP.IronSightsAng = Vector(0, 0, 0)
566
567SWEP.DSightsPos = Vector(0, 0, 0)
568SWEP.DSightsAng = Vector(0, 0, 0)
569
570SWEP.RunSightsPos = Vector(0, 0, 0)
571SWEP.RunSightsAng = Vector(0, 0, 0)
572
573function SWEP:Deploy()
574
575 timer.Simple(0.1, function()
576 self.Weapon:EmitSound("weapons/deagle/deploy.wav")
577 end)
578
579 timer.Simple(0.1, function()
580 self.Weapon:EmitSound("weapons/deagle/slide.wav")
581 end)
582
583 self.Weapon:SetNetworkedBool( "Ironsights", false )
584 return true
585end
586
587function SWEP:DrawWeaponSelection(x, y, wide, tall, alpha)
588
589 surface.SetDrawColor(255, 255, 255, alpha)
590 surface.SetTexture(self.WepSelectIcon)
591
592 local fsin = 0
593
594 if (self.BounceWeaponIcon == true) then
595 fsin = math.sin(CurTime() * 10) * 5
596 end
597
598 y = y + 10
599 x = x + 10
600 wide = wide - 20
601
602 surface.DrawTexturedRect(x + (fsin), y - (fsin), wide - fsin * 2, (wide / 2) + (fsin))
603
604 self:PrintWeaponInfo(x + wide + 20, y + tall * 0.95, alpha)
605end
606
607function SWEP:Reload()
608
609 if ( self.Weapon:GetNetworkedBool( "reloading" ) ) or self.Weapon:Clip1() == 5 then return end
610
611 self.Weapon:SetNetworkedBool( "Ironsights", false )
612
613 self.Weapon:SetNetworkedBool( "reloading", true )
614
615
616 timer.Simple(0.2, function()
617 self.Weapon:EmitSound("weapons/deagle/clipout.wav")
618 end)
619
620 timer.Simple(1.3, function()
621 self.Weapon:EmitSound("weapons/deagle/clipin.wav")
622 end)
623
624 timer.Simple(4.3, function()
625
626 self.Weapon:SetNetworkedBool( "reloading", false )
627
628 end)
629
630 self.Weapon:DefaultReload( ACT_VM_RELOAD );
631
632
633end
634
635function SWEP:PrimaryAttack()
636
637 if self.Weapon:Clip1() == 0 then
638 self.Weapon:EmitSound("weapons/shotgun/shotgun_empty.wav", 65, math.random(99, 101))
639 self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
640 self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
641 return
642 self.Weapon:Reload()
643 end
644 self.Reloadaftershoot = CurTime() + self.Primary.Delay
645 self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
646 self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
647
648 if ( !self:CanPrimaryAttack() ) then return end
649
650 self.Weapon:EmitSound( self.Primary.Sound )
651
652 self.PunchDir = Angle(-1,math.random(-1,1),0)
653
654 self:CSShootBullet( self.Primary.Damage, self.Primary.NumShots, self.Primary.Cone )
655
656 self.Owner:SetEyeAngles(self.Owner:EyeAngles()+self.PunchDir*self.ViewKick)
657
658 self:TakePrimaryAmmo( 1 )
659
660 if ( self.Owner:IsNPC() ) then return end
661
662
663
664 if ( (game.SinglePlayer() && SERVER) || CLIENT ) then
665 self.Weapon:SetNetworkedFloat( "LastShootTime", CurTime() )
666 end
667
668end
669
670function SWEP:Think()
671
672 if self.Weapon:GetNetworkedBool( "ironsights" ) then
673 self.Primary.Cone = self.AimCone
674 end
675
676 if not self.Weapon:GetNetworkedBool( "ironsights" ) then
677 self.Primary.Cone = self.HipCone
678 end
679end
680
681function SWEP:CSShootBullet( dmg, numbul, cone )
682
683 self:ShootEffects()
684
685 numbul = numbul or 1
686 cone = cone or 0.01
687
688 local bullet = {}
689 bullet.Num = numbul
690 bullet.Src = self.Owner:GetShootPos()
691 bullet.Dir = self.Owner:GetAimVector()
692 bullet.Spread = Vector( cone, cone, 0 )
693 bullet.Tracer = self.Primary.Tracer
694 bullet.Force = self.Primary.Force
695 bullet.Damage = 300
696
697 self.Owner:FireBullets( bullet )
698 self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
699 self.Owner:SetAnimation( PLAYER_ATTACK1 )
700
701 if ( self.Owner:IsNPC() ) then return end
702
703 self.Owner:SetEyeAngles(self.Owner:EyeAngles()+self.PunchDir*self.ViewKick)
704end
705
706function SWEP:DrawWeaponSelection(x, y, wide, tall, alpha)
707
708 surface.SetDrawColor(255, 255, 255, alpha)
709 surface.SetTexture(self.WepSelectIcon)
710
711 local fsin = 0
712
713 if (self.BounceWeaponIcon == true) then
714 fsin = math.sin(CurTime() * 10) * 5
715 end
716
717 y = y + 10
718 x = x + 10
719 wide = wide - 20
720
721 surface.DrawTexturedRect(x + (fsin), y - (fsin), wide - fsin * 2, (wide / 2) + (fsin))
722
723 self:PrintWeaponInfo(x + wide + 20, y + tall * 0.95, alpha)
724end
725
726local IRONSIGHT_TIME = 0.25
727
728
729function SWEP:GetViewModelPosition( pos, ang )
730
731 if ( !self.IronSightsPos ) then return pos, ang end
732
733 local bIron = self.Weapon:GetNetworkedBool( "Ironsights" )
734
735 if ( bIron != self.bLastIron ) then
736
737 self.bLastIron = bIron
738 self.fIronTime = CurTime()
739
740 if ( bIron ) then
741 self.SwayScale = 0
742 self.BobScale = 0
743 else
744 self.SwayScale = 0
745 self.BobScale = 0
746 end
747
748 end
749
750 local fIronTime = self.fIronTime or 0
751
752 if ( !bIron && fIronTime < CurTime() - IRONSIGHT_TIME ) then
753 return pos, ang
754 end
755
756 local Mul = 1.0
757
758 if ( fIronTime > CurTime() - IRONSIGHT_TIME ) then
759
760 Mul = math.Clamp( (CurTime() - fIronTime) / IRONSIGHT_TIME, 0, 1 )
761
762 if (!bIron) then Mul = 1 - Mul end
763
764 end
765
766 local Offset = self.IronSightsPos
767
768 if ( self.IronSightsAng ) then
769
770 ang = ang * 1
771 ang:RotateAroundAxis( ang:Right(), self.IronSightsAng.x * Mul )
772 ang:RotateAroundAxis( ang:Up(), self.IronSightsAng.y * Mul )
773 ang:RotateAroundAxis( ang:Forward(), self.IronSightsAng.z * Mul )
774
775
776 end
777
778 local Right = ang:Right()
779 local Up = ang:Up()
780 local Forward = ang:Forward()
781
782
783
784 pos = pos + Offset.x * Right * Mul
785 pos = pos + Offset.y * Forward * Mul
786 pos = pos + Offset.z * Up * Mul
787
788 return pos, ang
789
790end
791
792function SWEP:SetIronsights( b )
793
794 self.Weapon:SetNetworkedBool( "Ironsights", b )
795
796end
797
798
799SWEP.NextSecondaryAttack = 0
800
801 function SWEP:SecondaryAttack()
802
803 if ( !self.IronSightsPos ) then return end
804 if ( self.NextSecondaryAttack > CurTime() ) then return end
805
806 bIronsights = !self.Weapon:GetNetworkedBool( "Ironsights", false )
807
808 self:SetIronsights( bIronsights )
809
810 self.NextSecondaryAttack = CurTime() + 0.3
811
812end
813
814function SWEP:DrawHUD()
815 if ( self.Weapon:GetNetworkedBool( "Ironsights" ) ) then return end
816local x = ScrW()/2
817local y = ScrH()/2
818 local scale = 10 * self. HipCone // Scale the size of the crosshair according to how long ago we fired our weapon
819 local LastShootTime = self.Weapon:GetNetworkedFloat( "LastShootTime", 0 )
820 if self.Owner:KeyPressed(IN_ATTACK) then
821 mm = 20
822 end
823 if self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT) then
824 gap = math.Clamp(gap+0.45,15,40)
825 mm = 40
826 else
827 gap = math.Clamp(gap-0.45,15,mm)
828 end
829 gap = gap * (2 - math.Clamp( (CurTime() - LastShootTime)*5, 0, 1 ))
830 local length = gap + 70 * scale
831
832 surface.SetDrawColor( 25,25,25, 150)
833 surface.DrawRect( x+length+gap/length-10, y, 9, 5 )
834 surface.DrawRect( x-length+4, y, 9, 5 )
835 surface.DrawRect( x, y-length+4, 5, 9 )
836 surface.DrawRect( x, y+length+gap/length-10, 5, 9 )
837 surface.SetDrawColor( 225,225,225, 150)
838 surface.DrawRect( x+length+gap/length-10, y, 8, 4 )
839 surface.DrawRect( x-length+4, y, 8, 4 )
840 surface.DrawRect( x, y-length+4, 4, 8 )
841 surface.DrawRect( x, y+length+gap/length-10, 4, 8 ) if self.Weapon:GetNetworkedBool( "Scoped" ) == true then
842 surface.SetTexture(surface.GetTextureID("gmod/scope"))
843 surface.SetDrawColor(0,0,0,255)
844 surface.DrawTexturedRect(0,0,ScrW(),ScrH())
845 surface.SetDrawColor( 0, 0, 0, 255 )
846 surface.DrawLine( 0, ScrH() / 2, ScrW(), ScrH() / 2)
847 surface.DrawLine( ScrW() / 2, 0, ScrW() / 2, ScrH())
848 end
849
850end
851
852function SWEP:OnRestore()
853
854 self.NextSecondaryAttack = 0
855 self:SetIronsights( false )
856
857end
858
859
860function SWEP:ShouldDropOnDie()
861 return true
862end
863
864function SWEP:Holster()
865return true
866end
867
868function SWEP:ShootEffects()
869
870
871 self.Owner:MuzzleFlash()
872
873 local fx = EffectData()
874 fx:SetEntity(self.Weapon)
875 fx:SetOrigin(self.Owner:GetShootPos())
876 fx:SetNormal(self.Owner:GetAimVector())
877 fx:SetAttachment(self.MuzzleAttachment)
878 util.Effect(self.MuzzleEffect,fx)
879
880 local effectdata = EffectData()
881 effectdata:SetOrigin(self.Owner:GetShootPos())
882 effectdata:SetEntity(self.Weapon)
883 effectdata:SetStart(self.Owner:GetShootPos())
884 effectdata:SetNormal(self.Owner:GetAimVector())
885 effectdata:SetAttachment(1)
886
887
888 timer.Simple(self.ShellDelay, function()
889 if not self.Owner then return end
890 if not IsFirstTimePredicted() then return end
891 if not self.Owner:IsNPC() and not self.Owner:Alive() then return end
892
893 local fx = EffectData()
894 fx:SetEntity(self.Weapon)
895 fx:SetNormal(self.Owner:GetAimVector())
896 fx:SetAttachment(self.ShellEjectAttachment)
897
898 util.Effect(self.ShellEffect,fx)
899 end)
900
901 local trace = self.Owner:GetEyeTrace()
902
903end
904
905
906function SWEP:Sprint()
907
908 if self.Owner:KeyDown (IN_SPEED) then
909 if self.Rifle == true then
910 self:SetWeaponHoldType("passive")
911 end
912 if self.Pistol == true then
913 self:SetWeaponHoldType("passive")
914 end
915 self.Owner:SetFOV( self.FOV, 0.3 )
916 self.OriginalFOV = self.Owner:GetFOV()
917 self.IronSightsPos = self.RunSightsPos
918 self.IronSightsAng = self.RunSightsAng
919 self.Weapon:SetNextPrimaryFire(1000000000000000000000000000000)
920 self:SetIronsights(true, self.Owner)
921 if ( bIron ) then
922self.SwayScale = 0
923self.BobScale = 0
924else
925self.SwayScale = 0
926self.BobScale = 0
927end
928end
929
930 if self.Owner:KeyReleased (IN_SPEED) then
931 if self.Pistol == true then
932 self:SetWeaponHoldType("ar2")
933 end
934 if self.Rifle == true then
935 self:SetWeaponHoldType("ar2")
936 end
937 if self.Shotgun == true then
938 self:SetWeaponHoldType("Shotgun")
939 self.Weapon:SendWeaponAnim( ACT_VM_IDLE )
940 end
941
942 self.IronSightsPos = self.DSightsPos
943 self.IronSightsAng = self.DSightsAng
944 self:SetIronsights(false, self.Owner)
945 self.Weapon:SetNextPrimaryFire( CurTime())
946 self.Owner:SetFOV( self.FOV, 0.3 )
947 self.OriginalFOV = self.Owner:GetFOV()
948 if ( bIron ) then
949self.SwayScale = 0
950self.BobScale = 0
951else
952self.SwayScale = 0
953self.BobScale = 0
954end
955 end
956 end
957
958function SWEP:Ironzoom()
959
960 if self.Owner:KeyDown(IN_ATTACK2) then
961 self:SetIronsights(true, self.Owner)
962 self.Primary.Cone = self.AimCone
963
964 end
965
966 if self.Owner:KeyReleased(IN_ATTACK2) then
967 self:SetIronsights(false, self.Owner)
968 self.Primary.Cone = self.HipCone
969
970 end
971
972end