--Align Orientations

local bodyRotate = myRoot.AlignOrientation

local xAxisAttach = Instance.new("Attachment")
xAxisAttach.Parent = workspace.Terrain
xAxisAttach.CFrame = myRoot.CFrame

local yAxisAttach = Instance.new("Attachment")
yAxisAttach.Parent = workspace.Terrain
yAxisAttach.CFrame = myRoot.CFrame

bodyRotate.Attachment1 = yAxisAttach


local m4Hinge = m4.HingeConstraint
local m4HingeAttach = m4.HingeAttach1
local torsoHingeAttach = myTorso.HingeAttach0
local torsoHingAttachOrgPos = torsoHingeAttach.Position
local headHinge = myHead.HingeConstraint

local reloadAnimation = myHuman:LoadAnimation(marine.Reload)
reloadAnimation.Priority = Enum.AnimationPriority.Action

function actions.aim()
	
	local aimPos = Vector3.new()
	local sightIndex = 50
	
	status:set("weaponAimed",true)
	myHuman.AutoRotate = false
	bodyRotate.Enabled = true
	myHuman.WalkSpeed = 12
	
	while true do
		
		local target = status:get("currentTarget")

		if myHuman.Health <=0 or not target or not target.Parent then
			break
		elseif not status:get("m4Equipped") and not status:get("reloading") then
			break
		end

		local target = status:get("currentTarget")
		local dist = core.checkDist(target,myTorso)
		local canSee = core.checkSight(target)
		
		if not canSee then
			sightIndex = sightIndex - 1
		else
			sightIndex = 50
		end
		
		if not canSee and myRoot.Velocity.Magnitude > 3 and sightIndex <= 0 then
			
			local walkPoint = myHuman.WalkToPoint
			local myPos = myRoot.Position
			local dir = (Vector3.new(walkPoint.X,0,walkPoint.Z) - Vector3.new(myPos.X,0,myPos.Z)).Unit * 20
			aimPos = myRoot.Position + Vector3.new(0,1.5,0) + dir
		else
			aimPos = target.Position + target.Velocity/(10 - dist/50)
		end
		
		local tilt = (myRoot.Position.Y - aimPos.Y) / (dist*0.04)
		tilt = math.clamp(tilt,-45,45)
		rootJoint.C0 = rootJointOrg * CFrame.fromEulerAnglesYXZ(math.rad(tilt),0,0)
		lHip.C0 = lHipOrg * CFrame.fromEulerAnglesYXZ(0,0,math.rad(-tilt))
		rHip.C0 = rHipOrg * CFrame.fromEulerAnglesYXZ(0,0,math.rad(tilt))
		
		xAxisAttach.WorldCFrame = CFrame.lookAt(m4.Position,aimPos)
		m4.HingeConstraint.Attachment0.WorldCFrame = CFrame.new(m4.HingeConstraint.Attachment0.WorldPosition) * CFrame.fromEulerAnglesYXZ(math.rad(0),math.rad(myRoot.Orientation.Y),0)
		m4.HingeConstraint.TargetAngle = xAxisAttach.Orientation.X
		headHinge.TargetAngle = xAxisAttach.Orientation.X
		
		torsoHingeAttach.Position = torsoHingAttachOrgPos + Vector3.new(0,tilt/90,0)

		yAxisAttach.WorldCFrame = CFrame.lookAt(m4.Position,Vector3.new(
			aimPos.X,
			myRoot.Position.Y,
			aimPos.Z
			))

		if status:get("m4Equipped") then
			neck.C0 = CFrame.new(0,1,0) * CFrame.Angles(-1.5 + math.rad(xAxisAttach.Orientation.X),math.rad(15),math.rad(180))
		else
			neck.C0 = CFrame.new(0,1,0) * CFrame.Angles(-1.5 + math.rad(xAxisAttach.Orientation.X),0,math.rad(180))
		end

		runService.Heartbeat:Wait()
		runService.Heartbeat:Wait()
	end
	
	
	actions.resetHead()
	bodyRotate.Enabled = false
	myHuman.WalkSpeed = 16
	myHuman.AutoRotate = true
	
	rootJoint.C0 = rootJointOrg
	lHip.C0 = lHipOrg
	rHip.C0 = rHipOrg
	
	status:set("weaponAimed",false)
end

function actions.reload()
	status:set("weaponAimed",false)
	reloadSound:Play()
	status:set("reloading",true)
	
	actions.yieldM4()
	script.Parent.Parent.Parent["Right Arm"].Gun.Transparency = 0

	m4Weld.Part0 = nil
	m4.CFrame = lArm.CFrame * CFrame.new(0.5,-0.2,0) * CFrame.Angles(math.rad(-90),math.rad(0),math.rad(0))
	m4Weld.Part0 = lArm
	
	reloadAnimation:Play()
	reloadAnimation:AdjustSpeed(3)
	
	wait(0.2)
	
	local fakeMag = magazine:Clone()
	fakeMag.CanCollide = true
	fakeMag.Transparency = 0
	fakeMag.Parent = workspace
	debrisService:AddItem(fakeMag,4)
	magazine.Transparency = 1
	
	reloadAnimation.Stopped:Wait()

	magazine.Transparency = 1
	status:set("reloading",false)
	status:set("mag",marine.Settings.MagSize.Value)
	actions.drawM4()
