using System;
using System.Collections.Generic;
using UnityEngine;

// Token: 0x0200004B RID: 75
public class Rope : MonoBehaviour
{
	// Token: 0x060008CB RID: 2251 RVA: 0x000BED5C File Offset: 0x000BCF5C
	private void OnEnable()
	{
		if (!this.line)
		{
			this.line = base.gameObject.transform.GetChild(0).gameObject.GetComponent<LineRenderer>();
		}
		if (!this.end)
		{
			this.meshes.Clear();
			foreach (object obj in base.transform.GetChild(0))
			{
				Transform transform = (Transform)obj;
				if (transform.gameObject.name.Contains("End") || transform.gameObject.name.Contains("end"))
				{
					this.end = transform;
				}
				if (transform.gameObject.name.Contains("Start") || transform.gameObject.name.Contains("start"))
				{
					this.start = transform;
				}
				MeshRenderer component = transform.gameObject.GetComponent<MeshRenderer>();
				if (component)
				{
					this.meshes.Add(component);
				}
			}
		}
		if (!this.end || !this.start)
		{
			base.enabled = false;
			this.on = false;
			this.line.enabled = false;
			return;
		}
		this.on = false;
		this.line.enabled = false;
	}

	// Token: 0x060008CC RID: 2252 RVA: 0x000BEED8 File Offset: 0x000BD0D8
	private void OnDisable()
	{
		this.canUpdate = false;
		this.character = null;
	}

	// Token: 0x060008CD RID: 2253 RVA: 0x000BEEE8 File Offset: 0x000BD0E8
	public void SetupCharacter(Character c, float leashDistance)
	{
		this.character = c;
		this.neck = this.character.body.GetFoot();
		this.character.leashed = true;
		this.character.leashDistance = leashDistance;
		this.character.leashCenter = this.start.position;
		this.canUpdate = true;
	}

