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

// Token: 0x02000042 RID: 66
public class Projectile : MonoBehaviour
{
	// Token: 0x06000844 RID: 2116 RVA: 0x000B3A4B File Offset: 0x000B1C4B
	private void Start()
	{
	}

	// Token: 0x06000845 RID: 2117 RVA: 0x000B3A50 File Offset: 0x000B1C50
	private void FixedUpdate()
	{
		if (this.hasInfo)
		{
			bool flag = false;
			if (!Records.x.pocketPause || (Records.x.pocketPause && (this.startedinRally || this.mainCharacter || flag) && !Records.x.paused && !this.pocketProjectile))
			{
				if ((Links.x.combat.inRally || Links.x.combat.inPocketAttack) && !this.startedinRally)
				{
					if (this.meshRenderers == null)
					{
						this.meshRenderers = this.item.gameObject.GetComponentsInChildren<MeshRenderer>(false);
					}
					MeshRenderer[] array = this.meshRenderers;
					for (int i = 0; i < array.Length; i++)
					{
						array[i].enabled = false;
					}
					this.turnedOffForSynergy = true;
				}
				if (this.turnedOffForSynergy && !Links.x.combat.inRally && !Links.x.combat.inPocketAttack)
				{
					this.turnedOffForSynergy = false;
					MeshRenderer[] array = this.meshRenderers;
					for (int i = 0; i < array.Length; i++)
					{
						array[i].enabled = true;
					}
				}
				if (this.hasPowerDraw || this.type == 3 || this.type == 2)
				{
					if ((this.item.position - this.b).sqrMagnitude < 0.040000003f || this.currentIndex == -1)
					{
						if (this.currentIndex + 1 > this.arcPoints.Count - 1)
						{
							this.Explode(this.item.position);
							bool flag2 = false;
							if (this.target)
							{
								Vector3 vector = this.arcPoints[this.arcPoints.Count - 1];
								vector.y = this.target.tr.position.y;
								float num = 1f;
								if (this.inventoryRow._Name.Contains("Vial"))
								{
									num = 3f;
								}
								if ((vector - this.target.currentPosition).sqrMagnitude < num * num)
								{
									flag2 = true;
								}
							}
							else if (this.targetInteractive)
							{
								Vector3 vector2 = this.arcPoints[this.arcPoints.Count - 1];
								vector2.y = this.targetPosition.y;
								float num2 = 1f;
								if (this.inventoryRow._Name.Contains("Vial"))
								{
									num2 = 3f;
								}
								if ((vector2 - this.targetPosition).sqrMagnitude < num2 * num2)
								{
									flag2 = true;
								}
							}
							if (this.done)
							{
								flag2 = false;
							}
							this.End(flag2);
							return;
						}
						this.currentIndex++;
						this.startPosition = this.previousPosition;
						this.t = 0f;
						this.a = this.item.position;
						this.b = this.arcPoints[this.currentIndex];
						this.previousPosition = this.b;
					}
					this.t += Time.fixedDeltaTime * 5f;
					this.item.position = Vector3.MoveTowards(this.item.position, this.b, 20f * Time.fixedDeltaTime);
					if (this.b != this.a)
					{
						if (this.type == 1 || this.type == 0)
						{
							this.item.rotation = Quaternion.Slerp(this.item.rotation, Quaternion.LookRotation(this.b - this.a), Time.fixedDeltaTime * 10f);
							return;
						}
						this.item.Rotate(this.turn);
						return;
					}
				}
				else if (Links.x.combat.inRally)
				{
					this.t += Time.deltaTime / 0.5f;
					this.item.position = Vector3.Lerp(this.startPosition, this.targetPosition, this.t);
					if (this.t >= 1f)
					{
						this.End(true);
						return;
					}
					if (this.type == 1 || this.type == 0)
					{
						this.item.rotation = Quaternion.Slerp(this.item.rotation, Quaternion.LookRotation(this.targetPosition - this.startPosition), Time.fixedDeltaTime * 20f);
						return;
					}
					this.item.Rotate(this.turn);
					return;
				}
				else if (!this.findingNodes)
				{
					if ((this.startPosition - this.item.position).sqrMagnitude > (this.item.position - this.outOfSightPosition).sqrMagnitude)
					{
						this.End(false);
						return;
					}
					this.item.position = Vector3.MoveTowards(this.item.position, this.outOfSightPosition, this.speed * Time.fixedDeltaTime);
					if (this.type == 1 || this.type == 0)
					{
						this.item.rotation = Quaternion.Slerp(this.item.rotation, Quaternion.LookRotation(this.targetPosition - this.startPosition), Time.fixedDeltaTime * 10f);
					}
					else
					{
						this.item.Rotate(this.turn);
					}
					this.previousItemPosition = this.item.position;
				}
			}
		}
	}

	// Token: 0x06000846 RID: 2118 RVA: 0x000B3FDC File Offset: 0x000B21DC
	private void Explode(Vector3 pt)
	{
		if (this.type == 2)
		{
			GameObject pooledGameObject = Links.x.cellar.GetPooledGameObject(38);
			pooledGameObject.transform.position = pt - new Vector3(0f, 1.5f, 0f);
			pooledGameObject.SetActive(true);
			MasterAudio.PlaySound3DAtVector3AndForget("Elixir", base.gameObject.transform.position, Random.Range(0.5f, 0.6f), new float?(1f), 0f, "", null);
		}
		if (this.inventoryRow._Name.Contains("Rock"))
		{
			GameObject pooledGameObject2 = Links.x.cellar.GetPooledGameObject(60);
			pooledGameObject2.transform.position = this.item.position;
			pooledGameObject2.SetActive(true);
			MasterAudio.PlaySound3DAtVector3AndForget("Interactives", base.gameObject.transform.position, 0.5f, new float?(1f), 0f, "Chest Break", null);
		}
	}

	// Token: 0x06000847 RID: 2119 RVA: 0x000B40FC File Offset: 0x000B22FC
	private void OnTriggerEnter(Collider other)
	{
		if (this.done || !this.action || this.pocketProjectile)
		{
			return;
		}
		if (!this.action.ai)
		{
			return;
		}
		GameObject gameObject = other.gameObject;
		if (gameObject)
		{
			if (gameObject.layer == 31 && !this.targetInteractive)
			{
				SelectionCircle component = gameObject.GetComponent<SelectionCircle>();
				if (component)
				{
					Character character = component.character;
					this.Hit(character);
				}
			}
			if (gameObject.layer == 6 && !this.targetInteractive)
			{
				Character component2 = gameObject.GetComponent<Character>();
				if (component2)
				{
					this.Hit(component2);
				}
			}
			if (gameObject.layer == 16 && gameObject == this.targetInteractive)
			{
				if (this.type >= 2)
				{
					this.Explode(this.targetPosition);
				}
				this.done = true;
				this.End(true);
			}
		}
	}

	// Token: 0x06000848 RID: 2120 RVA: 0x000B41E8 File Offset: 0x000B23E8
	public void Hit(Character c)
	{
		if (this.done)
		{
			return;
		}
		bool flag = true;
		if (this.startedInPocket || Links.x.combat.miniEvent || this.pocketProjectile)
		{
			flag = false;
		}
		if (this.hit != 4f && this.hit != 1f)
		{
			if (c == this.target)
			{
				if (this.type >= 2)
				{
					this.Explode(this.target.tr.position);
				}
				this.done = true;
				this.End(true);
			}
			else if (flag && this.action.ai.CanAttack(c))
			{
				if (!this.action.quickAttack)
				{
					this.action.firstAttack = false;
				}
				this.target = c;
				this.action.actionTargets.Add(this.target);
				this.action.ActionRolls(false, true, this.target, null);
				if (this.action.HasListTarget(this.target))
				{
					this.action.GetListSet(this.target, out this.hit, out this.damage, out this.attackTotal, out this.defenseTotal, out this.hitText);
				}
				if (this.type >= 2 && this.target)
				{
					this.Explode(this.target.tr.position);
				}
				this.done = true;
				if (this.damage > 0f)
				{
					this.End(true);
				}
				else
				{
					this.SendEvent(false);
				}
			}
		}
		if (this.hit == 1f)
		{
			if (c == this.target)
			{
				this.done = true;
				this.SendEvent(false);
			}
			else if (this.action.ai.CanAttack(c) && flag)
			{
				this.target = c;
				if (!this.action.quickAttack)
				{
					this.action.firstAttack = false;
				}
				Character character = this.action.character;
				Debug.Log(((character != null) ? character.ToString() : null) + "'s projectile found new target");
				this.action.actionTargets.Add(this.target);
				this.action.ActionRolls(false, true, this.target, null);
				if (this.action.HasListTarget(this.target))
				{
					this.action.GetListSet(this.target, out this.hit, out this.damage, out this.attackTotal, out this.defenseTotal, out this.hitText);
				}
				this.done = true;
				if (this.damage > 0f)
				{
					this.End(true);
				}
				else
				{
					this.SendEvent(false);
				}
			}
			if (this.type >= 2 && c)
			{
				this.Explode(c.tr.position);
			}
		}
	}

