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

public class Player_Master : MonoBehaviour
{

    public float maxSpeed = 3f;
    public float speed = 50f;
    public float jumpPower = 150f;

    public int curLight;


    public bool grounded;
    public bool canJump;
    public bool firstLight;

    public GameObject fog;


    private Rigidbody2D rb2d;
    private Animator anim;


    public void Start()
    {

        firstLight = false;

        rb2d = gameObject.GetComponent<Rigidbody2D>();
        anim = gameObject.GetComponent<Animator>();

        curLight = 0;


    }


    void Update()
    {


        anim.SetBool("Grounded", grounded);

        anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));

        if (Input.GetAxis("Horizontal") < -0.1f)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }

        if (Input.GetAxis("Horizontal") > 0.1f)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }

        if (Input.GetButtonDown("Jump"))
        {
            if (grounded)
            {
                canJump = true;
                rb2d.AddForce(Vector2.up * jumpPower);
            }
            else
            {
                canJump = false;

            }
        }
        if (firstLight == true)
        {
            if (curLight <= 0)
            {
                //Die();
            }
                
        }




    }

    void FixedUpdate()
    {
        float hor = Input.GetAxis("Horizontal");



        rb2d.AddForce((Vector2.right * speed) * hor);

        if (rb2d.velocity.x > maxSpeed)
        {
            rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
        }

        if (rb2d.velocity.x < -maxSpeed)
        {
            rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
        }
    }


    public void Damage(int dmg)
    {
        curLight -= dmg;

        gameObject.GetComponent<Animation>().Play("Red_Flash");
    }

  

    public IEnumerator Knockback(float knockDur, float knockPow, Vector3 knockDir)
    {
        float timer = 0;

        rb2d.velocity = new Vector2(rb2d.velocity.x, 0);

        while (knockDur > timer)
        {
            timer += Time.deltaTime;

            rb2d.AddForce(new Vector3(knockDir.x * -100, knockDir.y * knockPow, transform.position.z));
        }

        yield return 0;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (gameObject.tag == "Lucecita")
        {
            firstLight = true;
            fog.gameObject.SetActive(true);

        }

    }

}


     
