191 lines
5.4 KiB (Stored with Git LFS)
C#
191 lines
5.4 KiB (Stored with Git LFS)
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.Serialization;
|
|
|
|
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;
|
|
|
|
public bool IsMoving;
|
|
public bool IsDashing;
|
|
public bool IsDashCoolDownActive;
|
|
|
|
private float _finalSpeed;
|
|
|
|
private PlayerStateMachine _stateMachine;
|
|
|
|
#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;
|
|
|
|
_stateMachine = new PlayerStateMachine();
|
|
ChangeState(new IdleState(this, _playerView));
|
|
}
|
|
|
|
//public CellManager cellManager;
|
|
private void Update()
|
|
{
|
|
_stateMachine.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));
|
|
}
|
|
|
|
public void ChangeState(IStateMachine stateMachine)
|
|
{
|
|
_stateMachine.ChangeState(stateMachine);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |