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

// Token: 0x020000F6 RID: 246
public class FlockController : MonoBehaviour
{
	// Token: 0x06001581 RID: 5505 RVA: 0x00199F61 File Offset: 0x00198161
	public void Start()
	{
		this._roamers.Clear();
		this.LateStart();
	}

	// Token: 0x06001582 RID: 5506 RVA: 0x00199F74 File Offset: 0x00198174
	public void LateStart()
	{
		if (!this._childPrefab)
		{
			Debug.LogWarning("Bird Flock does not have a bird prefab assigned, aborting.");
			base.enabled = false;
			return;
		}
		this._thisT = base.transform;
		if (this._positionSphereDepth == -1f)
		{
			this._positionSphereDepth = this._positionSphere;
		}
		if (this._spawnSphereDepth == -1f)
		{
			this._spawnSphereDepth = this._spawnSphere;
		}
		this._posBuffer = this._thisT.position + this._startPosOffset;
		if (!this._slowSpawn)
		{
			this.AddChild(this._childAmount);
			this._childCountCache = this._roamers.Count;
		}
		if (this._randomPositionTimer > 0f)
		{
			base.InvokeRepeating("SetFlockRandomPosition", this._randomPositionTimer, this._randomPositionTimer);
		}
	}

	// Token: 0x06001583 RID: 5507 RVA: 0x0019A044 File Offset: 0x00198244
	public void AddChild(int amount)
	{
		if (this._groupChildToNewTransform)
		{
			this.InstantiateGroup();
		}
		for (int i = 0; i < amount; i++)
		{
			FlockChild flockChild = Object.Instantiate<FlockChild>(this._childPrefab);
			flockChild._spawner = this;
			if (flockChild)
			{
				flockChild.transform.parent = base.transform;
				flockChild.transform.parent = null;
			}
			this._roamers.Add(flockChild);
			this.AddChildToParent(flockChild.transform);
		}
		this._childCountCache += amount;
	}

	// Token: 0x06001584 RID: 5508 RVA: 0x0019A0C9 File Offset: 0x001982C9
	public void AddChildToParent(Transform obj)
	{
		if (this._groupChildToFlock)
		{
			this._groupTransform = this._thisT;
		}
		obj.parent = this._groupTransform;
	}

	// Token: 0x06001585 RID: 5509 RVA: 0x0019A0EC File Offset: 0x001982EC
	public void RemoveChild(int amount)
	{
		for (int i = 0; i < amount; i++)
		{
			Component component = this._roamers[this._roamers.Count - 1];
			this._roamers.RemoveAt(this._roamers.Count - 1);
			Object.Destroy(component.gameObject);
		}
		this._childCountCache -= amount;
	}

	// Token: 0x06001586 RID: 5510 RVA: 0x0019A150 File Offset: 0x00198350
	public void Update()
	{
		if (this._activeChildren > 0f)
		{
			if (this._skipFrame)
			{
				this._updateCounter++;
				this._updateCounter %= 2;
				this._newDelta = Time.deltaTime * 2f % 0.5f;
			}
			else
			{
				this._newDelta = Time.deltaTime;
			}
		}
		this.UpdateChildAmount();
		for (int i = 0; i < this._roamers.Count; i++)
		{
			this._roamers[i].BirdUpdate();
		}
	}

	// Token: 0x06001587 RID: 5511 RVA: 0x0019A1E0 File Offset: 0x001983E0
	public void InstantiateGroup()
	{
		if (this._groupTransform != null)
		{
			return;
		}
		GameObject gameObject = new GameObject();
		this._groupTransform = gameObject.transform;
		this._groupTransform.position = this._thisT.position;
		if (this._groupName != "")
		{
			gameObject.name = this._groupName;
			return;
		}
		gameObject.name = this._thisT.name + " Bird Container";
	}

	// Token: 0x06001588 RID: 5512 RVA: 0x0019A25E File Offset: 0x0019845E
	public void UpdateChildAmount()
	{
		if (this._childAmount >= 0 && this._childAmount < this._roamers.Count)
		{
			this.RemoveChild(1);
			return;
		}
		if (this._childAmount > this._roamers.Count)
		{
			this.AddChild(1);
		}
	}

	// Token: 0x06001589 RID: 5513 RVA: 0x0019A2A0 File Offset: 0x001984A0
	public void OnDrawGizmos()
	{
		if (this._thisT == null)
		{
			this._thisT = base.transform;
		}
		if (!Application.isPlaying)
		{
			if (this._roamers.Count > 0)
			{
				this._roamers.Clear();
			}
			if (this._posBuffer != this._thisT.position + this._startPosOffset)
			{
				this._posBuffer = this._thisT.position + this._startPosOffset;
			}
		}
		if (this._positionSphereDepth == -1f)
		{
			this._positionSphereDepth = this._positionSphere;
		}
		if (this._spawnSphereDepth == -1f)
		{
			this._spawnSphereDepth = this._spawnSphere;
		}
		Gizmos.color = Color.blue;
		Gizmos.DrawWireCube(this._posBuffer, new Vector3(this._spawnSphere * 2f, this._spawnSphereHeight * 2f, this._spawnSphereDepth * 2f));
		Gizmos.color = Color.cyan;
		Gizmos.DrawWireCube(this._thisT.position, new Vector3(this._positionSphere * 2f + this._spawnSphere * 2f, this._positionSphereHeight * 2f + this._spawnSphereHeight * 2f, this._positionSphereDepth * 2f + this._spawnSphereDepth * 2f));
	}

