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

public class PlayerControlledMovement : MonoBehaviour {
	Vector3 fromPoint;
	Vector3 nextPoint;
	//public Transform TouchPoint;
	public float speed;

	LineRenderer touchLiner;

	// Use this for initialization
	void Start () {
		//touchLiner = TouchPoint.GetComponent<LineRenderer> ();
	}
	
	// Update is called once per frame
	void Update () {
		//transform.Rotate (Vector3.back, Input.GetAxis ("Horizontal"));

		fromPoint = Vector3.up;
		if (Input.GetMouseButtonDown (0)) {
			nextPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
			nextPoint.z = 0;
			//TouchPoint.gameObject.SetActive (true);
		}
		if (Input.GetMouseButton(0)) {
			fromPoint = nextPoint;
			Vector3 targetPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
			targetPoint.z = 0;
			nextPoint = Vector3.RotateTowards (fromPoint, targetPoint, Time.deltaTime * speed /*360*/ * Mathf.Deg2Rad, 0.0f);
			float angleSpeed = Vector3.SignedAngle (fromPoint, nextPoint, Vector3.forward);
			transform.Rotate (Vector3.forward, angleSpeed);
			if (transform.rotation.eulerAngles.z < 210 || transform.rotation.eulerAngles.z > 330) {
				transform.Rotate (Vector3.forward, -angleSpeed);
				nextPoint = fromPoint;
			}
			//TouchPoint.position = nextPoint;
			//touchLiner.SetPosition (1, nextPoint.normalized*5);
		}
		if (Input.GetMouseButtonUp (0)) {
			//TouchPoint.gameObject.SetActive (false);
		}
	}
}
