177 lines
5.0 KiB (Stored with Git LFS)
C#
177 lines
5.0 KiB (Stored with Git LFS)
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace DDD
|
|
{
|
|
public class RestaurantPlayer : MonoBehaviour
|
|
{
|
|
#region Variables
|
|
|
|
private RestaurantPlayerDataSo _playerData;
|
|
private RestaurantPlayerView _playerView;
|
|
|
|
private InputAction _moveAction;
|
|
private InputAction _dashAction;
|
|
private Coroutine _dashInstance;
|
|
|
|
private Vector3 _inputDirection;
|
|
private Vector3 _currentDirection = Vector3.back;
|
|
|
|
private bool _isMoving;
|
|
private bool _isDashing;
|
|
private bool _isDashCoolDownActive;
|
|
private float _finalSpeed;
|
|
|
|
#endregion
|
|
|
|
// Unity events
|
|
|
|
#region Unity events
|
|
|
|
private void Awake()
|
|
{
|
|
_playerData = Addressables.LoadAssetAsync<RestaurantPlayerDataSo>("RestaurantPlayerDataSo").WaitForCompletion();
|
|
_playerView = GetComponent<RestaurantPlayerView>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_moveAction = KeyManager.Instance.GetAction(InputActionMaps.Restaurant, RestaurantActions.Move);
|
|
_dashAction = KeyManager.Instance.GetAction(InputActionMaps.Restaurant, RestaurantActions.Dash);
|
|
|
|
_moveAction.performed += OnMove;
|
|
_moveAction.canceled += OnMove;
|
|
_dashAction.performed += OnDash;
|
|
}
|
|
|
|
//public CellManager cellManager;
|
|
private void Update()
|
|
{
|
|
FlipVisualLook();
|
|
|
|
//UpdateCell
|
|
//cellManager.SetupCell(transform.position);
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (!CanMove()) return;
|
|
|
|
Move();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_moveAction.performed -= OnMove;
|
|
_moveAction.canceled -= OnMove;
|
|
_dashAction.performed -= OnDash;
|
|
}
|
|
|
|
#endregion
|
|
|
|
// Methods
|
|
#region Methods
|
|
|
|
public void SetCurrentDirection(Vector3 normalDirection)
|
|
{
|
|
if (normalDirection == Vector3.zero) return;
|
|
|
|
_currentDirection = normalDirection;
|
|
}
|
|
|
|
private void FlipVisualLook()
|
|
{
|
|
Vector3 localScale = _playerView.GetLocalScale();
|
|
localScale.x = _currentDirection.x switch
|
|
{
|
|
> 0.01f => -Mathf.Abs(localScale.x),
|
|
< -0.01f => Mathf.Abs(localScale.x),
|
|
_ => localScale.x
|
|
};
|
|
_playerView.SetLocalScale(localScale);
|
|
}
|
|
|
|
public void OnMove(InputAction.CallbackContext context)
|
|
{
|
|
var movementInput = _moveAction.ReadValue<Vector2>();
|
|
_inputDirection = new Vector3(movementInput.x, 0, movementInput.y).normalized;
|
|
}
|
|
|
|
public bool CanMove()
|
|
{
|
|
return _playerData.IsMoveEnabled && !_isDashing;
|
|
}
|
|
|
|
public void Move()
|
|
{
|
|
SetCurrentDirection(_inputDirection);
|
|
_isMoving = _inputDirection != Vector3.zero;
|
|
|
|
var finalVelocity = _inputDirection * _playerData.MoveSpeed;
|
|
_playerView.SetVelocity(finalVelocity);
|
|
}
|
|
|
|
public void OnDash(InputAction.CallbackContext context)
|
|
{
|
|
if (!CanDash()) return;
|
|
|
|
Dash();
|
|
}
|
|
|
|
public bool CanDash()
|
|
{
|
|
return _playerData.IsDashEnabled && !_isDashing && !_isDashCoolDownActive;
|
|
}
|
|
|
|
public void Dash()
|
|
{
|
|
Utils.StartUniqueCoroutine(this, ref _dashInstance, DashCoroutine());
|
|
}
|
|
|
|
private IEnumerator DashCoroutine()
|
|
{
|
|
_isDashing = true;
|
|
_isDashCoolDownActive = true;
|
|
_playerView.PlayDashParticle();
|
|
|
|
AudioManager.Instance.PlaySfx(_playerData.DashSfxName);
|
|
|
|
var dashDirection = _inputDirection;
|
|
if (dashDirection == Vector3.zero)
|
|
{
|
|
dashDirection = _currentDirection;
|
|
}
|
|
|
|
var elapsedTime = 0f;
|
|
while (elapsedTime <= _playerData.DashTime)
|
|
{
|
|
var finalVelocity = dashDirection * _playerData.DashSpeed;
|
|
_playerView.SetVelocity(finalVelocity);
|
|
|
|
elapsedTime += Time.fixedDeltaTime;
|
|
yield return new WaitForFixedUpdate();
|
|
}
|
|
|
|
EndDash(_playerData.DashCooldown);
|
|
}
|
|
|
|
public void EndDash(float dashCooldown = float.PositiveInfinity)
|
|
{
|
|
Utils.EndUniqueCoroutine(this, ref _dashInstance);
|
|
_playerView.SetVelocity(Vector3.zero);
|
|
_isDashing = false;
|
|
|
|
if (float.IsPositiveInfinity(dashCooldown))
|
|
{
|
|
dashCooldown = _playerData.DashCooldown;
|
|
}
|
|
|
|
// TODO : ui 연동
|
|
StartCoroutine(Utils.CoolDownCoroutine(dashCooldown, () => _isDashCoolDownActive = false));
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |