using UnityEngine;

public class PlayerControler : MonoBehaviour
{
[SerializeField] private Rigidbody2D RB;
public float speed = 5f;

//跳跃相关变量
public int jumpamound = 2;
public int jumpamoundmax = 2;
public float jumpforce = 7f;
bool jumpPressed = false;

//酷豹时间相关变量
public float MaxCoyoteTime = 0.5f;
float CoyoteTime = 0f;
bool CoyoteTimeBool = false;
public bool CanCoyoteTime = false;

//冲刺相关变量
public float dashforce = 10f;
public bool dashPressed = false;
public float dashTime = 0.2f;
public float dashTimeLeft = 0;

void Start()
{
    RB = GetComponent<Rigidbody2D>();
    CoyoteTime = MaxCoyoteTime;
}

void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        jumpPressed = true;
    }
    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
        dashPressed = true;
        dashTimeLeft = dashTime;
    }
}

void FixedUpdate()
{
    JumpHandeling();
    CoyoteTimeHandeling();

    if (!dashPressed)
    {
        MovementHandeling();
    }
    else
    {
        DashHandeling();
    }
}

void DashHandeling()
{
    float movehorizontal = Input.GetAxis("Horizontal");
    float movevertical = Input.GetAxis("Vertical");

    if (dashPressed)
    {
        dashTimeLeft -= Time.deltaTime;
        if (dashTimeLeft > 0)
        {
            Vector2 dashdir = new Vector2(movehorizontal, movevertical);
            dashdir.Normalize();

            if (dashdir == Vector2.zero)
            {
                dashdir = new Vector2(transform.localScale.x, 0);
            }
            if (dashdir.y != 0)
            {
                if (dashdir.y > 0)
                {
                    RB.linearVelocity = -dashdir * dashforce;
                }
                else
                {
                    RB.linearVelocity = dashdir * dashforce;
                }
            }

            RB.linearVelocity = dashdir * dashforce;

            RB.gravityScale = 0;
        }
        else if (dashTimeLeft <= 0)
        {
            dashPressed = false;
            RB.gravityScale = 1f;
        }
    }
}

void MovementHandeling()
{
    float movehorizontal = Input.GetAxis("Horizontal");
    RB.linearVelocity = new Vector2(movehorizontal * speed * Time.deltaTime, RB.linearVelocity.y);
}

void JumpHandeling()
{
    if (jumpPressed && (jumpamound > 0 || CanCoyoteTime))
    {
        Vector2 velocity = RB.linearVelocity;
        velocity.y = 0f;
        RB.linearVelocity = velocity;

        RB.AddForce(new Vector2(0, jumpforce), ForceMode2D.Impulse);
        jumpPressed = false;
        jumpamound--;

        CanCoyoteTime = false;
        CoyoteTime = 0;
    }
}

void CoyoteTimeHandeling()
{
    if (CoyoteTimeBool)
    {
        if (CoyoteTime > 0)
        {
            CoyoteTime -= Time.deltaTime;
            CanCoyoteTime = true;
        }
        else if (CoyoteTime <= 0)
        {
            CanCoyoteTime = false;
            jumpamound = 0;
        }
    }
    else if (!CoyoteTimeBool)
    {
        jumpamound = 1;
        CanCoyoteTime = true;
    }
}

void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("ground"))
    {
        Debug.Log("ground");
        CoyoteTimeBool = true;
        jumpamound = jumpamoundmax;
        CoyoteTime = MaxCoyoteTime;
        CanCoyoteTime = true;
    }
}

void OnCollisionExit2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("ground"))
    {
        CoyoteTimeBool = true;
        CoyoteTime = MaxCoyoteTime;
    }
}

void OnCollisionStay2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("ground"))
    {
        jumpamound = jumpamoundmax;
    }
}

}