using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlightController2D : MonoBehaviour {

		public static FlightController2D instance;

		public float speed = .5f;
		public Transform spriteTransform;
		public Quaternion testQ;

		public AudioSource screech;
		public AudioSource constantFlutter;
		public AudioSource dirChange;

		public bool pause = false;

		void Start()
		{
			instance = this;
		}

		// Update is called once per frame
		void Update () {


			if(Input.GetKey(KeyCode.Q) || Input.GetKey("escape"))
			{
				 Application.Quit();
			}

			if(pause)
			return;
			

			if(Input.GetKey(KeyCode.Space))
			{
				if(release)
				{
					EchoLocate();
					screech.Play();
				}
				
			}

			

			 if (Input.GetKey(KeyCode.RightArrow)) {
				transform.Translate (Vector2.right * speed);
				spriteTransform.rotation =  Quaternion.Euler(new Vector3(0,0,0));
				//dirChange.Play();
			}
			 if (Input.GetKey(KeyCode.LeftArrow)) {
				transform.Translate (Vector2.left * speed);
				spriteTransform.rotation = Quaternion.Euler(new Vector3(0,180,0));
				//dirChange.Play();
			}
			 if (Input.GetKey(KeyCode.UpArrow) && (Input.GetKey(KeyCode.RightArrow))) {
				transform.Translate ((Vector2.up * speed)/2);
				spriteTransform.rotation = Quaternion.Euler(new Vector3(0,0,45));
				//dirChange.Play();
			}
			 if (Input.GetKey(KeyCode.UpArrow) && (Input.GetKey(KeyCode.LeftArrow))) {
				transform.Translate ((Vector2.up * speed)/2);
				spriteTransform.rotation = new Quaternion(45,180,0,0);
				//dirChange.Play();
			}
			 if (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.RightArrow)) {
				transform.Translate ((Vector2.down * speed)/2);
				spriteTransform.rotation = Quaternion.Euler(new Vector3(0,0,-45));
				//dirChange.Play();
			}
			 if (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.LeftArrow)) {
				transform.Translate ((Vector2.down * speed)/2);
				spriteTransform.rotation = Quaternion.Euler(new Vector3(0,180,-45));
				//dirChange.Play();
			}
			 if (Input.GetKey(KeyCode.DownArrow)) {
				transform.Translate (Vector2.down * speed);
				//dirChange.Play();
				//spriteTransform.rotation = Quaternion.Euler(new Vector3(0,180,-45));
			}
			 if (Input.GetKey(KeyCode.UpArrow)) {
				transform.Translate (Vector2.up * speed);
				//dirChange.Play();
				//spriteTransform.rotation = Quaternion.Euler(new Vector3(0,180,-45));
			}

			
			
			 
		}

		public Transform EchoPrefab;
		
		public float timeEchoLock = .3f;

		


		void EchoLocate()
		{
			release = false;
			Transform g = GameObject.Instantiate(EchoPrefab, transform.position, spriteTransform.rotation).transform;			
			//g.GetComponent<Echo>().SetInitialDirection(spriteTransform.rotation.eulerAngles);
			//g.GetComponent<Rigidbody2D>().velocity = g.transform.forward * 6;
			Invoke("Unlock", timeEchoLock);
		}
		bool release = true;
		void Unlock()
		{
			release = true;
		}
	}