end

function actions.drawM4()
	if not status:get("m4Equipped") and not status:get("reloading") then
		status:set("m4Equipped",true)
		status:set("m4Lowered",false)
		script.Parent.Parent.Parent["Right Arm"].Gun.Transparency = 0
		m4.Equip:Play()
		
		xAxisAttach.CFrame = myTorso.CFrame
		
		--M4 Setup
		m4Weld.Part0 = nil
		m4.CFrame = myRoot.CFrame * CFrame.new(0.65,-1.6,0) * CFrame.Angles(math.rad(-90),math.rad(0),math.rad(0))
		m4HingeAttach.WorldPosition = torsoHingeAttach.WorldPosition		
		m4Hinge.Enabled = true
		
		--Right Arm Setup
		rShoulder.Part1 = nil
		rArmWeld.Part1 = nil --x 1.25
		rArmWeld.Enabled = false
		rArm.CFrame = m4.CFrame * CFrame.new(0.7,-0.4,1.5) * CFrame.Angles(math.rad(80),math.rad(0),math.rad(-10))
		rArmWeld.Part1 = rArm
		rArmWeld.Enabled = true
		
		--Left Arm Setup 
		lShoulder.Part1 = nil
		lArmWeld.Part1 = nil
		lArm.CFrame = m4.CFrame * CFrame.new(-1,-0.3,0.8) * CFrame.Angles(math.rad(90),math.rad(-3),math.rad(28)) --x84
		lArmWeld.Part1 = lArm
		
		wait(0.5)
	end
end



function actions.yieldM4()
	if status:get("m4Equipped") or status:get("m4Lowered") then

		status:set("m4Equipped",false)
		status:set("weaponAimed",false)
		status:set("m4Lowered",false)
		script.Parent.Parent.Parent["Right Arm"].Gun.Transparency = 1
		m4.Equip:Play()
		
		--Right Arm setup
		rArmWeld.Part1 = nil
		rShoulder.Part1 = rArm
		
		--Left Arm Setup
		lArmWeld.Part1 = nil
		lShoulder.Part1 = lArm
		
		--M4 Setup
		m4Weld.Part0 = nil
		m4Hinge.Enabled = false
		m4.CFrame = myTorso.CFrame * CFrame.new(0,0,0.5) * CFrame.Angles(math.rad(-90),math.rad(45),math.rad(-270))
		m4Weld.Part0 = myTorso
	end
end

function actions.lowerM4()
	if not status:get("m4Lowered") and status:get("m4Equipped") and not status:get("reloading") then
		
		status:set("m4Equipped",false)
		status:set("m4Lowered",true)
		
		m4.Equip:Play()
		
		--M4 Setup
		m4Weld.Part0 = nil
		m4.CFrame = myRoot.CFrame * CFrame.new(0.65,-1.6,0) * CFrame.Angles(math.rad(-90),math.rad(0),math.rad(0))
		m4HingeAttach.WorldPosition = torsoHingeAttach.WorldPosition		
		m4Hinge.Enabled = true
		
		--Right Arm Setup
		rShoulder.Part1 = nil
		rArmWeld.Part1 = nil --x 1.25
		rArmWeld.Enabled = false
		rArm.CFrame = m4.CFrame * CFrame.new(0.7,-0.4,1.5) * CFrame.Angles(math.rad(80),math.rad(0),math.rad(-10))
		rArmWeld.Part1 = rArm
		rArmWeld.Enabled = true
		
		--Left Arm Setup 
		lShoulder.Part1 = nil
		lArmWeld.Part1 = nil
		lArm.CFrame = m4.CFrame * CFrame.new(-1,-0.3,0.8) * CFrame.Angles(math.rad(90),math.rad(-3),math.rad(28)) --x84
		lArmWeld.Part1 = lArm
		
		wait(0.5)
	end
end



function actions.yieldWeapons()
	actions.yieldM4()
end

function actions.resetHead()
	tweenService:Create(neck,TweenInfo.new(0.5),{C0 = CFrame.new(0,1,0) * CFrame.Angles(math.rad(-90),0,math.rad(180))}):Play()
end

local faces = myHead.Faces
function actions.updateFace(newStatus,recovered)
	if status:get("mood") ~= "Dead" then
		if status:get("mood") ~= "Hurt" or recovered or newStatus == "Dead" then
			local currentFace = status:get("currentFace")
			currentFace.Parent = faces 
			status:set("currentFace",faces["face"..newStatus])
			status:get("currentFace").Parent = myHead
		end
		status:set("mood",newStatus)
	end
end


return actions
