using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Controller : MonoBehaviour {

	private Animator Anime;
	private Rigidbody2D rb;
	public int forceJump;	

	public Transform groundCheck;
	public bool grounded;
	public LayerMask whatIsGround;

	public bool slide;
	public float slideTemp;
	public float timeTemp;

	public static int pontuacao;
	public Text txtPontos;

	// Use this for initialization
	void Start () {
		pontuacao = 0;
        rb = GetComponent<Rigidbody2D>();
        Anime = GetComponent<Animator>();
        PlayerPrefs.SetInt ("pontuacao", pontuacao);
	}

	// Update is called once per frame
	void Update () {

		txtPontos.text = pontuacao.ToString ();

		if (Input.GetButtonDown ("Fire1") && grounded) {
			rb.AddForce (new Vector2 (0, forceJump));
			slide = false;
		}

		if (Input.GetButtonDown ("Fire2")) {
			slide = true;
			timeTemp = 0;
		}

		if (slide) {
			timeTemp += Time.deltaTime;
			if (timeTemp >= slideTemp) {
				slide = false;
			}
		}
		grounded = Physics2D.OverlapCircle (groundCheck.position, -0.796f, whatIsGround);


		//Anime.SetBool ("jump", !grounded);
		//Anime.SetBool ("slide", slide);
	}

	void OnTriggerEnter2D(){
		PlayerPrefs.SetInt ("pontuacao", pontuacao);
		if(pontuacao > PlayerPrefs.GetInt("recorde")){
			PlayerPrefs.SetInt ("recorde", pontuacao);
		}
			
		SceneManager.LoadScene ("GameOver");
	}

}
