--------------------------------------------------------------------------------------
--------------------[ ANIMATIONS ]----------------------------------------------------
--------------------------------------------------------------------------------------



local AnimAng = {0, 0, 0, 0, 0}

local AnimCF = function(State, Ang)
	if State ~= "Running" then
		if (not Aimed) then
			if State == "Idleing" then
				return CF(
					RAD(SIN(Ang)) / 2 * StanceSway,
					1 + RAD(SIN(Ang * 5 / 2)) / 2 * StanceSway, 0
				)
			elseif State == "Walking" then
				return CF(
					RAD(SIN(Ang)) * 2 * StanceSway,
					1 + RAD(ABS(SIN(Ang))) * 2 * StanceSway, 0
				) * CFANG(0, RAD(SIN(Ang)) / 3, 0)
			end
		elseif Aimed then
			if State == "Idleing" then
				return CF(
					RAD(SIN(Ang)) / 4 * StanceSway,
					1 + RAD(SIN(Ang * 5 / 2)) / 4 * StanceSway, 0
				)
			elseif State == "Walking" then
				return CF(
					RAD(SIN(Ang)) / 3 * StanceSway,
					1 + RAD(ABS(SIN(Ang))) / 3 * StanceSway, 0
				)
			end
		end
	elseif State == "Running" then
		return CF(
			SIN(Ang) / 6 * StanceSway,
			0.9 + ABS(SIN(Ang)) / 5 * StanceSway, 0
		) * CFANG(0, -RAD(SIN(Ang)) * 7, 0)
	end
end

