ProjectDDD/Assets/_DDD/_Scripts/RestaurantCharacter/RestaurantPlayerMovement.cs
NTG_Lenovo bfb7ccbe4b SpineController 추가, 애니메이션 이벤트로 관리
FlipVisualLook RestaurantPlayerCharacter에서 관리
2025-07-14 14:07:15 +09:00

129 lines
3.6 KiB
C#

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
namespace DDD
{
public class RestaurantPlayerMovement : RestaurantCharacterMovement
{
private Rigidbody _rigidbody;
private RestaurantPlayerDataSo _playerData;
private Vector3 _inputDirection;
private Vector3 _currentDirection;
private bool _isMoving;
private bool _isDashing;
private bool _isDashCooldown;
private bool _isInitialized;
public Action<bool> OnMoving;
public Action<float> OnDashing;
private void Awake()
{
_rigidbody = GetComponent<Rigidbody>();
}
private async void Start()
{
try
{
_playerData = await AssetManager.LoadAsset<RestaurantPlayerDataSo>(DataConstants.RestaurantPlayerDataSo);
_playerData.MoveActionReference.action.performed += OnMove;
_playerData.MoveActionReference.action.canceled += OnMove;
_playerData.DashActionReference.action.performed += OnDash;
_isInitialized = true;
}
catch (Exception e)
{
Debug.LogError($"_playerData load failed\n{e}");
}
}
private void FixedUpdate()
{
if (_isInitialized == false) return;
if (CanMove())
{
Move();
}
}
private void OnDestroy()
{
if (_playerData)
{
_playerData.MoveActionReference.action.performed -= OnMove;
_playerData.MoveActionReference.action.canceled -= OnMove;
_playerData.DashActionReference.action.performed -= OnDash;
}
}
public void SetCurrentDirection(Vector3 normalDirection)
{
if (_inputDirection == Vector3.zero) return;
_currentDirection = normalDirection;
}
private void OnMove(InputAction.CallbackContext context)
{
Vector2 movementInput = context.ReadValue<Vector2>();
_inputDirection = new Vector3(movementInput.x, 0f, movementInput.y);
}
private bool CanMove()
{
return _playerData.IsMoveEnabled && _isDashing == false;
}
private void Move()
{
SetCurrentDirection(_inputDirection);
_isMoving = _inputDirection != Vector3.zero;
OnMoving?.Invoke(_isMoving);
Vector3 finalVelocity = _inputDirection * _playerData.MoveSpeed;
_rigidbody.linearVelocity = finalVelocity;
}
private void OnDash(InputAction.CallbackContext context)
{
if (CanDash())
{
StartCoroutine(DashCoroutine());
}
}
private bool CanDash()
{
return _playerData.IsDashEnabled && _isDashing == false && _isDashCooldown == false;
}
private IEnumerator DashCoroutine()
{
_isDashing = true;
_isDashCooldown = true;
OnDashing?.Invoke(_playerData.DashTime);
Vector3 dashVelocity = _currentDirection.normalized * _playerData.DashSpeed;
_rigidbody.linearVelocity = dashVelocity;
yield return new WaitForSeconds(_playerData.DashTime);
_isDashing = false;
yield return new WaitForSeconds(_playerData.DashCooldown);
_isDashCooldown = false;
}
public Vector3 GetCurrentDirection() => _currentDirection;
}
}