-- Helper functions

local function loadAnimation(controller, animationId)
	animation.AnimationId = "rbxassetid://" .. animationId
	return controller:LoadAnimation(animation)
end

local function isolateAndDampenYAngle(targetCFrame, proportionMultiplier)
	proportionMultiplier = proportionMultiplier or 1
	local _, y = targetCFrame:ToEulerAnglesXYZ()
	
	return CFrame.new(targetCFrame.Position) * CFrame.Angles(
		0,
		y * proportionMultiplier,
		0
	)
end

local function makeNPCLookAt(lookAtPosition)
	if not lookAtPosition then
		return
	end
	
	local differenceVector = lookAtPosition - npcModel.PrimaryPart.Position
	local targetCFrame = rotationOffset * CFrame.new(Vector3.new(), differenceVector.Unit) or rotationOffset
	
	-- Let the waving animation move the body
	if not waveAnimationTrack.IsPlaying then
		NPC.Root.Transform = isolateAndDampenYAngle(targetCFrame, TwistProportions.Root)
		NPC.Waist.Transform = isolateAndDampenYAngle(targetCFrame, TwistProportions.Waist)
	end
	NPC.Neck.Transform = isolateAndDampenYAngle(targetCFrame, waveAnimationTrack.IsPlaying and TwistProportions.Full or TwistProportions.Neck)
	
	return differenceVector.Magnitude
end

