using UnityEngine;
using System.Collections;

public class touch : MonoBehaviour {
	public GameObject TANKP;
	public GameObject posbulletp;

	public Transform tank;
	public bool chkdown;
	public bool chkdonwD;
	public bool chkdonwR;
	public bool chkdonwL;
	public GameObject bullet;
	public GameObject planreset;
	public Transform posbullet;
	public Animator anim;
	public GameObject chkbullet;

	bool chkedfaire;
	bool chkedfaire2;
	// Use this for initialization
	void Start () {
		InvokeRepeating ("FireFalse2", 0, 0.5f);
	}
	
	// Update is called once per frame
	void Update () {
		if (TANKP == null && posbulletp == null) {
			TANKP = GameObject.Find ("PICSELART--TANK1(Clone)");
			posbulletp = GameObject.Find ("posbullet");

		}
		if (TANKP != null && posbulletp != null) {
			
			
			tank=TANKP.transform;
			posbullet=posbulletp.transform;
			anim=tank.GetComponent<Animator>();
		}
	

	if (tank != null && Time.timeScale!=0 ) {

			if(chkedfaire==true && chkedfaire2==false){
				Fire();
				chkedfaire2=true;
			}

			//UP
			if (chkdown == true) {
				tank.rotation = Quaternion.Euler (new Vector3 (tank.rotation.x, tank.rotation.y, 180));
				tank.position = new Vector2 (tank.position.x, tank.position.y + 0.05f);
			}

			//DONW

			if (chkdonwD == true) {

				tank.rotation = Quaternion.Euler (new Vector3 (tank.rotation.x, tank.rotation.y, 0));
				tank.position = new Vector2 (tank.position.x, tank.position.y - 0.05f);

			}

			//	RIGHT
			if (chkdonwR == true) {

				tank.rotation = Quaternion.Euler (new Vector3 (tank.rotation.x, tank.rotation.y, 90));
				tank.position = new Vector2 (tank.position.x + 0.05f, tank.position.y);

			}

			//LAFT
			if (chkdonwL == true) {

				tank.rotation = Quaternion.Euler (new Vector3 (tank.rotation.x, tank.rotation.y, -90));
				tank.position = new Vector2 (tank.position.x - 0.05f, tank.position.y);

			}
		}
	}


	//up
	public void donw(){
		if (tank != null) {
			if (chkdonwD == false && chkdonwR == false && chkdonwL == false) {
				chkdown = true;
				anim.SetBool ("walk", true);
			}
		}
	
	}
	public void up(){
		if (tank != null) {
			anim.SetBool ("walk", false);
			chkdown = false;
		}
	}


	// DONW
	public void DonwD(){
		if (tank != null) {
			if (chkdown == false && chkdonwR == false && chkdonwL == false) {
				chkdonwD = true;
				anim.SetBool ("walk", true);
			}
		}
	}
	public void UPD(){
		if (tank != null) {
			anim.SetBool ("walk", false);
			chkdonwD = false;
		}
	}

	//Right
	public void DonwR(){
		if (tank != null) {
			if (chkdown == false && chkdonwD == false && chkdonwL == false) {
				chkdonwR = true;
				anim.SetBool ("walk", true);
			}
		}
		
	}
	public void UPR(){
		if (tank != null) {
			anim.SetBool ("walk", false);
			chkdonwR = false;
		}
	}

	//LAFt

	public void DonwL(){
		if (tank != null) {
			if (chkdown == false && chkdonwD == false && chkdonwR == false) {
				chkdonwL = true;
				anim.SetBool ("walk", true);
			}
		}
	}
	public void UPL(){
		if (tank != null) {
			anim.SetBool ("walk", false);
			chkdonwL = false;
		}
	}
	public void Fire(){
		chkbullet = GameObject.FindWithTag ("bullet"); 


		if (tank != null) {

			if(chkbullet==null){
			Instantiate (bullet, posbullet.position, posbullet.rotation);
			}
		}

	

	}


	public void FireTrue(){

		chkedfaire = true;
	}


	public void FireFalse(){

		chkedfaire = false;

	}

	public void FireFalse2(){
		
		chkedfaire2 = false;
		
	}
	public void puse(){
		if (Time.timeScale == 1) {

			planreset.SetActive(true);
			Time.timeScale = 0;
		} else {
			Time.timeScale = 1;
			planreset.SetActive(false);
		}

	}
	 
	public void reset(){

		Application.LoadLevel ("lave1");
		Time.timeScale = 1;
	}


}
