--------------------------------------------------------------------------------------
--------------------[ 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)) * 3 * StanceSway,
					1 + RAD(ABS(SIN(Ang))) * -3 * StanceSway, RAD(ABS(SIN(Ang)))
				) * CFANG(0, RAD(SIN(Ang)) / 5, RAD(SIN(Ang)))
			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)) * 1.5 * StanceSway,
					1 + RAD(ABS(SIN(Ang))) * -1.5 * StanceSway, RAD(ABS(SIN(Ang)))
				) * CFANG(0, RAD(SIN(Ang)) / 5, RAD(SIN(Ang)))
			end
		end
	elseif State == "Running" then
		return CF(
			SIN(Ang) / 5.25 * StanceSway,
			0.775 + ABS(SIN(Ang)) / -14.5 * StanceSway, RAD(SIN(Ang)* -6.5 * StanceSway + -1.2)
		) * CFANG(RAD(SIN(Ang)* 4.5 * StanceSway), RAD(SIN(Ang)* -8.5 ), RAD(SIN(Ang)* 1 * StanceSway))
	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 Selected
			IsWalking = (not Idleing) and Walking and (not Running) and (not Reloading) and (not Knifing) and Selected
			IsRunning = (not Idleing) and Walking and Running and (not Aiming) and (not Knifing) 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
				if (not IsRunning) and (not Aimed) and (not Reloading) and (not Knifing) 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(-.2, .85 - (SIN(AnimAng[5]) + 1)/15, -0.6)
				local LArmAng = CFANG(RAD(ABS(COS(AnimAng[5]))) * 10 + RAD(15), RAD(-15), RAD(-15))
				local RArmCF = CF(0, (SIN(AnimAng[5]) + 1)/2, -0.5)
				local RArmAng = CFANG(RAD(ABS(COS(AnimAng[5]))) * 10 + RAD(10), 0, RAD(50 + (SIN(AnimAng[5]) + 1) * 5))
				if S.PlayerAnimations then
					Camera.FieldOfView = 90
					TweenJoint(LWeld, ArmC0[1], LArmCF * LArmAng, Sine, 1.15)
					TweenJoint(RWeld, ArmC0[2], RArmCF * RArmAng, Sine, 1.15)
					TweenJoint(AnimWeld, AnimCF("Running", AnimAng[5]), CF(), Sine, 0.15)
					TweenJoint(Grip, Grip.C0, CFANG(0, RAD(-5), 0), Sine, 1.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
					Camera.FieldOfView = 90	
					AnimAng[5] = 0
				end
			end
			RS:wait()
		end						
	end)
end