using System;
using System.Collections;
using DarkTonic.MasterAudio;
using Pathfinding;
using UnityEngine;

// Token: 0x02000020 RID: 32
public class CollisionListener : MonoBehaviour
{
	// Token: 0x06000490 RID: 1168 RVA: 0x000674F0 File Offset: 0x000656F0
	public void StartLisening(string t)
	{
		if (this.turnOff != null)
		{
			this.EndNow();
			base.StopCoroutine(this.turnOff);
			this.turnOff = null;
		}
		this.type = t;
		base.transform.rotation = Quaternion.identity;
		this.rbody.isKinematic = false;
		this.timeSinceCollision = 0f;
		this.bounced = 0;
		this.listen = true;
		this.tr = base.transform;
		this.dynamicCollide = false;
		this.underground = false;
		if (this.c)
		{
			this.safePosition = this.c.tr.position;
			this.startPoint = this.c.tr.position;
		}
	}

	// Token: 0x06000491 RID: 1169 RVA: 0x000675B0 File Offset: 0x000657B0
	private void Update()
	{
		RaycastHit raycastHit;
		if (this.listen && !this.rbody.isKinematic && !this.dynamicCollide && this.c && Physics.Raycast(this.tr.position + new Vector3(0f, 30f, 0f), Vector3.up * -1f, out raycastHit, 200f, 4194305))
		{
			if (this.tr.position.y < raycastHit.point.y)
			{
				this.safePosition = raycastHit.point;
				this.underground = true;
				return;
			}
			if (!this.dynamicCollide)
			{
				Vector3 position = this.tr.position;
				bool flag = Physics.Linecast(position + new Vector3(0f, 20f, 0f), raycastHit.point, 144);
				bool flag2 = false;
				if (!flag)
				{
					flag2 = Physics.Raycast(position + new Vector3(0f, 400f, 0f), Vector3.up * -1f, 1000f, 768);
				}
				RaycastHit raycastHit2;
				if ((flag || flag2) && !this.dynamicCollide && Physics.SphereCast(this.c.tr.position + new Vector3(0f, 20f, 0f), 1.25f, Vector3.up * -1f, out raycastHit2, 200f, 4194305))
				{
					if (this.c.node != null)
					{
						this.c.setArea = (int)this.c.node.Area;
					}
					GraphNode graphNode = this.c.NearNode(raycastHit2.point, false, 0, -1, 0);
					if (graphNode != null)
					{
						this.safePosition = (Vector3)graphNode.position;
						this.c.ClaimNode(graphNode, this.safePosition, false);
					}
					else if (this.c.node != null)
					{
						this.safePosition = (Vector3)graphNode.position;
					}
					else
					{
						this.safePosition = this.startPoint;
					}
					this.dynamicCollide = true;
				}
			}
		}
	}

	// Token: 0x06000492 RID: 1170 RVA: 0x000677F4 File Offset: 0x000659F4
	private void FixedUpdate()
	{
		if (this.listen && !this.rbody.isKinematic)
		{
			if (this.body)
			{
				this.body.tr.position = this.tr.position - new Vector3(0f, 0.2f, 0f);
			}
			if (this.c)
			{
				if (!this.c.jumpingPosition && !this.c.jumpingAreas)
				{
					if (this.underground)
					{
						if (Mathf.Abs(this.tr.position.y - this.safePosition.y) > 0.5f)
						{
							Vector3 vector = Vector3.Lerp(this.c.tr.position, this.safePosition, Time.fixedDeltaTime * 5f);
							this.c.SetPosition(vector, null);
							return;
						}
					}
					else
					{
						if (this.dynamicCollide)
						{
							Vector3 vector2 = Vector3.Lerp(this.c.tr.position, this.safePosition, Time.fixedDeltaTime * 7f);
							this.c.SetPosition(vector2, null);
							return;
						}
						this.c.SetPosition(this.tr.position - new Vector3(0f, 0.4f, 0f), null);
						return;
					}
				}
				else
				{
					this.End();
				}
			}
		}
	}

	// Token: 0x06000493 RID: 1171 RVA: 0x0006796C File Offset: 0x00065B6C
	private void OnCollisionEnter(Collision collision)
	{
		if ((collision.gameObject.layer == 0 || collision.gameObject.layer == 22) && this.type == "Hit" && (Time.timeSinceLevelLoad > this.timeSinceCollision + 0.1f || this.timeSinceCollision == 0f))
		{
			if (this.bounced < 2)
			{
				this.HitFX();
			}
			this.timeSinceCollision = Time.timeSinceLevelLoad;
			this.bounced++;
			if (this.bounced >= this.bounceMax)
			{
				this.rbody.isKinematic = true;
			}
		}
	}

	// Token: 0x06000494 RID: 1172 RVA: 0x00067A0C File Offset: 0x00065C0C
	private void HitFX()
	{
		MasterAudio.PlaySound3DAtVector3AndForget("Body Fall", base.gameObject.transform.position, Random.Range(0.5f, 0.6f), new float?(Random.Range(0.95f, 1f)), 0f, "", null);
		if (this.c && !this.c.dead)
		{
			GameObject pooledGameObject = Links.x.cellar.GetPooledGameObject(129);
			RaycastHit raycastHit;
			if (Physics.Raycast(this.c.tr.position + new Vector3(0f, 3f, 0f), Vector3.up * -1f, out raycastHit, 10f, 4194305))
			{
				pooledGameObject.transform.rotation = Quaternion.Euler(Quaternion.FromToRotation(Vector3.up, raycastHit.normal).eulerAngles);
			}
			pooledGameObject.transform.position = this.c.tr.position + new Vector3(0f, 0.1f, 0f);
			pooledGameObject.SetActive(true);
		}
	}

	// Token: 0x06000495 RID: 1173 RVA: 0x00067B4E File Offset: 0x00065D4E
	public void End()
	{
		this.listen = false;
		this.turnOff = this.Ending();
		if (!base.gameObject.activeSelf)
		{
			base.gameObject.SetActive(true);
		}
		base.StartCoroutine(this.turnOff);
	}

	// Token: 0x06000496 RID: 1174 RVA: 0x00067B89 File Offset: 0x00065D89
	public void TurnOff()
	{
		this.HitFX();
		base.gameObject.SetActive(false);
	}

	// Token: 0x06000497 RID: 1175 RVA: 0x00067B9D File Offset: 0x00065D9D
	public void EndNow()
	{
		this.ReturnObjs();
	}

	// Token: 0x06000498 RID: 1176 RVA: 0x00067BA5 File Offset: 0x00065DA5
	private IEnumerator Ending()
	{
		this.rbody.isKinematic = true;
		yield return new WaitForSeconds(1f);
		this.ReturnObjs();
		base.gameObject.SetActive(false);
		yield break;
	}

	// Token: 0x06000499 RID: 1177 RVA: 0x00067BB4 File Offset: 0x00065DB4
	private void ReturnObjs()
	{
		if (this.pooledObject)
		{
			Links.x.cellar.ReturnPooledGameObject(this.pooledNumber, this.pooledObject);
		}
	}

	// Token: 0x04000755 RID: 1877
	public string type;

	// Token: 0x04000756 RID: 1878
	private IEnumerator turnOff;

	// Token: 0x04000757 RID: 1879
	private GameObject pooledObject;

	// Token: 0x04000758 RID: 1880
	private int pooledNumber;

	// Token: 0x04000759 RID: 1881
	public Rigidbody rbody;

	// Token: 0x0400075A RID: 1882
	private float timeSinceCollision;

	// Token: 0x0400075B RID: 1883
	public Character c;

	// Token: 0x0400075C RID: 1884
	public int bounceMax = 3;

	// Token: 0x0400075D RID: 1885
	private int bounced;

	// Token: 0x0400075E RID: 1886
	public bool listen;

	// Token: 0x0400075F RID: 1887
	private Transform tr;

	// Token: 0x04000760 RID: 1888
	public Body body;

	// Token: 0x04000761 RID: 1889
	private Vector3 safePosition;

	// Token: 0x04000762 RID: 1890
	private bool dynamicCollide;

	// Token: 0x04000763 RID: 1891
	private bool underground;

	// Token: 0x04000764 RID: 1892
	private Vector3 startPoint;
}