function Animate()
	local IsIdleing = false
	local IsWalking = false
	local IsRunning = false
	spawn(function()
		while Selected do
			IsIdleing = Idleing and (not Walking) and (not Reloading) and (not Knifing) and (not ThrowingGrenade) and Selected
			IsWalking = (not Idleing) and Walking and (not Running) and (not Reloading) and (not Knifing) and (not ThrowingGrenade) and Selected
			IsRunning = (not Idleing) and Walking and Running and (not Aiming) and (not Knifing) and (not ThrowingGrenade) and Selected
			RS:wait()
		end
		IsIdleing = false
		IsWalking = false
		IsRunning = false
	end)
	spawn(function()
		if S.PlayerAnimations then
			TweenJoint(LWeld2, CF(), CFANG(0, RAD(ArmTilt), 0), Sine, 0.15)
			TweenJoint(RWeld2, CF(), CFANG(0, RAD(ArmTilt), 0), Sine, 0.15)
			local PreviousArmTilt = ArmTilt
			while Selected do
				repeat RS:wait() until (ArmTilt ~= PreviousArmTilt) or (not Selected)
				if (not IsRunning) and (not Aimed) and (not Reloading) and (not Knifing) and (not ThrowingGrenade) and Selected then
					PreviousArmTilt = ArmTilt
					TweenJoint(LWeld2, CF(), CFANG(0, RAD(ArmTilt), 0), Sine, 0.15)
					TweenJoint(RWeld2, CF(), CFANG(0, RAD(ArmTilt), 0), Sine, 0.15)
				end
				RS:wait()
			end
		end
	end)
	spawn(function()
		while Selected do
			if IsIdleing then
				if (not Aimed) and (not Aiming) then
					Gui_Clone.CrossHair.Box:TweenSizeAndPosition(
						UDim2.new(0, 70, 0, 70),
						UDim2.new(0, -35, 0, -35),
						Enum.EasingDirection.Out,
						Enum.EasingStyle.Linear,
						S.PlayerAnimations and 0.15 or 0,
						true
					)
					if S.PlayerAnimations then
						TweenJoint(LWeld, ArmC0[1], S.ArmC1_UnAimed.Left, Sine, 0.15)
						TweenJoint(RWeld, ArmC0[2], S.ArmC1_UnAimed.Right, Sine, 0.15)
						TweenJoint(AnimWeld, AnimCF("Idleing", AnimAng[1]), CF(), Sine, 0.15)
						TweenJoint(Grip, Grip.C0, CFANG(0, RAD(20), 0), Sine, 0.15)
					else
						if (not LWeld:FindFirstChild("TweenCode"))
						and (not RWeld:FindFirstChild("TweenCode"))
						and (not ABWeld:FindFirstChild("TweenCode"))
						and (not AnimWeld:FindFirstChild("TweenCode")) then
							LWeld.C0, LWeld.C1 = ArmC0[1], S.ArmC1_UnAimed.Left
							RWeld.C0, RWeld.C1 = ArmC0[2], S.ArmC1_UnAimed.Right
							AnimWeld.C0 = CF(0, 1, 0)
							Grip.C1 = CFANG(0, RAD(20), 0)
						end
					end
				elseif Aimed and (not Aiming) then
					if S.PlayerAnimations then
						TweenJoint(LWeld, ArmC0[1], S.ArmC1_Aimed.Left, Sine, 0.15)
						TweenJoint(RWeld, ArmC0[2], S.ArmC1_Aimed.Right, Sine, 0.15)
						TweenJoint(AnimWeld, AnimCF("Idleing", AnimAng[2]), CF(), Sine, 0.15)
					else
						if (not LWeld:FindFirstChild("TweenCode"))
						and (not RWeld:FindFirstChild("TweenCode"))
						and (not ABWeld:FindFirstChild("TweenCode"))
						and (not AnimWeld:FindFirstChild("TweenCode")) then
							LWeld.C0, LWeld.C1 = ArmC0[1], S.ArmC1_Aimed.Left
							RWeld.C0, RWeld.C1 = ArmC0[2], S.ArmC1_Aimed.Right
							AnimWeld.C0 = CF(0, 1, 0)
							Grip.C1 = Aimed_GripCF
						end
					end
				end
				if S.PlayerAnimations then
					wait(0.15)
					RunTween = false
					while IsIdleing do
						if (not LWeld:FindFirstChild("TweenCode"))
						and (not RWeld:FindFirstChild("TweenCode"))
						and (not ABWeld:FindFirstChild("TweenCode"))
						and (not AnimWeld:FindFirstChild("TweenCode")) then
							if (not Aimed) and (not Aiming) then
								AnimWeld.C0 = AnimCF("Idleing", AnimAng[1])
								AnimAng[1] = AnimAng[1] + 0.03 * StanceSway
							elseif Aimed and (not Aiming) then
								AnimWeld.C0 = AnimCF("Idleing", AnimAng[2])
								AnimAng[2] = AnimAng[2] + 0.015 * StanceSway
							end
						end
						RS:wait()
					end
					AnimAng[1], AnimAng[2] = 0, 0
				end
			end
			if IsWalking then
				if (not Aimed) and (not Aiming) then
					Gui_Clone.CrossHair.Box:TweenSizeAndPosition(
						UDim2.new(0, 150, 0, 150),
						UDim2.new(0, -75, 0, -75),
						Enum.EasingDirection.Out,
						Enum.EasingStyle.Linear,
						S.PlayerAnimations and 0.15 or 0,
						true
					)
					if S.PlayerAnimations then
						TweenJoint(LWeld, ArmC0[1], S.ArmC1_UnAimed.Left, Sine, 0.15)
						TweenJoint(RWeld, ArmC0[2], S.ArmC1_UnAimed.Right, Sine, 0.15)
						TweenJoint(AnimWeld, AnimCF("Walking", AnimAng[3]), CF(), Sine, 0.15)
						TweenJoint(Grip, Grip.C0, CFANG(0, RAD(20), 0), Sine, 0.15)
					else
						if (not LWeld:FindFirstChild("TweenCode"))
						and (not RWeld:FindFirstChild("TweenCode"))
						and (not ABWeld:FindFirstChild("TweenCode"))
						and (not AnimWeld:FindFirstChild("TweenCode")) then
							LWeld.C0, LWeld.C1 = ArmC0[1], S.ArmC1_UnAimed.Left
							RWeld.C0, RWeld.C1 = ArmC0[2], S.ArmC1_UnAimed.Right
							AnimWeld.C0 = CF(0, 1, 0)
							Grip.C1 = CFANG(0, RAD(20), 0)
						end
					end
				elseif Aimed and (not Aiming) then
					if S.PlayerAnimations then
						TweenJoint(LWeld, ArmC0[1], S.ArmC1_Aimed.Left, Sine, 0.15)
						TweenJoint(RWeld, ArmC0[2], S.ArmC1_Aimed.Right, Sine, 0.15)
						TweenJoint(AnimWeld, AnimCF("Walking", AnimAng[4]), CF(), Sine, 0.15)
					else
						if (not LWeld:FindFirstChild("TweenCode"))
						and (not RWeld:FindFirstChild("TweenCode"))
						and (not ABWeld:FindFirstChild("TweenCode"))
						and (not AnimWeld:FindFirstChild("TweenCode")) then
							LWeld.C0, LWeld.C1 = ArmC0[1], S.ArmC1_Aimed.Left
							RWeld.C0, RWeld.C1 = ArmC0[2], S.ArmC1_Aimed.Right
							AnimWeld.C0 = CF(0, 1, 0)
							Grip.C1 = Aimed_GripCF
						end
					end
				end
				if S.PlayerAnimations then
					wait(0.15)
					RunTween = false
					while IsWalking do
						if (not LWeld:FindFirstChild("TweenCode"))
						and (not RWeld:FindFirstChild("TweenCode"))
						and (not ABWeld:FindFirstChild("TweenCode"))
						and (not AnimWeld:FindFirstChild("TweenCode"))then
							if (not Aimed) and (not Aiming) then
								AnimWeld.C0 = AnimCF("Walking", AnimAng[3])
								AnimAng[3] = AnimAng[3] + 0.15 * StanceSway
							elseif Aimed and (not Aiming) then
								AnimWeld.C0 = AnimCF("Walking", AnimAng[4])
								AnimAng[4] = AnimAng[4] + 0.1 * StanceSway
							end
						end
						RS:wait()
					end
					AnimAng[3], AnimAng[4] = 0, 0
				end
			end
			if IsRunning then
				Gui_Clone.CrossHair.Box:TweenSizeAndPosition(
					UDim2.new(0, 200, 0, 200),
					UDim2.new(0, -100, 0, -100),
					Enum.EasingDirection.Out,
					Enum.EasingStyle.Linear,
					S.PlayerAnimations and 0.15 or 0,
					true
				)
				local LArmCF = CF(0, 0.7 - (SIN(AnimAng[5]) + 1)/15, 0)
				local LArmAng = CFANG(RAD(ABS(COS(AnimAng[5]))) * 10 + RAD(35), RAD(-30), RAD((SIN(AnimAng[5]) + 1) * 15 - 40))
				local RArmCF = CF(0, (SIN(AnimAng[5]) + 1)/2, 0)
				local RArmAng = CFANG(RAD(ABS(COS(AnimAng[5]))) * 10 + RAD(20), 0, RAD(50 + (SIN(AnimAng[5]) + 1) * 5))
				if S.PlayerAnimations then
					TweenJoint(LWeld, ArmC0[1], LArmCF * LArmAng, Sine, 0.15)
					TweenJoint(RWeld, ArmC0[2], RArmCF * RArmAng, Sine, 0.15)
					TweenJoint(AnimWeld, AnimCF("Running", AnimAng[5]), CF(), Sine, 0.15)
					TweenJoint(Grip, Grip.C0, CFANG(0, RAD(-5), 0), Sine, 0.15)
				else
					LWeld.C0, LWeld.C1 = ArmC0[1], LArmCF * LArmAng
					RWeld.C0, RWeld.C1 = ArmC0[2], RArmCF * RArmAng
					AnimWeld.C0 = CF(0, 0.9, 0)
					Grip.C1 = CFANG(0, RAD(-5), 0)
				end
				if S.PlayerAnimations then
					RunTween = true
					wait(0.15)
					while IsRunning do
						if (not Aiming) then
							AnimWeld.C0 = AnimCF("Running", AnimAng[5])
							AnimAng[5] = AnimAng[5] + 0.18
						end
						RS:wait()
					end
					AnimAng[5] = 0
				end
			end
			RS:wait()
		end
	end)
end