	// Token: 0x060008CE RID: 2254 RVA: 0x000BEF48 File Offset: 0x000BD148
	private void LateUpdate()
	{
		if (this.canUpdate)
		{
			if (!this.character)
			{
				base.enabled = false;
				this.line.enabled = false;
				this.on = false;
				return;
			}
			if (this.character.visible == 0 || !this.character.leashed || this.character.inactive)
			{
				if (!this.on)
				{
					goto IL_00FB;
				}
				this.line.enabled = false;
				this.on = false;
				using (List<MeshRenderer>.Enumerator enumerator = this.meshes.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						MeshRenderer meshRenderer = enumerator.Current;
						meshRenderer.enabled = false;
					}
					goto IL_00FB;
				}
			}
			if (!this.on)
			{
				this.on = true;
				this.line.enabled = true;
				foreach (MeshRenderer meshRenderer2 in this.meshes)
				{
					meshRenderer2.enabled = true;
				}
			}
			IL_00FB:
			if (this.on)
			{
				this.end.position = this.neck.position;
				this.line.positionCount = 2;
				this.line.SetPosition(0, this.start.position);
				this.line.SetPosition(1, this.end.position);
			}
		}
	}

	// Token: 0x060008CF RID: 2255 RVA: 0x000BF0C4 File Offset: 0x000BD2C4
	public void DrawArc(Vector3 arcEndPosition)
	{
		this.arcTarget = arcEndPosition;
		this.arcPoints.Clear();
		Vector3 position = this.start.position;
		this.initialVelocity = 1f;
		Quaternion quaternion = this.GetBearing(position, this.arcTarget);
		this.start.rotation = quaternion;
		this.CalculateTrajectory();
	}

	// Token: 0x060008D0 RID: 2256 RVA: 0x000BF11C File Offset: 0x000BD31C
	public Quaternion GetBearing(Vector3 start, Vector3 targetPt)
	{
		this.bearing = Quaternion.Euler(Vector3.zero);
		Vector3 vector = targetPt - start;
		float num = Mathf.Sqrt(Mathf.Pow(vector.x, 2f) + Mathf.Pow(vector.z, 2f));
		float num2 = -vector.y;
		float num3 = this.gravity * (this.gravity * Mathf.Pow(num, 2f) + 2f * num2 * Mathf.Pow(this.initialVelocity, 2f));
		float num4 = Mathf.Sqrt(Mathf.Pow(this.initialVelocity, 4f) - num3);
		bool flag = float.IsNaN(num4);
		if (!flag)
		{
			float num5 = (Mathf.Pow(this.initialVelocity, 2f) + (float)this.trajectoryModifier * num4) / (this.gravity * num);
			float num6 = 57.29578f * Mathf.Atan(num5);
			this.bearing = Quaternion.LookRotation(vector);
			Vector3 eulerAngles = this.bearing.eulerAngles;
			eulerAngles.x = num6;
			this.bearing.eulerAngles = eulerAngles;
		}
		if (flag)
		{
			this.initialVelocity += 1f;
			this.GetBearing(start, targetPt);
		}
		return this.bearing;
	}

	// Token: 0x060008D1 RID: 2257 RVA: 0x000BF250 File Offset: 0x000BD450
	public void CalculateTrajectory()
	{
		RaycastHit raycastHit = default(RaycastHit);
		this.line.positionCount = this.maximumIterations;
		Vector3 position = this.start.position;
		float num = -(1f / this.trajectoryPrecision);
		bool flag = false;
		Vector3 position2 = this.start.position;
		for (int i = 0; i < this.maximumIterations; i++)
		{
			if (!flag)
			{
				num += 1f / this.trajectoryPrecision;
				Vector3 vector = position + this.start.forward * this.initialVelocity * num + 0.5f * this.gravity2 * Mathf.Pow(num, 2f);
				float num2 = vector.y - position2.y;
				vector.y = position2.y - num2;
				Vector3 vector2 = vector;
				if (Physics.Raycast(new Vector3(vector.x, position2.y, vector.z), Vector3.up * -1f, out raycastHit, 5f, 4194449))
				{
					Vector3 point = raycastHit.point;
					if (point.y > vector2.y)
					{
						vector2.y = point.y;
					}
				}
				if ((vector - this.arcTarget).sqrMagnitude < 1f || vector2.y > position2.y)
				{
					flag = true;
					this.line.SetPosition(i, this.end.position);
					this.line.positionCount = i + 1;
				}
				else
				{
					this.line.SetPosition(i, vector2);
				}
				this.arcPoints.Add(vector);
			}
		}
		this.arcPoints.Add(this.arcTarget);
	}

	// Token: 0x04000DEA RID: 3562
	private LineRenderer line;

	// Token: 0x04000DEB RID: 3563
	public Transform end;

	// Token: 0x04000DEC RID: 3564
	public Transform start;

	// Token: 0x04000DED RID: 3565
	public Character character;

	// Token: 0x04000DEE RID: 3566
	private bool on;

	// Token: 0x04000DEF RID: 3567
	private bool canUpdate;

	// Token: 0x04000DF0 RID: 3568
	private Transform neck;

	// Token: 0x04000DF1 RID: 3569
	private List<MeshRenderer> meshes = new List<MeshRenderer>();

	// Token: 0x04000DF2 RID: 3570
	private float gravity = -9.81f;

	// Token: 0x04000DF3 RID: 3571
	public float initialVelocity = 1f;

	// Token: 0x04000DF4 RID: 3572
	private Quaternion bearing;

	// Token: 0x04000DF5 RID: 3573
	public int trajectoryModifier = -1;

	// Token: 0x04000DF6 RID: 3574
	private Vector3 projectileVelocity;

	// Token: 0x04000DF7 RID: 3575
	private Vector3 arcTarget;

	// Token: 0x04000DF8 RID: 3576
	public float trajectoryPrecision = 100f;

	// Token: 0x04000DF9 RID: 3577
	public int maximumIterations = 500;

	// Token: 0x04000DFA RID: 3578
	public Vector3 gravity2 = new Vector3(0f, -9.81f, 0f);

	// Token: 0x04000DFB RID: 3579
	private List<Vector3> arcPoints = new List<Vector3>();

	// Token: 0x04000DFC RID: 3580
	private int currentArcIndex;

	// Token: 0x04000DFD RID: 3581
	private Vector3 previousPosition;
}