	// Token: 0x06000849 RID: 2121 RVA: 0x000B44BC File Offset: 0x000B26BC
	public void SetInfo(Character target1, GameObject targetInteractive1, float hit1, float damage1, string attackTotal1, string defenseTotal1, string hitText1, bool powerDraw)
	{
		this.hit = hit1;
		this.target = target1;
		this.actionCharacter = this.action.character;
		this.targetInteractive = targetInteractive1;
		this.originalTargetPosition = this.targetPosition;
		this.startedInPocket = Records.x.pocketPause;
		if (Links.x.combat.inRally || Links.x.combat.inPocketAttack || Links.x.combat.bubbleEvent)
		{
			this.startedinRally = true;
		}
		Vector3 vector = Quaternion.LookRotation(this.targetPosition - this.item.position) * Vector3.forward;
		this.outOfSightPosition = this.item.position + vector * 50f;
		if (targetInteractive1)
		{
			this.outOfSightPosition = this.originalTargetPosition;
		}
		this.damage = damage1;
		this.attackTotal = attackTotal1;
		this.defenseTotal = defenseTotal1;
		this.hitText = hitText1;
		this.mainCharacter = false;
		if (this.action.character.mainSelected)
		{
			this.mainCharacter = true;
		}
		this.hasPowerDraw = powerDraw;
		this.startPosition = this.item.position;
		this.previousItemPosition = this.startPosition;
		this.turn = new Vector3(Random.Range(-10f, 10f), Random.Range(-10f, 10f), Random.Range(-10f, 10f));
		this.t = 0f;
		if (this.actionCharacter.stats.animal && this.type == 1)
		{
			this.speed = 15f;
		}
		else if (this.type == 0 || this.type == 1)
		{
			this.speed = 50f;
		}
		else
		{
			this.speed = 60f;
		}
		if (powerDraw || this.type == 3 || this.type == 2)
		{
			if (this.type == 3 && this.target)
			{
				this.targetPosition = this.target.tr.position;
			}
			this.DrawArc();
			this.currentIndex = -1;
		}
		this.hasInfo = true;
		this.distance = Vector3.Distance(this.action.character.torsoPosition, this.outOfSightPosition);
		Links.x.combat.Projectile(this, true);
		this.box = base.gameObject.AddComponent<BoxCollider>();
		if (!this.box)
		{
			this.box = base.gameObject.AddComponent<BoxCollider>();
		}
		this.box.isTrigger = true;
		Bounds bounds = this.box.bounds;
		if (this.type == 1 && !this.actionCharacter.stats.animal)
		{
			bounds.Expand(0.5f);
		}
		else
		{
			bounds.Expand(0.2f);
		}
		this.box.size = bounds.size;
		this.box.isTrigger = true;
		this.rb = base.gameObject.GetComponent<Rigidbody>();
		if (!this.rb)
		{
			this.rb = base.gameObject.AddComponent<Rigidbody>();
		}
		this.rb.isKinematic = true;
		base.gameObject.layer = 23;
		if (Links.x.rallyStage.playing)
		{
			this.pocketProjectile = true;
			Links.x.rallyStage.projectile = this;
			Debug.Log("Pocket projectile");
		}
	}

	// Token: 0x0600084A RID: 2122 RVA: 0x000B4838 File Offset: 0x000B2A38
	public void SendEvent(bool didHit)
	{
		this.sentEvent = true;
		this.action.rangeIndex = this.index;
		if (!didHit && this.hit == 0f)
		{
			if (!this.action.character.mainSelected)
			{
				this.hit = 4f;
			}
			else
			{
				this.hit = 1f;
			}
		}
		this.action.OnAttackComplete(this.target, this.targetInteractive, this.hit, this.damage, this.attackTotal, this.defenseTotal, this.hitText, true, this.inventoryRow, this.index, false);
	}

