using UnityEngine;
using System.Collections;

public class Projectile4 : MonoBehaviour
{
	public Controller player;

	public GameObject impactEffect;

	private Rigidbody2D myRigidBody2D;

	public float speed;

	public float rotationSpeed;

	private Transform target;

	public float damageToGive;

	// Use this for initialization
	void Start()
	{
		if (FindObjectOfType<Controller> () != null)
		{
			player = FindObjectOfType<Controller> ();

			myRigidBody2D = GetComponent<Rigidbody2D> ();

			target = player.transform;

			if (player.transform.position.x < transform.position.x)
			{
				rotationSpeed = -rotationSpeed;
			}
		}
	}

	// Update is called once per frame
	void Update ()
	{
		if (FindObjectOfType<Controller> () != null)
		{
			Vector3 angle = (target.position - transform.position).normalized;

			if (Time.timeScale != 0)
			{
				myRigidBody2D.AddForce (angle * speed);
				myRigidBody2D.angularVelocity = rotationSpeed;
			}
		}
	}

	void OnTriggerEnter2D(Collider2D other)
	{
		if (other.name != "Enemy")
		{
			Instantiate(impactEffect, transform.position, transform.rotation);
			Destroy (gameObject);

			if (other.name == "Player 1")
			{
				other.GetComponent<PlayerHealth> ().giveDamage (damageToGive);
			}

			if (other.name == "Player 2")
			{
				other.GetComponent<PlayerHealth> ().giveDamage (damageToGive);
			}
		}
	}
}
