using System;
using UnityEngine;

// Token: 0x0200001E RID: 30
[RequireComponent(typeof(Animator))]
public class CleanIK : MonoBehaviour
{
	// Token: 0x06000479 RID: 1145 RVA: 0x00066E5D File Offset: 0x0006505D
	private void Start()
	{
		this.animator = base.GetComponent<Animator>();
		this.layerMask = 4194433;
	}

	// Token: 0x0600047A RID: 1146 RVA: 0x00066E76 File Offset: 0x00065076
	private void Update()
	{
		this.handleColliderOffset();
	}

	// Token: 0x0600047B RID: 1147 RVA: 0x00066E80 File Offset: 0x00065080
	private void OnAnimatorIK()
	{
		if (this.animator && this.ikActive)
		{
			if (this.LeftFoot != null)
			{
				this.solveIK(this.LeftFoot, true);
			}
			if (this.RightFoot != null)
			{
				this.solveIK(this.RightFoot, false);
			}
		}
	}

	// Token: 0x0600047C RID: 1148 RVA: 0x00066ED8 File Offset: 0x000650D8
	private void solveIK(Transform foot, bool left)
	{
		string name = foot.name;
		RaycastHit raycastHit = default(RaycastHit);
		Vector3 vector = default(Vector3);
		Quaternion quaternion = Quaternion.identity;
		if (Physics.Linecast(this.checkOrigin(foot.position), this.checkTarget(foot.position), out raycastHit, this.layerMask))
		{
			vector = this.footPosition(raycastHit);
			quaternion = this.footRotation(foot, raycastHit);
			if (left)
			{
				this.animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1f);
				this.animator.SetIKPosition(AvatarIKGoal.LeftFoot, vector);
				this.LeftFootY = vector.y;
				if (this.rotation)
				{
					this.animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 1f);
					this.animator.SetIKRotation(AvatarIKGoal.LeftFoot, quaternion);
				}
			}
			if (!left)
			{
				this.animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1f);
				this.animator.SetIKPosition(AvatarIKGoal.RightFoot, vector);
				this.RightFootY = vector.y;
				if (this.rotation)
				{
					this.animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, 1f);
					this.animator.SetIKRotation(AvatarIKGoal.RightFoot, quaternion);
				}
			}
		}
	}

	// Token: 0x0600047D RID: 1149 RVA: 0x00066FE4 File Offset: 0x000651E4
	private void handleColliderOffset()
	{
		this.stateBasedLegDistance();
		if (!this.controller)
		{
			this.controller = base.gameObject.transform.parent.GetComponent<CharacterController>();
			if (this.controller)
			{
				this.colliderHeight = this.controller.height;
				this.controllerBoundsBottom = this.controller.bounds.extents.y;
			}
		}
		if (this.controller)
		{
			if (this.planeSpeed(ref this.controller) < 0.1f)
			{
				float num = Mathf.Abs(this.LeftFootY - this.RightFootY);
				this.controller.height = this.colliderHeight - num * this.deltaAmplifier;
				return;
			}
			this.controller.height = this.colliderHeight;
		}
	}

	// Token: 0x0600047E RID: 1150 RVA: 0x000670BA File Offset: 0x000652BA
	private void stateBasedLegDistance()
	{
		if (this.controller)
		{
			this.legDistance = 1f / (this.planeSpeed(ref this.controller) + 0.8f);
		}
	}

	// Token: 0x0600047F RID: 1151 RVA: 0x000670E8 File Offset: 0x000652E8
	private float planeSpeed(ref CharacterController characterController)
	{
		Vector3 vector = new Vector3(characterController.velocity.x, 0f, characterController.velocity.z);
		return vector.magnitude;
	}

	// Token: 0x06000480 RID: 1152 RVA: 0x00067120 File Offset: 0x00065320
	private Quaternion footRotation(Transform foot, RaycastHit hit)
	{
		return Quaternion.LookRotation(Vector3.ProjectOnPlane(base.transform.forward, hit.normal), hit.normal);
	}

	// Token: 0x06000481 RID: 1153 RVA: 0x00067148 File Offset: 0x00065348
	private Vector3 footPosition(RaycastHit hit)
	{
		Vector3 point = hit.point;
		point.y += this.footOffset;
		return point;
	}

	// Token: 0x06000482 RID: 1154 RVA: 0x0006716F File Offset: 0x0006536F
	private Vector3 checkOrigin(Vector3 footPosition)
	{
		return footPosition + (this.legDistance + 0.25f) * Vector3.up;
	}

	// Token: 0x06000483 RID: 1155 RVA: 0x0006718D File Offset: 0x0006538D
	private Vector3 checkTarget(Vector3 footPosition)
	{
		return footPosition - this.legDistance / 2f * Vector3.up;
	}

	// Token: 0x0400073E RID: 1854
	protected Animator animator;

	// Token: 0x0400073F RID: 1855
	public bool ikActive;

	// Token: 0x04000740 RID: 1856
	public bool rotation = true;

	// Token: 0x04000741 RID: 1857
	public bool showMarkers;

	// Token: 0x04000742 RID: 1858
	public Transform LeftFoot;

	// Token: 0x04000743 RID: 1859
	public Transform RightFoot;

	// Token: 0x04000744 RID: 1860
	public float footOffset;

	// Token: 0x04000745 RID: 1861
	private float legDistance;

	// Token: 0x04000746 RID: 1862
	private int layerMask = 256;

	// Token: 0x04000747 RID: 1863
	private CharacterController controller;

	// Token: 0x04000748 RID: 1864
	private float colliderHeight;

	// Token: 0x04000749 RID: 1865
	private float controllerBoundsBottom;

	// Token: 0x0400074A RID: 1866
	private float LeftFootY;

	// Token: 0x0400074B RID: 1867
	private float RightFootY;

	// Token: 0x0400074C RID: 1868
	public float smooth = 10f;

	// Token: 0x0400074D RID: 1869
	public float deltaAmplifier = 1f;

	// Token: 0x0400074E RID: 1870
	private Rigidbody rbody;
}
