using UnityEngine;
using System.Collections;

public class Weapon : MonoBehaviour 
{
	public enum weapon {sword, gun}
	public weapon selectWeapon;

	public GameObject sword;
	public GameObject gun;

	public float fireRate = 0f;
	public float damage = 10f;
	public LayerMask whatToHit;


	float timeToFire = 0f;
	Transform firePoint;
	Transform endFire;


	void Start () 
	{
		firePoint = transform.FindChild ("FirePoint");
		endFire = transform.FindChild ("EndPoint");

		if (firePoint == null) 
		{
			Debug.LogError ("No encontro el firePoint bitch");
		}
	}


	void Update () 
	{
		Fire ();
	}


	void Fire()
	{
		if (fireRate == 0) 
		{
			if (Input.GetKeyDown(KeyCode.X))
				{
					Shoot ();
				}
		}
		else
		{
			if (Input.GetKey (KeyCode.X) && Time.time > timeToFire) 
			{
				timeToFire = Time.time + 1 / fireRate;
				Shoot ();
			}
		}
	}


	void Shoot()
	{
		Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
		Vector2 endFirePosition = new Vector2 (endFire.position.x,endFire.position.y); // (endFire.position.x, endFire.position.y);
		RaycastHit2D hit = Physics2D.Raycast (firePointPosition, endFirePosition - firePointPosition, 4.5f, whatToHit);

		// PRUEBAS
		Debug.DrawLine (firePointPosition,endFirePosition, Color.cyan);		// Distancia
		if (hit.collider != null) 	// Donde choca con algo
		{
			Debug.DrawLine (firePointPosition, hit.point, Color.red);
		}
	}

}



