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

// Token: 0x02000024 RID: 36
public class Critters : MonoBehaviour
{
	// Token: 0x06000589 RID: 1417 RVA: 0x00079E28 File Offset: 0x00078028
	private void Start()
	{
		this.count = 7;
		for (int i = 0; i < this.count; i++)
		{
			GameObject gameObject = Object.Instantiate<GameObject>(this.critterTypes[Random.Range(0, this.critterTypes.Count)], new Vector3(10000f, 0f, 0f), Quaternion.identity, base.gameObject.transform);
			CritterActions critterActions = gameObject.transform.GetChild(0).gameObject.AddComponent<CritterActions>();
			this.activeCritters.Add(critterActions);
			gameObject.transform.SetParent(Links.x.gameplay.transform);
		}
		this.party = Links.x.party;
	}

	// Token: 0x0600058A RID: 1418 RVA: 0x00079EE4 File Offset: 0x000780E4
	private void Update()
	{
		if (Links.x.gaia.changingMapsComplete && Links.x.gaia.sceneLoaded && Links.x.main)
		{
			if ((this.prevPosition - Links.x.main.tr.position).sqrMagnitude > 100f && Links.x.main.desiredGraph == 0 && !Records.x.partySailing)
			{
				if (Links.x.main.node != null)
				{
					this.StartNewCritterPaths();
				}
				this.prevPosition = Links.x.main.tr.position;
			}
			Vector3 vector = Vector3.zero;
			int num = this.activeCritters.Count;
			for (int i = 0; i < num; i++)
			{
				if (!this.activeCritters[i].dead && this.activeCritters[i].tr)
				{
					vector = this.activeCritters[i].tr.position;
					for (int j = 0; j < this.party.Count; j++)
					{
						Character character = this.party[j];
						if (character && character.moving && character.stats.Floating() == 0)
						{
							vector.y = character.tr.position.y;
							if ((character.tr.position - vector).sqrMagnitude < 0.36f)
							{
								this.activeCritters[i].Squish(character);
								if (this.activeCritters[i].isResource)
								{
									break;
								}
								if (this.outsideNodes.Count > 0)
								{
									this.activeCritters[i].MakeRandomPath(this.outsideNodes[Random.Range(0, this.outsideNodes.Count)]);
									break;
								}
								this.activeCritters[i].tr.position = Vector3.zero;
								break;
							}
						}
					}
				}
			}
		}
	}

	// Token: 0x0600058B RID: 1419 RVA: 0x0007A12F File Offset: 0x0007832F
	public void StartNewCritterPaths()
	{
		if (!this.lookingForPaths)
		{
			base.StartCoroutine(this.CritterPaths());
		}
	}

	// Token: 0x0600058C RID: 1420 RVA: 0x0007A146 File Offset: 0x00078346
	private IEnumerator CritterPaths()
	{
		this.outsideNodes.Clear();
		if (Links.x.gaia.changingMapsComplete && Links.x.gaia.sceneLoaded && Links.x.gaia.pathfindingReady)
		{
			GraphNode node = Links.x.main.node;
			Vector3 startPosition = (Vector3)node.position;
			ConstantPath constPath = null;
			constPath = ConstantPath.ConstructFast(node, Records.x.GetConstantPathRadius(12), null);
			this.nodeConstraint.constrainWalkability = false;
			this.nodeConstraint.walkable = true;
			this.nodeConstraint.constrainTags = false;
			this.nodeConstraint.checkCircleID = 0;
			this.nodeConstraint.passID = 0;
			this.nodeConstraint.passID2 = 0;
			this.nodeConstraint.checkConnections = 0;
			this.nodeConstraint.constrainPenalty = 0;
			this.nodeConstraint.constrainToArea = -1;
			this.nodeConstraint.constrainToEnvironment = 1;
			constPath.nnConstraint = this.nodeConstraint;
			AstarPath.StartPath(constPath, false);
			yield return base.StartCoroutine(constPath.WaitForPath());
			int num = 0;
			constPath.Claim(this);
			this.allNodes = constPath.allNodes;
			int num2 = this.allNodes.Count;
			float max = 900f;
			for (int j = 0; j < num2; j++)
			{
				if (((Vector3)this.allNodes[j].position - startPosition).sqrMagnitude > max && this.allNodes[j].Walkable)
				{
					this.outsideNodes.Add(this.allNodes[j]);
					num++;
				}
			}
			constPath.Release(this, false);
			max = 900f;
			int num3;
			for (int i = 0; i < this.count; i = num3 + 1)
			{
				if (!this.activeCritters[i].dead && (Links.x.follow.position - this.activeCritters[i].tr.position).sqrMagnitude > max)
				{
					if (this.outsideNodes.Count > 0)
					{
						GraphNode graphNode = this.outsideNodes[Random.Range(0, this.outsideNodes.Count)];
						this.activeCritters[i].MakeRandomPath(graphNode);
					}
					yield return new WaitForSeconds(0.1f);
				}
				num3 = i;
			}
			this.CheckSquished(startPosition);
			startPosition = default(Vector3);
			constPath = null;
		}
		yield break;
	}

