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

public class Osito : MonoBehaviour
{
    public Rigidbody2D rb;
    public int F,Anim,Speed;
    public bool Salto;
    public Animator anim;
    public float TiempoGeneral;
    public bool Head, Body, Legs,NRobot;
    public int Puntuacion;
    public Text Puntos;
    public int Vidas;
    Animator m_Animator;


    public AudioSource Sonidos,Auch,Points;    
    public AudioClip [] Audio;

    // Start is called before the first frame update
    void Start()
    {        
        rb = GetComponent<Rigidbody2D>();        
        Speed = 1;
        Head = false;
        Body = false;
        Legs = false;
        NRobot = false;

        m_Animator = gameObject.GetComponent<Animator>();        

    }

    // Update is called once per frame
    void Update()
    {
        TiempoGeneral += Time.deltaTime;

        if (Input.GetKeyDown(KeyCode.Space)&& Salto == true)
        {                        
            Sonidos.clip = Audio[1];
            Sonidos.Play();
            Sonidos.loop = false;            
            rb.AddForce(new Vector3(0, F, 0), ForceMode2D.Impulse);
            Salto = false;
        }       

        if (TiempoGeneral > 10 )
        {
            Speed = 2;
            Sonidos.pitch = 1.2f;
        }
        if (Puntuacion >= 2)
        {
            Speed = 3;
            Sonidos.pitch = 1.5f;
        }
        if (Puntuacion >= 5)
        {
            Speed = 3;
            Sonidos.pitch = 1.8f;
        }

        anim = gameObject.GetComponent<Animator>();
        anim.speed = Speed;

        if (Head == true && Body && true && Legs == true)
        {
            Puntuacion = Puntuacion + 1;
            Head = false;
            Body = false;
            Legs = false;
            Points.clip = Audio[4];
            Points.Play();
        }
        Puntos.text = Puntuacion.ToString();

    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Piso" || collision.gameObject.tag == "BoteBasura")
        {
            Sonidos.clip = Audio[0];
            Sonidos.Play();
            Sonidos.loop = true;
            Salto = true;
            anim.SetBool("Salto", false);
        }

        if (collision.gameObject.tag == "Head")
        {
            Head = true;
            Points.clip = Audio[3];
            Points.Play();
        }
        if (collision.gameObject.tag == "Body")
        {
            Body = true;
            Points.clip = Audio[3];
            Points.Play();
        }
        if (collision.gameObject.tag == "Legs")
        {
            Legs = true;
            Points.clip = Audio[3];
            Points.Play();
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Piso" || collision.gameObject.tag == "BoteBasura")
        {            
            Salto = false;
            anim.SetBool("Salto", true);
        }
    }
    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "BoteBasura")
        {
            Auch.clip = Audio[2];
            Auch.Play();
            Auch.loop = false;
            Vidas = Vidas - 1;
            anim.SetBool("Daño", true);            
        }

        if (collision.gameObject.tag == "BoteBasura" && Vidas <= 0 && Puntuacion < 5)
        {
            Application.LoadLevel("Endgame");
        }
        if (collision.gameObject.tag == "BoteBasura" && Vidas <= 0 && Puntuacion >= 5 )
        {
            Application.LoadLevel("FinalBueno");
        }
        if (collision.gameObject.tag == "BoteBasura" && Vidas <= 0 && Puntuacion >= 10)
        {
            Application.LoadLevel("FinalMalo");
        }
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "BoteBasura")
        {            
            anim.SetBool("Daño", false);
        }
    }
}
