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

public class playercontroller : MonoBehaviour
{
	
	
	public float speed = 5.0f;
	public float gravity = 10.0f;
	public float turnSpeed = 60.0f;
	public GameManager manager;
	// private LayerMask layermask;
	public bool pause;
	public bool kematian;
	public AudioClip death;

	private CharacterController charactercontroller;
	private Animator anim;
	//    private Vector3 currentlooktarget = Vector3.zero;
	
	private Vector3 moveDirection = Vector3.zero;

	// Use this for initialization
	void Start ()
	{
		charactercontroller = GetComponent<CharacterController> ();

		anim = GetComponent<Animator> ();
		
	}
	
	// Update is called once per frame
	void Update ()
	{

		if (manager.state.Equals (GameState.GAMEPLAY)) {
			if (!pause) {
				if (!kematian) {
					anim.SetFloat ("moveSpeed", charactercontroller.velocity.magnitude);
					anim.SetFloat ("horizontalValue", Input.GetAxis ("Horizontal"));
					if(anim.GetFloat("moveSpeed")>.2f){
						if(!GetComponent<AudioSource>().isPlaying)
							GetComponent<AudioSource>().Play();
					}
					else {
						GetComponent<AudioSource>().Stop();
					}

					transform.Rotate (0, Input.GetAxis ("Horizontal") * turnSpeed * Time.deltaTime, 0);
					moveDirection = new Vector3 (0, 0, Input.GetAxis ("Vertical"));
					moveDirection = transform.TransformDirection (moveDirection);
					moveDirection *= speed;
					moveDirection.y -= gravity;
					//        charactercontroller.SimpleMove(MoveDirection * speed);
					charactercontroller.Move (moveDirection * Time.deltaTime);
				}
			}
		}
	}
	
	
	
}