	// Token: 0x0600084B RID: 2123 RVA: 0x000B48DC File Offset: 0x000B2ADC
	public void End(bool didHit)
	{
		if (this.inventoryRow._DmgMax <= 0 && this.inventoryRow._Effect != "" && this.actionCharacter)
		{
			Library.Abilities abilityRow = Links.x.library.GetAbilityRow(this.inventoryRow._Effect);
			GraphNode graphNode;
			if (this.type >= 2)
			{
				graphNode = this.targetNode;
				if (graphNode == null)
				{
					graphNode = this.actionCharacter.NearNode(this.item.position, false, 0, -1, 0);
				}
			}
			else
			{
				graphNode = this.actionCharacter.NearNode(this.item.position, false, 0, -1, 0);
			}
			if (graphNode != null && abilityRow != null)
			{
				TileEffects tileEffects = Links.x.tileEffects.AddComponent<TileEffects>();
				tileEffects.spellIndex = this.action.character.stats.SpellIndex(this.inventoryRow._Effect);
				tileEffects.paganRow = Links.x.library.GetPaganRow(this.inventoryRow._Effect);
				tileEffects.mainSkill = abilityRow._MainSkill;
				tileEffects.sourceLevel = this.inventoryRow._Level;
				tileEffects.source = this.action.character;
				if (abilityRow._RadiusCenter != "Mouse" && this.target)
				{
					tileEffects.singleTarget = this.target;
				}
				tileEffects.startNode = graphNode;
				tileEffects.Setup(abilityRow, null, null, null, null, this.actionCharacter);
			}
		}
		string text = this.inventoryRow._CharacterModel + "_projectile";
		Links.x.archives.RecycleItemBody(this.item.gameObject, text);
		if (!this.action)
		{
			Links.x.combat.Projectile(this, false);
			Object.Destroy(this);
			return;
		}
		if (this.inventoryRow._Name.Contains("Poison"))
		{
			GameObject pooledGameObject = Links.x.cellar.GetPooledGameObject(106);
			pooledGameObject.transform.position = this.item.position + new Vector3(0f, 0.5f, 0f);
			pooledGameObject.SetActive(true);
			MasterAudio.PlaySound3DAtVector3AndForget("WetPunch", base.gameObject.transform.position, 0.5f, new float?(1f), 0f, "", null);
		}
		if (!this.sentEvent && this.targetInteractive)
		{
			didHit = true;
		}
		if (!didHit && !this.sentEvent)
		{
			this.hit = 4f;
		}
		if (!this.sentEvent)
		{
			this.action.rangeIndex = this.index;
			this.action.OnAttackComplete(this.target, this.targetInteractive, this.hit, this.damage, this.attackTotal, this.defenseTotal, this.hitText, true, this.inventoryRow, this.index, false);
		}
		this.sentEvent = true;
		if (this.lineRendererToDrawTrajectory)
		{
			Object.Destroy(this.lineRendererToDrawTrajectory);
		}
		Links.x.combat.Projectile(this, false);
		this.action.RemoveProjectile(this);
		Object.Destroy(this.box);
		Object.Destroy(this.rb);
		Object.Destroy(this);
	}

	// Token: 0x0600084C RID: 2124 RVA: 0x000B4C3C File Offset: 0x000B2E3C
	public void DrawArc()
	{
		Vector3 position = this.item.position;
		this.trajectoryModifier = 1;
		this.initialVelocity = 6f;
		Quaternion quaternion = this.GetBearing(position, this.targetPosition);
		this.item.rotation = quaternion;
		this.CalculateTrajectory();
	}

