SWEP.WepSelectIcon = Material("vgui/entities/hud_usp_icon.png")

if CLIENT then
	function SWEP:DrawWeaponSelection(x, y, w, h, a)
		surface.SetDrawColor(255, 255, 255, a)
		surface.SetMaterial(self.WepSelectIcon)
		
		local size = math.min(w, h)
		surface.DrawTexturedRect(x + 80, y + 35, size - 80, size - 80)
	end
end
SWEP.PrintName = ".45 PISTOL"
SWEP.Instructions = "Do something"

SWEP.Category = "My cool things"

SWEP.Spawnable= true
SWEP.AdminSpawnable= true
SWEP.AdminOnly = false

SWEP.CSMuzzleFlashes = true
SWEP.ViewModelFOV = 50
SWEP.ViewModel = "models/weapons/cstrike/c_pist_usp.mdl"
SWEP.WorldModel = "models/weapons/w_pistol.mdl"
SWEP.ViewModelFlip = false
SWEP.ShowWorldModel = true
SWEP.ShowViewModel = true


SWEP.AutoSwitchTo = true
SWEP.AutoSwitchFrom = true

SWEP.Slot = 1
SWEP.SlotPos = 1
 
SWEP.UseHands = true

SWEP.HoldType = "pistol" 

SWEP.FiresUnderwater = true

SWEP.DrawCrosshair = true

SWEP.DrawAmmo = true

SWEP.ReloadSound = "Weapon_MAC10.Clipout"

SWEP.Base = "weapon_base"

SWEP.Primary.Sound = Sound("weapons/pistol/pistol_fire1_new.wav") 
SWEP.Primary.Damage = 19
SWEP.Primary.TakeAmmo = 1
SWEP.Primary.ClipSize = 12
SWEP.Primary.Ammo = "pistol"
SWEP.Primary.DefaultClip = 12
SWEP.Primary.Spread = 0.11
SWEP.Primary.NumberofShots = 1
SWEP.Primary.Automatic = false
SWEP.Primary.Recoil = 1.5
SWEP.Primary.Delay = 0.1
SWEP.Primary.Force = 1.5

SWEP.Secondary.ClipSize = 0
SWEP.Secondary.DefaultClip = 0
SWEP.Secondary.Automatic = true
SWEP.Secondary.Ammo = "none"
SWEP.Secondary.Delay = 0.2
SWEP.Secondary.Spread = 0.5
SWEP.Secondary.Recoil = 2

function SWEP:Initialize()
util.PrecacheSound(self.Primary.Sound) 
util.PrecacheSound(self.ReloadSound) 
        self:SetWeaponHoldType( self.HoldType )
end 
function SWEP:Deploy()
	self:SendWeaponAnim( ACT_VM_DRAW )
	self:SetNextPrimaryFire( CurTime() + self:SequenceDuration() )
	return true
end 

function SWEP:PrimaryAttack()
 
if ( !self:CanPrimaryAttack() ) then return end
 
local bullet = {} 
bullet.Num = self.Primary.NumberofShots 
bullet.Src = self.Owner:GetShootPos() 
bullet.Dir = self.Owner:GetAimVector() 
bullet.Spread = Vector( self.Primary.Spread * 0.1 , self.Primary.Spread * 0.1, 0)
bullet.Tracer = 1
bullet.TracerName = "Tracer"
bullet.Force = self.Primary.Force 
bullet.Damage = self.Primary.Damage 
bullet.AmmoType = self.Primary.Ammo 
bullet.Callback = function(att, tr, dmginfo)
	if ( att.IsPlayer() ) then
	    local eyeang = att:EyeAngles()
		eyeang.pitch = math.Clamp(eyeang.pitch - 3, -90, 90)
		att:SetEyeAngles(eyeang)
	end
end
 
local rnda = self.Primary.Recoil * -1 
local rndb = self.Primary.Recoil * math.random(-1, 1) 
 
self:ShootEffects()
self.Owner:MuzzleFlash()
 
self.Owner:FireBullets( bullet ) 
self.Owner:EmitSound(Sound(self.Primary.Sound), 140, 100, 1, CHAN_WEAPON )
self.Owner:ViewPunch( Angle( rnda,rndb,rnda ) ) 
self:TakePrimaryAmmo(self.Primary.TakeAmmo) 
 
self:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
self:SetNextSecondaryFire( CurTime() + self.Primary.Delay ) 
end

function SWEP:SecondaryAttack()
return false
end

function SWEP:DoImpactEffect( tr, nDamageType )
		local lamp = ProjectedTexture()
		self.lamp = lamp

		lamp:SetTexture( "garrycrisis_shared/muzzleflash/soft" )
		lamp:SetFarZ( 400 )
		lamp:SetNearZ ( 60 )
		lamp:SetFOV( 140 )
		lamp:SetBrightness( 1 )
		lamp:SetEnableShadows ( true )

		lamp:SetPos( self.Owner:GetShootPos() )
		lamp:SetAngles( self.Owner:GetAngles() )
		lamp:Update()
		timer.Create( "muzzledelay" .. self:EntIndex(), 0.02, 1, function()
			lamp:Remove()
		end )

end

function SWEP:OnRemove()
	self:Holster()
end

function SWEP:Reload()
         if ( self:Clip1() ~= self.Primary.ClipSize and self:Ammo1() ~= 0 ) then
		      self:DefaultReload( ACT_VM_RELOAD )
			  self:EmitSound( "Weapon_Pistol.Reload" )
		 end
end