-------------------------------------

wait(1)

local Arrow = Instance.new("Part")
Arrow.Locked = true
Arrow.BackSurface = 0
Arrow.BottomSurface = 0
Arrow.Name = "Bullet"
Arrow.FrontSurface = 0
Arrow.CanCollide = false
Arrow.LeftSurface = 0
Arrow.RightSurface = 0
Arrow.TopSurface = 0
Arrow.Shape = "Ball"
Arrow.formFactor = "Custom"
Arrow.Size = Vector3.new(2,2,2)
Arrow.BrickColor = BrickColor.new("Bright yellow")

script.Mesh:clone().Parent = Arrow

local force = Instance.new("BodyForce")
force.force = Vector3.new(0,Arrow:GetMass() * (236 / 1.2))
force.Parent = Arrow

sound = script.Parent.Handle.Shot
rightshoot = true
function fire(mouse_pos, VELOCITY)
	sound.Pitch = math.random(08, 15)/10
	sound:Play()
		head = game.Players.LocalPlayer.Character:findFirstChild("Right Arm")
		rightshoot = false
	
	if head == nil then return end

	local dir = mouse_pos - head.Position
	dir = computeDirection(dir)

	local launch = head.Position + 3 * dir

	local delta = mouse_pos - launch
	delta = delta / (delta.magnitude)

	local missile = Arrow:clone()
		
	missile.CFrame = CFrame.new(launch, launch + dir)

	missile.Velocity = delta * VELOCITY


	local creator_tag = Instance.new("ObjectValue")
	creator_tag.Value = game.Players.LocalPlayer
	creator_tag.Name = "creator"
	creator_tag.Parent = missile
	
	missile.Parent = game.Workspace

end

function computeDirection(vec)
	local lenSquared = vec.magnitude * vec.magnitude
	local invSqrt = 1 / math.sqrt(lenSquared)
	return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end

enabled = true
mousedown = false
function onButton1Down(mouse)
	if not enabled then return end
	local player = game.Players.LocalPlayer
	if player == nil then return end

	mousedown = true
	enabled = false

	mouse.Icon = "rbxasset://textures\\GunWaitCursor.png"

	-- find the best cf
	--local cf = mouse.Hit
	--local v = cf.lookVector
	
	while mousedown do
		if Tool.Cola.Value > 0 then 
		local pos = mouse.Hit.p + Vector3.new(math.random(-SPREAD,SPREAD),math.random(-SPREAD,SPREAD),math.random(-SPREAD,SPREAD))
		if player.Character.Humanoid.Health > 0 then
		fire(pos, math.random(MIN_VELOCITY, MAX_VELOCITY))
		end 
		Tool.Cola.Value = Tool.Cola.Value - 1
		end 
		wait(PULLBACK_TIME)
	end

	wait(COOLDOWN)
	mouse.Icon = "rbxasset://textures\\GunCursor.png"
	enabled = true
end

function onButton1Up(mouse)
	mousedown = false
end

function onUnequipped()
	mousedown = false
	Up()
end

function Changed()

	cola.Bar:TweenSize(UDim2.new((Tool.Cola.Value/Tool.Cola.MaxValue),0,1,0))
end 

function Down()
	if Tool.Jetpack.Value == true then 
	rdown = true 
	
	Tool.Handle.Rocket:play() 
	Tool.Handle.Smoke.Enabled = true 
	Tool.Handle.BV.maxForce = Vector3.new(0,400000,0)	
		while rdown == true do 
			wait(0.5) 
			Tool.Cola.Value = Tool.Cola.Value - 1 
			if rdown == false or Tool.Cola.Value == 0 then
				Up() 
				break
			end
		end
	end 
end

function Up()
	Tool.Handle.Rocket:stop()
	Tool.Handle.Smoke.Enabled = false 
	Tool.Handle.BV.maxForce = Vector3.new(0,00000,0)
	rdown = false
	
end

function onEquipped(mouse)
	 Changed()
	script.Parent.Handle.Equip:play()
	mouse.Icon = "rbxasset://textures\\GunCursor.png"
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
	mouse.Button2Down:connect(function() Down(mouse) end)
	mouse.Button2Up:connect(function() Up(mouse) end)
	mouse.Button1Up:connect(function() onButton1Up(mouse) end)
end

Tool.Cola.Changed:connect(Changed) 

script.Parent.Equipped:connect(onEquipped)
script.Parent.Unequipped:connect(onUnequipped)

