2025-07-11 05:48:49 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
2025-07-09 09:45:11 +00:00
|
|
|
using UnityEngine;
|
2025-07-11 05:48:49 +00:00
|
|
|
using UnityEngine.InputSystem;
|
2025-07-09 09:45:11 +00:00
|
|
|
|
|
|
|
namespace DDD
|
|
|
|
{
|
|
|
|
public class RestaurantPlayerMovement : RestaurantCharacterMovement
|
|
|
|
{
|
2025-07-11 05:48:49 +00:00
|
|
|
private Rigidbody _rigidbody;
|
|
|
|
private RestaurantCharacterAnimation _animation;
|
|
|
|
private Transform _visualLook;
|
|
|
|
|
|
|
|
private RestaurantPlayerDataSo _playerData;
|
|
|
|
|
|
|
|
private Vector3 _inputDirection;
|
|
|
|
private Vector3 _currentDirection;
|
|
|
|
private bool _isMoving;
|
|
|
|
private bool _isDashing;
|
|
|
|
private bool _isDashCooldown;
|
|
|
|
private bool _isInitialized;
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
_rigidbody = GetComponent<Rigidbody>();
|
|
|
|
_animation = GetComponent<RestaurantCharacterAnimation>();
|
|
|
|
_visualLook = transform.Find("VisualLook");
|
|
|
|
}
|
|
|
|
|
|
|
|
private async void Start()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_playerData = await AssetManager.LoadAsset<RestaurantPlayerDataSo>("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 Update()
|
|
|
|
{
|
|
|
|
if (_isInitialized == false) return;
|
|
|
|
|
|
|
|
FlipVisualLook();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 FlipVisualLook()
|
|
|
|
{
|
|
|
|
Vector3 localScale = _visualLook.localScale;
|
|
|
|
localScale.x = _currentDirection.x switch
|
|
|
|
{
|
|
|
|
> 0.01f => -Mathf.Abs(localScale.x),
|
|
|
|
< -0.01f => Mathf.Abs(localScale.x),
|
|
|
|
_ => localScale.x
|
|
|
|
};
|
|
|
|
_visualLook.localScale = localScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
string animationName = _isMoving ? "RunFast" : "Idle";
|
|
|
|
_animation.PlayAnimation(animationName, true);
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
// TODO : ui생기면 연동
|
|
|
|
|
|
|
|
_isDashing = true;
|
|
|
|
_isDashCooldown = true;
|
|
|
|
|
|
|
|
_animation.PlayAnimationDuration("Dash", false, _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;
|
|
|
|
}
|
2025-07-09 09:45:11 +00:00
|
|
|
}
|
|
|
|
}
|