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

public class PlayerController : MonoBehaviour
{
[Header("Horizontal Movement Settings")]
[SerializeField] private float walkSpeed = 1;
[Space(5)]

[Header("Vertical Movement Settings")]
[SerializeField] private float jumpForce = 45;
private int jumpBufferCounter = 0;
[SerializeField] private int jumpBufferFrames;
private float coyoteTimeCounter = 0;
[SerializeField] private float coyoteTime;
private int airJumpCounter = 0;
[SerializeField] private int maxAirJumps;
[Space(5)]

[Header("Ground Check Settings")]
[SerializeField] private Transform groundCheckPoint;
[SerializeField] private float groundCheckY = 0.2f;
[SerializeField] private float groundCheckX = 0.5f;
[SerializeField] private LayerMask whatIsGround;
[Space(5)]

[Header("Dash Settings")]
[SerializeField] private float dashSpeed;
[SerializeField] private float dashTime;
[SerializeField] private float dashCooldown;
[Space(5)]


PlayerStateList pState;
private Rigidbody2D rb;
private float xAxis;
private float gravity;
Animator anim;
private bool canDash;
private bool dashed;


public static PlayerController Instance;

private void Awake()
{
if(Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
Instance = this;
}
}

// Start is called before the first frame update
void Start()
{
pState = GetComponent<PlayerStateList>();

rb = GetComponent<Rigidbody2D>();

anim = GetComponent<Animator>();

gravity = rb.gravityScale;
}

// Update is called once per frame
void Update()
{
Getinputs();
UpdateJumpVariables();

if (pState.dashing) return;
Flip();
Move();
Jump();
StartDash();
}

void Getinputs()
{
xAxis = Input.GetAxisRaw("Horizontal");
}

void Flip()
{
if(xAxis < 0)
{
transform.localScale = new Vector2(-1, transform.localScale.y);
}
else if(xAxis > 0)
{
transform.localScale = new Vector2(1, transform.localScale.y);
}
}


private void Move()
{
rb.velocity = new Vector2(walkSpeed * xAxis, rb.velocity.y);
anim.SetBool("Walking", rb.velocity.x != 0 && Grounded());
}

void StartDash()
{
if(Input.GetButtonDown("Dash") && canDash && !dashed)
{
StartCoroutine(Dash());
dashed = true;
}

if(Grounded())
{
dashed = false;
}
}

IEnumerator Dash()
{
canDash = false;
pState.dashing = true;
anim.SetTrigger("Dashing");
rb.gravityScale = 0; //While dashing, gravity doesn't have effect
rb.velocity = new Vector2(transform.localScale.x * dashSpeed, 0);
yield return new WaitForSeconds(dashTime);
rb.gravityScale = gravity;
pState.dashing = false;
yield return new WaitForSeconds(dashCooldown);
canDash = true;
}

public bool Grounded()
{
if(Physics2D.Raycast(groundCheckPoint.position, Vector2.down, groundCheckY, whatIsGround)
|| Physics2D.Raycast(groundCheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround)
|| Physics2D.Raycast(groundCheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround))
{
return true;
}
else
{
return false;
}
}

void Jump()
{
if(Input.GetButtonUp("Jump") && rb.velocity.y >0)
{
rb.velocity = new Vector2(rb.velocity.x, 0);

pState.jumping = false;
}

if (!pState.jumping)
{
if (jumpBufferCounter > 0 && coyoteTimeCounter > 0)
{
rb.velocity = new Vector3(rb.velocity.x, jumpForce);

pState.jumping = true;
}
else if (!Grounded() && airJumpCounter < maxAirJumps && Input.GetButtonDown("Jump"))
{
pState.jumping = true;

airJumpCounter++;

rb.velocity = new Vector3(rb.velocity.x, jumpForce);
}
}



anim.SetBool("Jumping", !Grounded());
}

void UpdateJumpVariables()
{
if (Grounded())
{
pState.jumping = false;
coyoteTimeCounter = coyoteTime;
airJumpCounter = 0;
}
else
{
coyoteTimeCounter -= Time.deltaTime;
// Time.deltaTime is the amount of the time between each frame
// By Subtracting the Time.deltaTime value every frame, It decreases our coyoteTimeCounter by 1 every second
}
if (Input.GetButtonDown("Jump"))
{
jumpBufferCounter = jumpBufferFrames;
}
else
{
jumpBufferCounter--;
}
}
}


아까 영상만 올리고 코드 안올렸던 뉴비야....

너무 긴거같아서 파일 올리려했는데 지원하지 않는 확장자여서 메모장이랑 여기에 그냥 복붙했어...

뭐지 메모장 txt파일도 안올라가는건가?