using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
public class PlayerMoviment : MonoBehaviour
{

    private  Rigidbody2D   feijaoPlayer;
    private      bool      gameover = false;
    private      Animator  anim;
    public       float     velocidadeMovimento;
    public       float     upforce = 400;

    // sistema de Hud


    public int seiva;
    public int agua;
    public Text aguaTexto, seivaTexto;
    
    // Start is called before the first frame update
    void Start()
    {
        feijaoPlayer = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        StartCoroutine("contagemDeAgua");
    }

    // Update is called once per frame
    void Update()
    {
        if(!gameover)
        {
        float horizontal = Input.GetAxis("Horizontal"); // personagem planando 
        float vertical = Input.GetAxis ("Vertical");
        feijaoPlayer.velocity = new Vector2(horizontal * velocidadeMovimento, vertical = 0);
        anim.SetTrigger("");
       
        if(Input.GetKeyDown(KeyCode.Space)) // personagem subindo
        {
            feijaoPlayer.velocity = Vector2.zero;
            feijaoPlayer.AddForce(new Vector2(0,upforce));
            anim.SetTrigger("");
        }
    
        }
    }
    private void OnCollisionEnter2D (Collision2D collision)
    {
        feijaoPlayer.velocity = Vector2.zero;
        gameover = true;
        anim.SetTrigger("GameOver");
        SceneManager.LoadScene("Menu");
    }
     IEnumerator contagemDeAgua()
    {
        aguaTexto.text = agua.ToString();
        yield return new WaitForSeconds(1);

        agua -=1;
        upforce -=10;

        if (agua > 0)
        {
            StartCoroutine("contagemDeAgua");
        }
        else
        {
            SceneManager.LoadScene("Menu");
        }
    }
   
    void OnTriggerEnter2D( Collider2D col)
   {
    if (col.tag == "ForçaAgua")
    {
        agua += 10;
        upforce += 15;
        aguaTexto.text = agua.ToString();
        Destroy(col.gameObject);
      
    }  
      if (col.tag == "SeivaForce")
        {
            seiva += 1;
            seivaTexto.text = seiva.ToString();
            Destroy(col.gameObject);
        }
         if (col.tag == "Brotou")
        {
            SceneManager.LoadScene("Brotou");
        }
   }

}