	// Token: 0x0600158A RID: 5514 RVA: 0x0019A400 File Offset: 0x00198600
	public void SetFlockRandomPosition()
	{
		Vector3 zero = Vector3.zero;
		zero.x = Random.Range(-this._positionSphere, this._positionSphere) + this._thisT.position.x;
		zero.z = Random.Range(-this._positionSphereDepth, this._positionSphereDepth) + this._thisT.position.z;
		zero.y = Random.Range(-this._positionSphereHeight, this._positionSphereHeight) + this._thisT.position.y;
		this._posBuffer = zero;
		if (this._forceChildWaypoints)
		{
			for (int i = 0; i < this._roamers.Count; i++)
			{
				this._roamers[i].Wander(Random.value * this._forcedRandomDelay);
			}
		}
	}

	// Token: 0x0600158B RID: 5515 RVA: 0x0019A4D4 File Offset: 0x001986D4
	public void destroyBirds()
	{
		for (int i = 0; i < this._roamers.Count; i++)
		{
			if (this._roamers[i] != null)
			{
				Object.Destroy(this._roamers[i].gameObject);
			}
		}
		this._childCountCache = 0;
		this._childAmount = 0;
		this._roamers.Clear();
	}

	// Token: 0x0400256D RID: 9581
	public FlockChild _childPrefab;

	// Token: 0x0400256E RID: 9582
	public int _childAmount = 250;

	// Token: 0x0400256F RID: 9583
	public bool _slowSpawn;

	// Token: 0x04002570 RID: 9584
	public float _spawnSphere = 3f;

	// Token: 0x04002571 RID: 9585
	public float _spawnSphereHeight = 3f;

	// Token: 0x04002572 RID: 9586
	public float _spawnSphereDepth = -1f;

	// Token: 0x04002573 RID: 9587
	public float _minSpeed = 6f;

	// Token: 0x04002574 RID: 9588
	public float _maxSpeed = 10f;

	// Token: 0x04002575 RID: 9589
	public float _minScale = 0.7f;

	// Token: 0x04002576 RID: 9590
	public float _maxScale = 1f;

	// Token: 0x04002577 RID: 9591
	public float _soarFrequency;

	// Token: 0x04002578 RID: 9592
	public string _soarAnimation = "Soar";

	// Token: 0x04002579 RID: 9593
	public string _flapAnimation = "Flap";

	// Token: 0x0400257A RID: 9594
	public string _idleAnimation = "Idle";

	// Token: 0x0400257B RID: 9595
	public float _diveValue = 7f;

	// Token: 0x0400257C RID: 9596
	public float _diveFrequency = 0.5f;

	// Token: 0x0400257D RID: 9597
	public float _minDamping = 1f;

	// Token: 0x0400257E RID: 9598
	public float _maxDamping = 2f;

	// Token: 0x0400257F RID: 9599
	public float _waypointDistance = 1f;

	// Token: 0x04002580 RID: 9600
	public float _minAnimationSpeed = 2f;

	// Token: 0x04002581 RID: 9601
	public float _maxAnimationSpeed = 4f;

	// Token: 0x04002582 RID: 9602
	public float _randomPositionTimer = 10f;

	// Token: 0x04002583 RID: 9603
	public float _positionSphere = 25f;

	// Token: 0x04002584 RID: 9604
	public float _positionSphereHeight = 25f;

	// Token: 0x04002585 RID: 9605
	public float _positionSphereDepth = -1f;

	// Token: 0x04002586 RID: 9606
	public bool _childTriggerPos;

	// Token: 0x04002587 RID: 9607
	public bool _forceChildWaypoints;

	// Token: 0x04002588 RID: 9608
	public float _forcedRandomDelay = 1.5f;

	// Token: 0x04002589 RID: 9609
	public bool _flatFly;

	// Token: 0x0400258A RID: 9610
	public bool _flatSoar;

	// Token: 0x0400258B RID: 9611
	public bool _birdAvoid;

	// Token: 0x0400258C RID: 9612
	public int _birdAvoidHorizontalForce = 1000;

	// Token: 0x0400258D RID: 9613
	public bool _birdAvoidDown;

	// Token: 0x0400258E RID: 9614
	public bool _birdAvoidUp;

	// Token: 0x0400258F RID: 9615
	public int _birdAvoidVerticalForce = 300;

	// Token: 0x04002590 RID: 9616
	public float _birdAvoidDistanceMax = 4.5f;

	// Token: 0x04002591 RID: 9617
	public float _birdAvoidDistanceMin = 5f;

	// Token: 0x04002592 RID: 9618
	public float _soarMaxTime;

	// Token: 0x04002593 RID: 9619
	public LayerMask _avoidanceMask = -1;

	// Token: 0x04002594 RID: 9620
	public List<FlockChild> _roamers;

	// Token: 0x04002595 RID: 9621
	public Vector3 _posBuffer;

	// Token: 0x04002596 RID: 9622
	public bool _skipFrame;

	// Token: 0x04002597 RID: 9623
	public int _updateDivisor = 2;

	// Token: 0x04002598 RID: 9624
	public float _newDelta;

	// Token: 0x04002599 RID: 9625
	public int _updateCounter;

	// Token: 0x0400259A RID: 9626
	public float _activeChildren;

	// Token: 0x0400259B RID: 9627
	public bool _groupChildToNewTransform;

	// Token: 0x0400259C RID: 9628
	public Transform _groupTransform;

	// Token: 0x0400259D RID: 9629
	public string _groupName = "";

	// Token: 0x0400259E RID: 9630
	public bool _groupChildToFlock;

	// Token: 0x0400259F RID: 9631
	public Vector3 _startPosOffset;

	// Token: 0x040025A0 RID: 9632
	public Transform _thisT;

	// Token: 0x040025A1 RID: 9633
	public bool LimitPitchRotation = true;

	// Token: 0x040025A2 RID: 9634
	public int _childCountCache;
}
