Wizard_Of_Wak/Assets/1_Script/Player/PlayerContoller.cs

121 lines
3.9 KiB
C#

using System.Collections;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f; // 이동 속도
public float dashDistance = 3f; // 대시 거리
public float dashCooldown = 1f; // 대시 쿨타임
public float dashTime = 0.5f;
private bool canDash = true; // 대시 가능 여부
private Vector2 moveDirection = Vector2.zero; // 현재 이동 방향
private bool isWalking = false; // 이동 중인지 여부
private float dashValue;
Animator animator;
SpriteRenderer spriteRenderer;
private float idleState = 0f; // 0: 오른쪽, 0.333: 왼쪽, 0.666: 위, 1: 아래
void Start()
{
spriteRenderer = GetComponentInChildren<SpriteRenderer>();
animator = GetComponentInChildren<Animator>();
// 키 이벤트와 스프라이트 및 회전 처리 연결
PlayManager.Instance.keySdown += () => SetMovement(Vector2.down, 1f / 3f); // 아래
PlayManager.Instance.keyWdown += () => SetMovement(Vector2.up, 2f / 3f); // 위
PlayManager.Instance.keyAdown += () => SetMovement(Vector2.left, 1f); // 왼쪽
PlayManager.Instance.keyDdown += () => SetMovement(Vector2.right, 1f); // 오른쪽
// 대시 기능 연결
PlayManager.Instance.keySpace += Dash;
}
void Update()
{
HandleMovement(); // 이동 처리
}
// 이동 처리
void HandleMovement()
{
if (!canDash) return;
moveDirection = Vector2.zero; // 매 프레임 초기화
// 키 상태를 직접 확인하고 방향 결정
if (Input.GetKey(KeyCode.A)) moveDirection += Vector2.left;
if (Input.GetKey(KeyCode.D)) moveDirection += Vector2.right;
if (Input.GetKey(KeyCode.W)) moveDirection += Vector2.up;
if (Input.GetKey(KeyCode.S)) moveDirection += Vector2.down;
// 이동 여부 판별
isWalking = moveDirection != Vector2.zero;
animator.SetBool("isWalking", isWalking);
// 이동 방향이 있을 때만 이동
if (isWalking)
{
transform.position += (Vector3)(moveDirection.normalized * moveSpeed * Time.deltaTime);
// 애니메이션 이동 방향 설정
animator.SetFloat("moveX", Mathf.Abs(moveDirection.x));
animator.SetFloat("moveY", moveDirection.y);
// 회전 처리
if (Mathf.Abs(moveDirection.x) > 0)
{
if (moveDirection.x < 0 && transform.GetChild(0).localEulerAngles.y != 180)
{
transform.GetChild(0).localEulerAngles = new Vector3(0, 180, 0);
}
else if (moveDirection.x > 0 && transform.GetChild(0).localEulerAngles.y != 0)
{
transform.GetChild(0).localEulerAngles = new Vector3(0, 0, 0);
}
}
}
else
{
// 이동하지 않으면 Idle 상태를 설정
animator.SetFloat("moveX", 0);
animator.SetFloat("moveY", 0);
animator.SetFloat("idleState", idleState);
}
}
// 대시 처리
void Dash()
{
if (canDash)
{
canDash = false;
isWalking = false;
animator.SetTrigger("DodgeTrigger");
StartCoroutine(DashCoroutine()); // 대시 시작
}
}
IEnumerator DashCoroutine()
{
Vector3 startPosition = transform.position;
Vector3 targetPosition = startPosition + (Vector3)(moveDirection.normalized * dashDistance);
float elapsedTime = 0f;
while (elapsedTime < dashTime)
{
transform.position = Vector3.Lerp(startPosition, targetPosition, elapsedTime / dashTime);
elapsedTime += Time.deltaTime;
yield return null;
}
transform.position = targetPosition;
canDash = true;
}
// 이동 방향과 스프라이트, 회전 처리
void SetMovement(Vector2 direction, float idleStateValue)
{
moveDirection = direction;
idleState = idleStateValue;
}
}