
using UnityEngine;
using System.Collections;

public class swipe : MonoBehaviour
{
		private Vector2 leftFingerPos = Vector2.zero;
		private Vector2 leftFingerLastPos = Vector2.zero;
		private Vector2 leftFingerMovedBy = Vector2.zero;
		public float slideMagnitudeX = 0.0f;
		public float slideMagnitudeY = 0.0f;
		public enum Direction
		{
				LEFT,
				RIGHT,
				UP,
				DOWN,
				NONE
		}

		public static Direction dir = Direction.NONE;

		void Update ()
		{

				if (Input.touchCount == 1) {
						Touch touch = Input.GetTouch (0);
						if (touch.phase == TouchPhase.Began) {
								leftFingerPos = Vector2.zero;
								leftFingerLastPos = Vector2.zero;
								leftFingerMovedBy = Vector2.zero;

								slideMagnitudeX = 0;
								slideMagnitudeY = 0;

								leftFingerPos = touch.position;
						} else if (touch.phase == TouchPhase.Moved) {
								leftFingerMovedBy = touch.position - leftFingerPos;
								leftFingerLastPos = leftFingerPos;

								leftFingerPos = touch.position;

								float alpha = Mathf.Acos ((leftFingerMovedBy.x + leftFingerMovedBy.y) / Mathf.Sqrt (2) / leftFingerMovedBy.magnitude);
								slideMagnitudeX = leftFingerMovedBy.magnitude * Mathf.Cos (alpha);
								slideMagnitudeY = leftFingerMovedBy.magnitude * Mathf.Sin (alpha);

								if (Mathf.Abs (slideMagnitudeX) > Mathf.Abs (slideMagnitudeY))
										dir = slideMagnitudeX < 0 ? Direction.LEFT : Direction.RIGHT;
								else
										dir = leftFingerMovedBy.y < 0 ? Direction.DOWN : Direction.UP;
						} else if (touch.phase == TouchPhase.Stationary) {

								leftFingerLastPos = leftFingerPos;
								leftFingerPos = touch.position;

								slideMagnitudeX = 0.0f;
								slideMagnitudeY = 0.0f;
						} else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) {
								dir = Direction.NONE;
								slideMagnitudeX = 0.0f;
								slideMagnitudeY = 0.0f;
						}
				}
		}
}