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

// Token: 0x02000023 RID: 35
public class CritterActions : MonoBehaviour
{
	// Token: 0x06000581 RID: 1409 RVA: 0x000795C9 File Offset: 0x000777C9
	private void Start()
	{
		this.GetVariables();
	}

	// Token: 0x06000582 RID: 1410 RVA: 0x000795D4 File Offset: 0x000777D4
	private void GetVariables()
	{
		this.tr = base.transform.parent.gameObject.transform;
		this.anim = base.gameObject.GetComponent<Animator>();
		this.seeker = base.gameObject.GetComponent<Seeker>();
		this.seeker.pathCallback = new OnPathDelegate(this.PathFinished);
	}

	// Token: 0x06000583 RID: 1411 RVA: 0x00079638 File Offset: 0x00077838
	public void MakeRandomPath(GraphNode startNode)
	{
		if (this.dead)
		{
			return;
		}
		if (!this.tr)
		{
			this.GetVariables();
		}
		this.nodeConstraint.constrainWalkability = true;
		this.nodeConstraint.walkable = true;
		this.nodeConstraint.constrainPenalty = 0;
		this.nodeConstraint.constrainTags = true;
		this.nodeConstraint.passID = 0;
		this.nodeConstraint.passID2 = 0;
		this.nodeConstraint.checkConnections = 0;
		this.nodeConstraint.constrainToEnvironment = 1;
		this.nodeConstraint.constrainToArea = -1;
		if (startNode == null)
		{
			if (this.resourceLocation == -1)
			{
				if (this.isResource)
				{
					startNode = AstarPath.active.GetNearest(this.resourcePoint, this.nodeConstraint).node;
				}
				else
				{
					startNode = Links.x.critters.GetRandomNode();
				}
			}
			else
			{
				this.isResource = true;
				startNode = AstarPath.active.GetNearest(this.resourcePoint, this.nodeConstraint).node;
			}
		}
		if (startNode != null && Links.x.gaia.sceneLoaded && Links.x.gaia.changingMapsComplete)
		{
			this.pathmaking = true;
			if ((this.tr.position - (Vector3)startNode.position).sqrMagnitude > 25f)
			{
				this.tr.position = (Vector3)startNode.position;
				this.Stop();
			}
			this.seeker.pathConstraint = this.nodeConstraint;
			if (Links.x.hasMain)
			{
				Vector3 position = Links.x.main.tr.position;
				this.seeker.StartRandomPath(startNode, 40000, new OnPathDelegate(this.PathFinished), 0.2f, 30000, position);
				return;
			}
			this.seeker.StartRandomPath(startNode, 40000, new OnPathDelegate(this.PathFinished), 0.2f, 30000, base.gameObject.transform.position);
		}
	}

	// Token: 0x06000584 RID: 1412 RVA: 0x00079846 File Offset: 0x00077A46
	public void Stop()
	{
		this.nodes.Clear();
		this.targetPosition = Vector3.zero;
	}

	// Token: 0x06000585 RID: 1413 RVA: 0x00079860 File Offset: 0x00077A60
	public void PathFinished(Path p)
	{
		p.Claim(this);
		if (!p.error && !this.dead)
		{
			this.nodes.Clear();
			this.pathVectors = p.vectorPath;
			this.pathNodes = p.path;
			for (int i = 0; i < this.pathVectors.Count; i++)
			{
				this.nodes.Add(this.pathVectors[i]);
			}
			this.lastNode = this.pathNodes[this.pathNodes.Count - 1];
			this.currentIndex = 0;
			this.t = 0f;
			this.startPosition = this.tr.position;
			if (this.currentIndex < this.nodes.Count && this.currentIndex > -1)
			{
				this.targetPosition = this.nodes[this.currentIndex];
				this.anim.Play("Move");
				this.anim.speed = 1.25f;
			}
			else
			{
				this.targetPosition = this.tr.position;
				this.Stop();
			}
		}
		else
		{
			this.tr.position = new Vector3(1000f, 0f, 0f);
			this.targetPosition = Vector3.zero;
		}
		this.pathmaking = false;
		p.Release(this, false);
	}

