using UnityEngine;
using System.Collections;

public class touch : MonoBehaviour {
	public Transform tank;
	public bool chkdown;
	public bool chkdonwD;
	public bool chkdonwR;
	public bool chkdonwL;
	public GameObject bullet;
	public Transform posbullet;
	public Animator anim;
	public GameObject chkbullet;

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
	if (tank != null) {
			//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);
			}
		}
	}

}
