using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class PlayerMovement : MonoBehaviour {
	float xMove = 0f;
	float yMove = 0f;
	public float xSpeed = 10f;
	public float ySpeed = 5f;
	Rigidbody2D rb;
	public float boundsLeft = -3f;
	public float boundsRight = 3f;
	public float boundsUp = -2f;
	public float boundsDown = -4f;



	void Start () {
		rb = GetComponent<Rigidbody2D> ();
	}
	void Update () {
		CheckInput ();
		CheckToRotate ();
	}
	void FixedUpdate(){
		Move ();
	CheckBounds ();
	}
	void CheckBounds(){
		Vector2 newPos;
		//horizontal
		if (transform.position.x < boundsLeft) {
			newPos = new Vector2 (boundsLeft, transform.position.y);
			transform.position = newPos;

		} else if (transform.position.x > boundsRight) {
			newPos = new Vector2 (boundsRight, transform.position.y);
			transform.position = newPos;
		}
		//vertical
		/*if (transform.position.y < boundsDown) {
			transform.position = new Vector2 (transform.position.x, boundsDown);
		} else if (transform.transform.position.y > boundsUp) {
			transform.position = new Vector2 (transform.position.x, boundsUp);
		}*/



	}
	void Move(){
		Vector2 newVelocity = new Vector2 (xMove, yMove);
		rb.velocity = newVelocity;
	}
	void CheckInput(){
		xMove = Input.GetAxis ("Horizontal") * xSpeed;
		yMove = Input.GetAxis ("Vertical")* ySpeed;

	}

	void CheckToRotate()
	{
		if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.W))
			{
			transform.eulerAngles = new Vector3 (0, 0, 45);
			}
		else if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.S))
		{
			transform.eulerAngles = new Vector3 (0, 0, 135);
		}
		else if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.W))
		{
			transform.eulerAngles = new Vector3 (0, 0, -45);
		}
		else if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.S))
		{
			transform.eulerAngles = new Vector3 (0, 0, -135);
		}





		else if (Input.GetKey(KeyCode.D))
		{
			//transform.Rotate(0, 0, -90);
			transform.eulerAngles = new Vector3 (0, 0, -90);
		}
		else if (Input.GetKey(KeyCode.S))
		{
			transform.eulerAngles = new Vector3 (0, 0, 180);
			
		}
		else if (Input.GetKey(KeyCode.A))
		{
			transform.eulerAngles = new Vector3 (0, 0, 90);
		}
		else if (Input.GetKey(KeyCode.W))
		{
			transform.eulerAngles = new Vector3 (0, 0, 0);
		}


	}

	


}