	// Token: 0x0600084D RID: 2125 RVA: 0x000B4C88 File Offset: 0x000B2E88
	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: 0x0600084E RID: 2126 RVA: 0x000B4DBC File Offset: 0x000B2FBC
	public RaycastHit CalculateTrajectory()
	{
		RaycastHit raycastHit = default(RaycastHit);
		Vector3 position = this.item.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.item.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.sightBlockers))
					{
						flag = true;
					}
					this.prevPos = vector;
				}
				this.arcPoints.Add(vector);
			}
		}
		return raycastHit;
	}

	// Token: 0x04000CF2 RID: 3314
	[Header("INFO")]
	public Transform item;

	// Token: 0x04000CF3 RID: 3315
	public bool hasEffect;

	// Token: 0x04000CF4 RID: 3316
	private Vector3 startPosition;

	// Token: 0x04000CF5 RID: 3317
	public Vector3 targetPosition;

	// Token: 0x04000CF6 RID: 3318
	public Vector3 outOfSightPosition;

	// Token: 0x04000CF7 RID: 3319
	public Actions action;

	// Token: 0x04000CF8 RID: 3320
	public Library.Inventory inventoryRow;

	// Token: 0x04000CF9 RID: 3321
	public int index;

	// Token: 0x04000CFA RID: 3322
	private float t;

	// Token: 0x04000CFB RID: 3323
	public Character target;

	// Token: 0x04000CFC RID: 3324
	private Character actionCharacter;

	// Token: 0x04000CFD RID: 3325
	private float hit;

	// Token: 0x04000CFE RID: 3326
	private float damage;

	// Token: 0x04000CFF RID: 3327
	private string attackTotal;

	// Token: 0x04000D00 RID: 3328
	private string defenseTotal;

	// Token: 0x04000D01 RID: 3329
	private string hitText;

	// Token: 0x04000D02 RID: 3330
	private bool hasInfo;

	// Token: 0x04000D03 RID: 3331
	public int type;

	// Token: 0x04000D04 RID: 3332
	private Transform savedParent;

	// Token: 0x04000D05 RID: 3333
	private Vector3 originalTargetPosition;

	// Token: 0x04000D06 RID: 3334
	private bool findingNodes;

	// Token: 0x04000D07 RID: 3335
	private Vector3 previousItemPosition;

	// Token: 0x04000D08 RID: 3336
	private GameObject targetInteractive;

	// Token: 0x04000D09 RID: 3337
	private BoxCollider box;

	// Token: 0x04000D0A RID: 3338
	private Rigidbody rb;

	// Token: 0x04000D0B RID: 3339
	private bool sentEvent;

	// Token: 0x04000D0C RID: 3340
	private bool done;

	// Token: 0x04000D0D RID: 3341
	private bool startedInPocket;

	// Token: 0x04000D0E RID: 3342
	private MeshRenderer[] meshRenderers;

	// Token: 0x04000D0F RID: 3343
	public GraphNode targetNode;

	// Token: 0x04000D10 RID: 3344
	private float speed = 60f;

	// Token: 0x04000D11 RID: 3345
	[Header("ARC")]
	public bool hasPowerDraw;

	// Token: 0x04000D12 RID: 3346
	private float gravity = -9.81f;

	// Token: 0x04000D13 RID: 3347
	private float initialVelocity = 3f;

	// Token: 0x04000D14 RID: 3348
	private Quaternion bearing;

	// Token: 0x04000D15 RID: 3349
	private int trajectoryModifier = 1;

	// Token: 0x04000D16 RID: 3350
	private Vector3 projectileVelocity;

	// Token: 0x04000D17 RID: 3351
	private float trajectoryPrecision = 20f;

	// Token: 0x04000D18 RID: 3352
	private int maximumIterations = 300;

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

	// Token: 0x04000D1A RID: 3354
	public LineRenderer lineRendererToDrawTrajectory;

	// Token: 0x04000D1B RID: 3355
	private Vector3 prevPos;

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

	// Token: 0x04000D1D RID: 3357
	public int currentIndex;

	// Token: 0x04000D1E RID: 3358
	private Vector3 a;

	// Token: 0x04000D1F RID: 3359
	private Vector3 b;

	// Token: 0x04000D20 RID: 3360
	private Vector3 previousPosition;

	// Token: 0x04000D21 RID: 3361
	private bool mainCharacter;

	// Token: 0x04000D22 RID: 3362
	private float distance;

	// Token: 0x04000D23 RID: 3363
	private float wait;

	// Token: 0x04000D24 RID: 3364
	private float slow;

	// Token: 0x04000D25 RID: 3365
	private Vector3 turn;

	// Token: 0x04000D26 RID: 3366
	public bool startedinRally;

	// Token: 0x04000D27 RID: 3367
	public bool turnedOffForSynergy;

	// Token: 0x04000D28 RID: 3368
	public bool pocketProjectile;
}
