using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

	public float movmentSpeed = 5f;
	public float mouseSens = 2f;
	public float upDownRange = 45f;
	public float jumpSpeed = 5f;
	public AudioClip sjump;
	public bool playerIsGrounded = true;





	float vertivalRotation = 0f;
	public float vertivalVelocity = 0f;

	AudioSource sound;


	CharacterController characterController;

	// Use this for initialization
	void Start () {




		sound = GetComponent<AudioSource>();

		characterController = GetComponent<CharacterController>();

		Screen.lockCursor = true;
		Screen.showCursor = false;
	
	}
	
	// Update is called once per frame
	void Update () {






		if(characterController.isGrounded){
			playerIsGrounded = true;
		}else{
			playerIsGrounded = false;
		}




		// Mouse Look
		float rotLeftRight = Input.GetAxis("Mouse X") * mouseSens;

		transform.Rotate(0,rotLeftRight,0);

		vertivalRotation -= Input.GetAxis("Mouse Y") * mouseSens;
		vertivalRotation = Mathf.Clamp(vertivalRotation, - upDownRange, upDownRange);

		Camera.main.transform.localRotation = Quaternion.Euler(vertivalRotation,0,0);


		// Key Movments

		float forwardSpeed = Input.GetAxis("Vertical") * movmentSpeed;
		float sideSpeed = Input.GetAxis("Horizontal") * movmentSpeed;



		vertivalVelocity += Physics.gravity.y * Time.deltaTime;

		if(characterController.isGrounded && Input.GetButtonDown("Jump")){
			vertivalVelocity = jumpSpeed;
			sound.PlayOneShot(sjump,1F);


		}

		Vector3 Speed = new Vector3(sideSpeed,vertivalVelocity,forwardSpeed);

		Speed = transform.rotation * Speed;

		characterController.Move(Speed * Time.deltaTime);

	
	}
	void OnMouseDown(){
		Screen.lockCursor = true;

	}

}
