using UnityEngine;
using System.Collections;

public class Projectile : MonoBehaviour {

    [SerializeField] private float projAccel;
    [SerializeField] private GameObject explosion;
    private Color myColor;
    private Rigidbody2D body;
    private GameObject player;

    private float timer = 0;

	// Use this for initialization
	void Start () {
        body = GetComponent<Rigidbody2D>();

        transform.up = body.velocity.normalized;
    }

    public void SetPlayer(GameObject playerIn)
    {
        player = playerIn;
    }
	
	// Update is called once per frame
	void Update () {
        //GetComponent<Rigidbody2D>().velocity *= projAccel;

        timer += 1.0f * Time.deltaTime;
    }

    public void SetExplosionColor(Color colorIn)
    {
        myColor = colorIn;
    }

    void OnTriggerEnter2D(Collider2D col)
    {
		if (timer < .03f && col.gameObject == player)
		{
			return;
		}
		else if (col.gameObject.layer == LayerMask.NameToLayer("Explosion"))
		{
			return;
		}
		else
		{
			var proj = (GameObject)Instantiate(explosion, transform.position, transform.rotation);
			proj.GetComponent<SpriteRenderer>().color = myColor;
			proj.transform.parent = null;
			Destroy(this.gameObject);
		}
    }
}
