using UnityEngine;
using System.Collections;

public class Canhao : MonoBehaviour {
	public bool naMiraL,naMiraR,naMiraS,naMiraN;
	public bool podeMiraL,podeMiraR,podeMiraS,podeMiraN;
	public Transform alvoL,alvoR,alvoS,alvoN;
	public Transform missil;
	public float cooldown;
	private float contador;
	public float dano; //0.1 0u 0.2 ou 0.4
	private Transform missilLancado;
	// Use this for initialization
	void Start () {
		contador = 0.0f;
	}
	
	// Update is called once per frame
	void Update () {
		contador -= 0.01f;
		if (contador > 0)
						this.gameObject.GetComponentInChildren<BarraCoolDown> ().alteraBarra ((cooldown-contador)/cooldown);

		naMiraL=Physics2D.Linecast (this.transform.position,alvoL.position,
		                    1<< LayerMask.NameToLayer("zombie"));
		naMiraR=Physics2D.Linecast (this.transform.position,alvoR.position,
		                            1<< LayerMask.NameToLayer("zombie"));
		naMiraS=Physics2D.Linecast (this.transform.position,alvoS.position,
		                            1<< LayerMask.NameToLayer("zombie"));
		naMiraN=Physics2D.Linecast (this.transform.position,alvoN.position,
		                            1<< LayerMask.NameToLayer("zombie"));
				
		if (podeMiraL && naMiraL && contador < 0.0f) {
						contador = cooldown;
						missilLancado = Instantiate (missil, this.transform.position, alvoL.transform.rotation) as Transform;
						missilLancado.GetComponent<Missil> ().dano = -dano;
				}
		if (podeMiraR && naMiraR && contador < 0.0f) {
			contador = cooldown;
			missilLancado = Instantiate (missil, this.transform.position, alvoR.transform.rotation) as Transform;
			missilLancado.GetComponent<Missil> ().dano = -dano;
		}
		if (podeMiraN && naMiraN && contador < 0.0f) {
			contador = cooldown;
			missilLancado = Instantiate (missil, this.transform.position, alvoN.transform.rotation) as Transform;
			missilLancado.GetComponent<Missil> ().dano = -dano;
		}
		if (podeMiraS && naMiraS && contador < 0.0f) {
			contador = cooldown;
			missilLancado = Instantiate (missil, this.transform.position, alvoS.transform.rotation) as Transform;
			missilLancado.GetComponent<Missil> ().dano = -dano;
		}



	}

}
