using System.Collections; using System.Collections.Generic; using UnityEngine; using static PlayerController; public class PlayerAttack : MonoBehaviour { Animator animator; PlayerController controller; public float attackTime = 0.5f; // 공격 후 쿨다운 public float attackCoolTime = 0.5f; private int attackCount = 0; // 현재 공격 횟수 private int attackIdx = 0; // 공격 애니메이션 인덱스 (0,1 반복) private bool canAttack = true; private void Start() { controller = GetComponent<PlayerController>(); animator = GetComponentInChildren<Animator>(); PlayManager.Instance.leftMouve += Attack; Debug.Log(123); } void Attack() { if(!canAttack || controller.playerState == PlayerState.Skill) return; controller.playerState = PlayerState.Attack; attackCount++; animator.SetInteger("AttackIdx", attackIdx); attackIdx = (attackIdx + 1) % 2; AttackRot(); animator.SetFloat("idleState", controller.idleState); if(coroutine != null) { StopCoroutine(coroutine); coroutine = StartCoroutine(AttackStopCoroutin()); } else { coroutine = StartCoroutine(AttackStopCoroutin()); } if (attackCount >= 3) { StartCoroutine(AttackCooldown()); // 마지막 공격이면 쿨다운 실행 } } Coroutine coroutine; IEnumerator AttackStopCoroutin() { yield return new WaitForSeconds(attackTime); AttackStop(); } private void AttackStop() { attackCount = 0; controller.playerState = PlayerState.Idle; animator.SetInteger("AttackIdx", -1); } public void AttackInitialized() { attackCount = 0; } IEnumerator AttackCooldown() { canAttack = false; yield return new WaitForSeconds(attackCoolTime); AttackStop(); // 마지막 공격 후 Stop canAttack = true; } private void AttackRot() { Vector3 playerScreenPos = Camera.main.WorldToScreenPoint(transform.position); Vector3 mouseScreenPos = Input.mousePosition; bool isLeft = mouseScreenPos.x < playerScreenPos.x; if (Mathf.Abs(mouseScreenPos.x - playerScreenPos.x) > Mathf.Abs(mouseScreenPos.y - playerScreenPos.y)) { transform.GetChild(0).localEulerAngles = isLeft ? new Vector3(0, 180, 0) : new Vector3(0, 0, 0); animator.SetFloat("AttackState", 1); controller.idleState = 1f; } else { if (mouseScreenPos.y > playerScreenPos.y) { animator.SetFloat("AttackState", 0.5f); controller.idleState = 2f / 3f; } else { animator.SetFloat("AttackState", 0); controller.idleState = 1f / 3f; } } } }