	// Token: 0x0600058D RID: 1421 RVA: 0x0007A155 File Offset: 0x00078355
	public void AddActiveCritter(CritterActions c)
	{
		this.activeCritters.Add(c);
	}

	// Token: 0x0600058E RID: 1422 RVA: 0x0007A163 File Offset: 0x00078363
	public void RemoveActiveCritter(CritterActions c)
	{
	}

	// Token: 0x0600058F RID: 1423 RVA: 0x0007A165 File Offset: 0x00078365
	public void AddSquished(GameObject go, bool respawn)
	{
		this.squished.Add(go);
	}

	// Token: 0x06000590 RID: 1424 RVA: 0x0007A173 File Offset: 0x00078373
	public GraphNode GetRandomNode()
	{
		return this.outsideNodes[Random.Range(0, this.outsideNodes.Count)];
	}

	// Token: 0x06000591 RID: 1425 RVA: 0x0007A194 File Offset: 0x00078394
	public void CheckSquished(Vector3 fromDistance)
	{
		bool flag = false;
		for (int i = 0; i < this.squished.Count; i++)
		{
			if ((this.squished[i].transform.position - fromDistance).sqrMagnitude > 1225f)
			{
				Links.x.cellar.ReturnPooledGameObject(20, this.squished[i]);
				this.squished[i] = null;
				flag = true;
			}
		}
		if (flag)
		{
			this.squished.RemoveAll((GameObject item) => item == null);
		}
	}

	// Token: 0x06000592 RID: 1426 RVA: 0x0007A240 File Offset: 0x00078440
	public void RemoveScene()
	{
		foreach (GameObject gameObject in this.squished)
		{
			Links.x.cellar.ReturnPooledGameObject(20, gameObject);
		}
		this.outsideNodes.Clear();
		for (int i = 0; i < this.activeCritters.Count; i++)
		{
			if (i < this.activeCritters.Count && this.activeCritters[i])
			{
				this.activeCritters[i].Stop();
				this.activeCritters[i].dead = false;
				this.activeCritters[i].gameObject.transform.position = new Vector3(10000f, 10000f, 10000f);
			}
		}
		this.prevPosition = new Vector3(100000f, 100000f, 100000f);
	}

	// Token: 0x04000890 RID: 2192
	public List<GameObject> critterTypes = new List<GameObject>();

	// Token: 0x04000891 RID: 2193
	public List<CritterActions> activeCritters = new List<CritterActions>();

	// Token: 0x04000892 RID: 2194
	public bool lookingForPaths;

	// Token: 0x04000893 RID: 2195
	private Vector3 prevPosition;

	// Token: 0x04000894 RID: 2196
	private List<GraphNode> outsideNodes = new List<GraphNode>();

	// Token: 0x04000895 RID: 2197
	private NNConstraint nodeConstraint = new NNConstraint();

	// Token: 0x04000896 RID: 2198
	private List<GraphNode> allNodes;

	// Token: 0x04000897 RID: 2199
	private int count;

	// Token: 0x04000898 RID: 2200
	private List<Character> party;

	// Token: 0x04000899 RID: 2201
	private List<GameObject> squished = new List<GameObject>();

	// Token: 0x0400089A RID: 2202
	public GameObject crab;
}
