using UnityEngine;
using System.Collections;

public class Player_Shoot : MonoBehaviour {
	Vector3 mouse_pos;
	Transform target;
	Vector3 object_pos;
	float angle = 0f;
	public GameObject projectile;
	public GameObject rocketspawn;
	GameObject projectile_dupe;
	Renderer dupe_render;
	//public float dupe_velocity;
	Vector3 rocket_spawn;
	Rigidbody2D r_body;
	//changes i made
	bool fired = false;
	// Use this for initialization
	//Vector3 rocket_spawn = Rocket_Spawn.transform;
	void Start () {
		target = GetComponent<Transform> ();
		r_body = GetComponent<Rigidbody2D> ();
	}
	
	// Update is called once per frame
	void Update () {
		mouse_pos = Input.mousePosition;
		mouse_pos.z = 5.23f; //The distance between the camera and object
		object_pos = Camera.main.WorldToScreenPoint(target.position);
		mouse_pos.x = mouse_pos.x - object_pos.x;
		mouse_pos.y = mouse_pos.y - object_pos.y;
		angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
		transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));


		if(Input.GetButtonDown("Fire1") && fired == false)
		{
			//anim.SetTrigger("Attack");
			//r_body.velocity = Vector2.zero;
			/*projectile_dupe = projectile;
			dupe_render = projectile_dupe.GetComponent<Renderer>();
			dupe_render.enabled = true;

			dupe_render = projectile_dupe.GetComponent<Renderer>();
			r_dupe = projectile_dupe.GetComponent<Rigidbody2D>();
			r_dupe.velocity = new Vector2(dupe_velocity, dupe_velocity);
			projectile_dupe.transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));*/
			//rocket_spawn = GetComponent<Vector3> ();
			rocket_spawn = rocketspawn.transform.position;
			GameObject bullet = Instantiate(projectile,  rocket_spawn, Quaternion.Euler(new Vector3(0, 0, angle))) as GameObject;


			bullet.GetComponent<Rocket>().ShootDirection( mouse_pos - transform.position);
			fired = true;
			Debug.Log(fired);
			Invoke("Fire_Delay", 0.33f);
			//yield return new WaitForSeconds(1);



		}
	}
	//void FixedUpdate() {

		//if (fired == true) {
			//yield return new WaitForSeconds (1);
			//fired = false;
		//}
	//}
	/*void LateUpdate() {
		if (fired == true) 
		{
			Invoke("Fire_Delay", 1f);
		}
	}*/
	void Fire_Delay() {
		fired = false;
	}	
}


