--CasingMesh.Scale = Vector3.new(.75,.75,.75)

CasingMesh.Parent = CasingBase

function OnFire()
	if IsShooting then return end
	if MyHumanoid and MyHumanoid.Health > 0 then
		if RecoilTrack and AmmoInClip > 0 then
			
		end
		IsShooting = true
		if AmmoInClip > 0 and not Reloading then
			--if RecoilTrack then RecoilTrack:Play() end
			if Handle:FindFirstChild('FireSound') then
				Handle.FireSound:Play()
			end
			CreateFlash()
			if MyMouse then
				MyMouse.Icon = "rbxasset://textures\\GunWaitCursor.png"
				local targetPoint = MyMouse.Hit.p
				for i = 1, BulletCount do
					local shootDirection = (targetPoint - Handle.Position).unit
					-- Adjust the shoot direction randomly off by a little bit to account for recoil
					-- moves random*spread in a random angle from center shot point (center bias)
					shootDirection = ((CFrame.new(Vector3.new(0,0,0),shootDirection) * CFrame.Angles(0,0,math.random()*2*math.pi))*CFrame.Angles(math.random()*Spread,0,0)).lookVector
					local hitObject, bulletPos = RayCast(Handle.Position, shootDirection, Range)
					local bullet
					-- Create a bullet here
					if hitObject then
						bullet = CreateBullet(bulletPos)
					end
					if hitObject and hitObject.Parent then
						local hitHumanoid = hitObject.Parent:FindFirstChild("Humanoid")
						if hitHumanoid then
							local hitPlayer = game.Players:GetPlayerFromCharacter(hitHumanoid.Parent)
							if MyPlayer.Neutral or (hitPlayer and hitPlayer.TeamColor ~= MyPlayer.TeamColor) then
								TagHumanoid(hitHumanoid, MyPlayer)
								hitHumanoid:TakeDamage(Damage)
								if bullet then
									bullet:Destroy()
									bullet = nil
									--bullet.Transparency = 1
								end
							end
						end
					end
				end

				local casing = CasingBase:Clone()
				casing.Position = Tool.Handle.Position + Vector3.new(0,1,0)
				casing.Velocity = (Vector3.new((math.random()-.5),(.5+math.random()),(math.random()-.5)) - 1.5*(Tool.Handle.CFrame * CFrame.Angles(0,math.pi/4,0)).lookVector)*20
				DebrisService:AddItem(casing, 2.5)
				casing.Parent = game.Workspace
	
				AmmoInClip = AmmoInClip - 1
				UpdateAmmo(AmmoInClip)
			end
			wait(FireRate-.2)

			if AmmoInClip > 0 then
				if PumpTrack then
					PumpTrack:Play()
				end
				if Handle:FindFirstChild('PumpSound') then
					Handle.PumpSound:Play()
				end
				wait(.2)
			end
			MyMouse.Icon = "rbxasset://textures\\GunCursor.png"
		end		
		IsShooting = false
		if AmmoInClip == 0 then
			Reload()
		end
		if RecoilTrack then
			RecoilTrack:Stop()
		end
	end
end

function UpdateAmmo(value)
	if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('ClipAmmo') then
		WeaponGui.AmmoHud.ClipAmmo.Text = AmmoInClip
	end
	--if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('TotalAmmo') then
	--	WeaponGui.AmmoHud.TotalAmmo.Text = SpareAmmo
	--end
end


function OnMouseDown()
	LeftButtonDown = true
	OnFire()
end

function OnMouseUp()
	LeftButtonDown = false
end

function OnKeyDown(key)
	if string.lower(key) == 'r' then
		Reload()
	end
end


function OnEquipped(mouse)
	EquipAnim = WaitForChild(Tool, 'Equip')
	PumpAnim = WaitForChild(Tool, 'Reload')
	FireSound  = WaitForChild(Handle, 'FireSound')

	MyCharacter = Tool.Parent
	MyPlayer = game:GetService('Players'):GetPlayerFromCharacter(MyCharacter)
	MyHumanoid = MyCharacter:FindFirstChild('Humanoid')
	MyTorso = MyCharacter:FindFirstChild('Torso')
	MyMouse = mouse
	WeaponGui = WaitForChild(Tool, 'WeaponHud'):Clone()
	if WeaponGui and MyPlayer then
		WeaponGui.Parent = MyPlayer.PlayerGui
		UpdateAmmo(AmmoInClip)
	end
	if EquipAnim then
		Tool.Grip = CFrame.new(0.132808685, 0.00749024749, 0.526850462, 0.925221086, 0.000443253666, 0.379427731, 0.00859767944, 0.999718785, -0.0221330915, -0.379331142, 0.0237402022, 0.924956799)
		EquipTrack = MyHumanoid:LoadAnimation(EquipAnim)
		EquipTrack:Play()
	end
	if PumpAnim then
		PumpTrack = MyHumanoid:LoadAnimation(PumpAnim)
	end

	if MyMouse then

		MyMouse.Icon = "rbxasset://textures\\GunCursor.png"
		-- Disable mouse icon
		--MyMouse.Icon = "http://www.roblox.com/asset/?id=18662154"
		MyMouse.Button1Down:connect(OnMouseDown)
		MyMouse.Button1Up:connect(OnMouseUp)
		MyMouse.KeyDown:connect(OnKeyDown)
	end
end

