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

// Token: 0x02000040 RID: 64
public class Pickups : MonoBehaviour
{
	// Token: 0x0600082E RID: 2094 RVA: 0x000B2723 File Offset: 0x000B0923
	private void Start()
	{
	}

	// Token: 0x0600082F RID: 2095 RVA: 0x000B2728 File Offset: 0x000B0928
	public void Setup(Vector3 from, Vector3 to, GraphNode startNode)
	{
		if (!this.animator)
		{
			this.animator = base.gameObject.GetComponent<Animator>();
			this.nodeConstraint = new NNConstraint();
		}
		if (Records.x.pocketPause)
		{
			this.startedInPocket = true;
		}
		else
		{
			this.startedInPocket = false;
		}
		base.gameObject.SetActive(true);
		this.tr = base.transform;
		this.tr.position = to;
		this.startTime = Links.x.gameplay.seconds;
		Quaternion quaternion = Quaternion.LookRotation(to - from);
		this.targetPosition = to + quaternion * Vector3.forward * 2f;
		this.node = startNode;
		this.currentIndex = -1;
		this.meshRenderer.material.SetFloat("_Blink", 0f);
		this.blinking = 0;
		Vector3 vector = this.targetPosition;
		this.combatCharacters = Links.x.diorama.characters;
		this.node = this.GetSlideNode(vector, startNode);
		if (this.node != null)
		{
			Vector3 vector2 = quaternion * Vector3.forward;
			vector = this.tr.position + vector2 * 2.5f * 2f;
			GraphNode slideNode = this.GetSlideNode(vector, this.node);
			if (slideNode != null)
			{
				this.node = slideNode;
				vector = this.tr.position + vector2 * 2.5f * 3f;
				GraphNode slideNode2 = this.GetSlideNode(vector, slideNode);
				if (slideNode2 != null)
				{
					this.node = slideNode2;
					vector = this.tr.position + vector2 * 2.5f * 4f;
					GraphNode slideNode3 = this.GetSlideNode(vector, slideNode2);
					if (slideNode3 != null)
					{
						this.node = slideNode3;
						if (!this.IsPointAway((Vector3)this.node.position))
						{
							vector = this.tr.position + vector2 * 2.5f * 5f;
							GraphNode slideNode4 = this.GetSlideNode(vector, slideNode3);
							if (slideNode4 != null)
							{
								this.node = slideNode4;
								if (!this.IsPointAway((Vector3)this.node.position))
								{
									vector = this.tr.position + vector2 * 2.5f * 6f;
									GraphNode slideNode5 = this.GetSlideNode(vector, slideNode4);
									if (slideNode5 != null)
									{
										this.node = slideNode5;
										if (!this.IsPointAway((Vector3)this.node.position))
										{
											vector = this.tr.position + vector2 * 2.5f * 6f;
											GraphNode slideNode6 = this.GetSlideNode(vector, slideNode5);
											if (slideNode6 != null)
											{
												this.node = slideNode6;
												if (!this.IsPointAway((Vector3)this.node.position))
												{
													vector = this.tr.position + vector2 * 2.5f * 6f;
													GraphNode slideNode7 = this.GetSlideNode(vector, slideNode6);
													if (slideNode7 != null)
													{
														this.node = slideNode7;
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		if (this.node != null)
		{
			this.targetPosition = (Vector3)this.node.position;
			Links.x.cellar.AddPickups(this);
			this.done = false;
			this.startPosition = this.tr.position;
			this.turn = new Vector3(Random.Range(-10f, 10f), Random.Range(-10f, 10f), Random.Range(-10f, 10f));
			base.enabled = true;
			this.DoBlink(0f);
			this.blinking = 0;
			this.DrawArc();
			return;
		}
		this.End();
	}

	// Token: 0x06000830 RID: 2096 RVA: 0x000B2B1C File Offset: 0x000B0D1C
	public void PickItUp(Character character)
	{
		if (character)
		{
			bool flag = false;
			bool flag2 = false;
			if (this.isSpirit)
			{
				if (character.party && character.stats.HasSpellbook() && character.stats.SpiritCurrent() < 90)
				{
					flag = true;
				}
				if (character.npc && !character.stats.semizenMagic && character.stats.canCastSpells && character.stats.SpiritCurrent() < 90)
				{
					flag = true;
				}
			}
			else if ((float)character.stats.HealthCurrent() / (float)character.stats.HealthMax() < 0.97f)
			{
				flag2 = true;
			}
			if ((this.isHealth && flag2) || (flag && this.isSpirit))
			{
				this.blinking = 0;
				this.DoBlink(0f);
				if (this.isSpirit)
				{
					character.stats.AddSpiritFromKill((float)this.amount);
					MasterAudio.PlaySoundAndForget("Spirit Pickup", 1f, new float?(1f), 0f, "", null);
				}
				else if (this.isHealth)
				{
					character.stats.AddHealth(this.amount);
					MasterAudio.PlaySoundAndForget("Health Pickup", 1f, new float?(1f), 0f, "", null);
				}
				this.node = null;
				this.animator.Play("PickItUp");
				if (!Records.x.filming)
				{
					GameObject pooledGameObject = Links.x.cellar.GetPooledGameObject(105);
					float num = character.torsoPosition.y - character.currentPosition.y;
					num = Mathf.Clamp(num, 1.5f, 3f);
					pooledGameObject.transform.position = character.currentPosition + new Vector3(0f, num, 0f);
					pooledGameObject.transform.localScale = Vector3.one * character.stats.characterRow._Height;
					pooledGameObject.SetActive(true);
					pooledGameObject.transform.SetParent(character.tr, true);
					if (this.isSpirit)
					{
						pooledGameObject.GetComponent<Animator>().Play("Spirit Pickup");
						return;
					}
					pooledGameObject.GetComponent<Animator>().Play("Pickup");
				}
			}
		}
	}

	// Token: 0x06000831 RID: 2097 RVA: 0x000B2D6C File Offset: 0x000B0F6C
	public void OutOfTime()
	{
		MasterAudio.PlaySound3DAtVector3AndForget("Effect Removed", this.tr.position, 1f, new float?(1f), 0f, null, null);
		this.node = null;
		this.animator.Play("Remove");
	}

	// Token: 0x06000832 RID: 2098 RVA: 0x000B2DC4 File Offset: 0x000B0FC4
	public void End()
	{
		Links.x.cellar.ReturnPickups(this);
		base.gameObject.SetActive(false);
	}

	// Token: 0x06000833 RID: 2099 RVA: 0x000B2DE4 File Offset: 0x000B0FE4
	public void UpdateTime()
	{
		float num = 200f;
		if (this.isSpirit)
		{
			num = 500f;
		}
		float seconds = Links.x.gameplay.seconds;
		if (seconds > this.startTime + num)
		{
			this.OutOfTime();
			return;
		}
		if (seconds > this.startTime + num - 40f && this.blinking == 0)
		{
			this.StartBlink(40f, seconds);
		}
		if (seconds > this.startTime + num - 20f)
		{
			this.blinkInterval = 3f;
		}
		this.CheckBlink(seconds);
	}

	// Token: 0x06000834 RID: 2100 RVA: 0x000B2E6F File Offset: 0x000B106F
	public void StartBlink(float totalTimeLength, float s)
	{
		this.blinking = 1;
		this.blinkTime = s;
		this.blinkInterval = 6f;
		this.DoBlink(1f);
	}

	// Token: 0x06000835 RID: 2101 RVA: 0x000B2E98 File Offset: 0x000B1098
	public void CheckBlink(float x)
	{
		if (this.blinking > 0 && x > this.blinkTime + this.blinkInterval && !Records.x.pocketPause)
		{
			this.blinkTime = x;
			this.blinking++;
			if (this.blinking % 2 == 0)
			{
				this.DoBlink(0f);
				return;
			}
			this.DoBlink(1f);
		}
	}

	// Token: 0x06000836 RID: 2102 RVA: 0x000B2F00 File Offset: 0x000B1100
	private void DoBlink(float state)
	{
		this.meshRenderer.material.SetFloat("_Blink", state);
	}

	// Token: 0x06000837 RID: 2103 RVA: 0x000B2F18 File Offset: 0x000B1118
	private void Update()
	{
		if ((!Records.x.pocketPause || (Records.x.pocketPause && this.startedInPocket)) && !Records.x.paused && !this.done)
		{
			if ((this.tr.position - this.b).sqrMagnitude < 0.040000003f || this.currentIndex == -1)
			{
				if (this.currentIndex + 1 > this.arcPoints.Count - 1)
				{
					this.done = true;
					base.enabled = false;
					return;
				}
				this.currentIndex++;
				this.startPosition = this.previousPosition;
				this.a = this.tr.position;
				this.b = this.arcPoints[this.currentIndex];
				this.previousPosition = this.b;
			}
			else
			{
				this.tr.position = Vector3.MoveTowards(this.tr.position, this.b, 20f * Time.deltaTime);
			}
			this.b != this.a;
		}
	}

	// Token: 0x06000838 RID: 2104 RVA: 0x000B3048 File Offset: 0x000B1248
	public void DrawArc()
	{
		Vector3 position = this.tr.position;
		this.trajectoryModifier = 1;
		this.initialVelocity = 1f;
		Quaternion quaternion = this.GetBearing(position, this.targetPosition);
		this.tr.rotation = quaternion;
		this.CalculateTrajectory();
		this.tr.rotation = Quaternion.Euler(new Vector3(0f, 0f, 0f));
	}

	// Token: 0x06000839 RID: 2105 RVA: 0x000B30B8 File Offset: 0x000B12B8
	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: 0x0600083A RID: 2106 RVA: 0x000B31EC File Offset: 0x000B13EC
	public RaycastHit CalculateTrajectory()
	{
		RaycastHit raycastHit = default(RaycastHit);
		Vector3 position = this.tr.position;
		float num = -(1f / this.trajectoryPrecision);
		bool flag = false;
		for (int i = 0; i < this.maximumIterations; i++)
		{
			if (!flag)
			{
				num += 1f / this.trajectoryPrecision;
				Vector3 vector = position + this.tr.forward * this.initialVelocity * num + 0.5f * this.gravity2 * Mathf.Pow(num, 2f);
				if ((vector - this.targetPosition).sqrMagnitude < 0.25f)
				{
					if (Physics.Linecast(this.prevPos, vector, out raycastHit, Links.x.floor))
					{
						flag = true;
					}
					this.prevPos = vector;
				}
				this.arcPoints.Add(vector);
			}
		}
		return raycastHit;
	}

	// Token: 0x0600083B RID: 2107 RVA: 0x000B32EC File Offset: 0x000B14EC
	public GraphNode GetSlideNode(Vector3 direction, GraphNode n)
	{
		if (!Links.x.gaia.pathfindingReady)
		{
			return null;
		}
		if (!Links.x.gaia.sceneLoaded)
		{
			return null;
		}
		this.nodeConstraint.constrainToArea = (int)n.Area;
		this.nodeConstraint.walkable = true;
		this.nodeConstraint.constrainWalkability = true;
		this.nodeConstraint.constrainTags = false;
		this.nodeConstraint.checkCircleID = 0;
		this.nodeConstraint.passID = 0;
		this.nodeConstraint.passID2 = 0;
		this.nodeConstraint.constrainPenalty = 0;
		this.nodeConstraint.checkConnections = 0;
		return n.ClosestConnection(n, 0, 0, 225f, direction, 0, 0, this.nodeConstraint, 0);
	}

	// Token: 0x0600083C RID: 2108 RVA: 0x000B33A8 File Offset: 0x000B15A8
	public bool IsPointAway(Vector3 center)
	{
		if (Random.Range(0, 3) == 0)
		{
			return false;
		}
		for (int i = 0; i < this.combatCharacters.Count; i++)
		{
			Character character = this.combatCharacters[i];
			if (character && character.hasActions && (character.currentPosition - center).sqrMagnitude <= 16f)
			{
				return false;
			}
		}
		return true;
	}

	// Token: 0x04000CBF RID: 3263
	public bool isSpirit;

	// Token: 0x04000CC0 RID: 3264
	public bool isHealth;

	// Token: 0x04000CC1 RID: 3265
	public int amount;

	// Token: 0x04000CC2 RID: 3266
	public Transform tr;

	// Token: 0x04000CC3 RID: 3267
	private float startTime;

	// Token: 0x04000CC4 RID: 3268
	private Animator animator;

	// Token: 0x04000CC5 RID: 3269
	public GraphNode node;

	// Token: 0x04000CC6 RID: 3270
	private NNConstraint nodeConstraint;

	// Token: 0x04000CC7 RID: 3271
	[Header("ARC")]
	private float gravity = -9.81f;

	// Token: 0x04000CC8 RID: 3272
	private float initialVelocity = 3f;

	// Token: 0x04000CC9 RID: 3273
	private Quaternion bearing;

	// Token: 0x04000CCA RID: 3274
	private int trajectoryModifier = 1;

	// Token: 0x04000CCB RID: 3275
	private Vector3 projectileVelocity;

	// Token: 0x04000CCC RID: 3276
	private float trajectoryPrecision = 20f;

	// Token: 0x04000CCD RID: 3277
	private int maximumIterations = 300;

	// Token: 0x04000CCE RID: 3278
	private Vector3 gravity2 = new Vector3(0f, -9.81f, 0f);

	// Token: 0x04000CCF RID: 3279
	private LineRenderer lineRendererToDrawTrajectory;

	// Token: 0x04000CD0 RID: 3280
	private Vector3 prevPos;

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

	// Token: 0x04000CD2 RID: 3282
	private int currentIndex;

	// Token: 0x04000CD3 RID: 3283
	private Vector3 a;

	// Token: 0x04000CD4 RID: 3284
	private Vector3 b;

	// Token: 0x04000CD5 RID: 3285
	private Vector3 previousPosition;

	// Token: 0x04000CD6 RID: 3286
	private Vector3 targetPosition;

	// Token: 0x04000CD7 RID: 3287
	private bool done;

	// Token: 0x04000CD8 RID: 3288
	private Vector3 startPosition;

	// Token: 0x04000CD9 RID: 3289
	private Vector3 turn;

	// Token: 0x04000CDA RID: 3290
	private bool startedInPocket;

	// Token: 0x04000CDB RID: 3291
	private List<Character> combatCharacters;

	// Token: 0x04000CDC RID: 3292
	[Header("Blinking")]
	public int blinking;

	// Token: 0x04000CDD RID: 3293
	private float blinkTime;

	// Token: 0x04000CDE RID: 3294
	private float blinkInterval;

	// Token: 0x04000CDF RID: 3295
	public MeshRenderer meshRenderer;
}