	// Token: 0x06000586 RID: 1414 RVA: 0x000799C4 File Offset: 0x00077BC4
	public void Squish(Character character)
	{
		if (this.dead)
		{
			return;
		}
		if (Time.timeSinceLevelLoad > this.spawnTime + 2f || !this.isResource)
		{
			this.targetPosition = Vector3.zero;
			this.currentIndex = 0;
			MasterAudio.PlaySound3DAtVector3AndForget("Squish", this.tr.position, 1f, new float?(1f), 0f, "", null);
			GameObject pooledGameObject = Links.x.cellar.GetPooledGameObject(21);
			pooledGameObject.gameObject.SetActive(true);
			pooledGameObject.transform.position = this.tr.position + new Vector3(0f, 0.1f, 0f);
			if (this.resourceLocation >= 0)
			{
				Library.Inventory invRowFromName = Links.x.library.GetInvRowFromName("Shell");
				if (invRowFromName != null)
				{
					int num = -1;
					if (character.node != null)
					{
						num = (int)character.node.Area;
					}
					Item item = Links.x.inventory.groundBag.DropLoot(invRowFromName._ID, 1, Vector3.zero, Vector3.zero, new Vector4((float)invRowFromName._DurabilityMax, 0f, 0f, 0f), null, base.gameObject.transform.position, false, false, num);
					Links.x.diorama.AddItem(item);
				}
				this.dead = true;
				Links.x.diorama.ReturnResource(this.resourceLocation, this.tr.parent.gameObject);
				base.gameObject.SetActive(false);
				return;
			}
			if (this.isResource)
			{
				GameObject pooledGameObject2 = Links.x.cellar.GetPooledGameObject(20);
				pooledGameObject2.gameObject.SetActive(true);
				pooledGameObject2.transform.position = this.tr.position;
				pooledGameObject2.transform.rotation = this.tr.rotation;
				Links.x.critters.AddSquished(pooledGameObject2, false);
				base.gameObject.SetActive(false);
				return;
			}
			GameObject pooledGameObject3 = Links.x.cellar.GetPooledGameObject(20);
			pooledGameObject3.gameObject.SetActive(true);
			pooledGameObject3.transform.position = this.tr.position;
			pooledGameObject3.transform.rotation = this.tr.rotation;
			Links.x.critters.AddSquished(pooledGameObject3, true);
			if (character)
			{
				character.stats.AddSpiritFromKill(1f);
			}
		}
	}

	// Token: 0x06000587 RID: 1415 RVA: 0x00079C54 File Offset: 0x00077E54
	private void OnAnimatorMove()
	{
		if (this.dead)
		{
			return;
		}
		if (this.targetPosition != Vector3.zero && !this.pathmaking)
		{
			if ((this.tr.position - this.targetPosition).sqrMagnitude < 0.09f)
			{
				if (this.resourceLocation >= 0)
				{
					this.lastNode = this.pathNodes[this.currentIndex];
				}
				if (this.currentIndex + 1 < this.nodes.Count && this.nodes.Count > 0)
				{
					this.currentIndex++;
					this.targetPosition = this.nodes[this.currentIndex];
					this.t = 0f;
					this.startPosition = this.tr.position;
					return;
				}
				this.targetPosition = Vector3.zero;
				this.MakeRandomPath(this.lastNode);
				return;
			}
			else
			{
				if (!this.isResource)
				{
					this.tr.rotation = Quaternion.LookRotation(this.targetPosition - this.tr.position);
					Vector3 rootPosition = this.anim.rootPosition;
					rootPosition.y = Mathf.Lerp(this.tr.position.y, this.targetPosition.y, Time.deltaTime * 5f);
					this.tr.position = rootPosition;
					return;
				}
				this.t += Time.deltaTime;
				this.tr.position = Vector3.Lerp(this.startPosition, this.targetPosition, this.t * 2f);
			}
		}
	}

	// Token: 0x0400087D RID: 2173
	public Transform tr;

	// Token: 0x0400087E RID: 2174
	public bool dead;

	// Token: 0x0400087F RID: 2175
	private Animator anim;

	// Token: 0x04000880 RID: 2176
	private Vector3 moveFollow;

	// Token: 0x04000881 RID: 2177
	private Seeker seeker;

	// Token: 0x04000882 RID: 2178
	private NNConstraint nodeConstraint = new NNConstraint();

	// Token: 0x04000883 RID: 2179
	public List<Vector3> nodes = new List<Vector3>();

	// Token: 0x04000884 RID: 2180
	private List<Vector3> pathVectors;

	// Token: 0x04000885 RID: 2181
	private List<GraphNode> pathNodes;

	// Token: 0x04000886 RID: 2182
	private GraphNode lastNode;

	// Token: 0x04000887 RID: 2183
	private Vector3 targetPosition;

	// Token: 0x04000888 RID: 2184
	private Vector3 startPosition;

	// Token: 0x04000889 RID: 2185
	private float t;

	// Token: 0x0400088A RID: 2186
	private int currentIndex;

	// Token: 0x0400088B RID: 2187
	private bool pathmaking;

	// Token: 0x0400088C RID: 2188
	public int resourceLocation = -1;

	// Token: 0x0400088D RID: 2189
	public Vector3 resourcePoint;

	// Token: 0x0400088E RID: 2190
	public bool isResource;

	// Token: 0x0400088F RID: 2191
	public float spawnTime;
}
