234 lines
6.3 KiB
C#
234 lines
6.3 KiB
C#
using System.Collections;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace DDD
|
|
{
|
|
public class RestaurantPlayer : MonoBehaviour
|
|
{
|
|
#region Variables
|
|
|
|
private Rigidbody _rigidbody;
|
|
private Transform _visualLook;
|
|
|
|
// Move
|
|
[SerializeField, Range(1f, 20f), Tooltip("이동 속도")]
|
|
private float _moveSpeed = 7f;
|
|
|
|
[SerializeField]
|
|
private float _moveSpeedMultiplier = 1f;
|
|
|
|
private bool _isMoveEnabled = true;
|
|
|
|
private bool _isMoving;
|
|
|
|
// Dash
|
|
[Title("대쉬")]
|
|
[SerializeField, Range(1f, 50f), Tooltip("대쉬 속도")]
|
|
private float _dashSpeed = 20f;
|
|
|
|
[SerializeField, Range(0.1f, 1f), Tooltip("대쉬 시간")]
|
|
private float _dashTime = 0.2f;
|
|
|
|
[SerializeField, Range(0f, 5f), Tooltip("대쉬 쿨타임")]
|
|
private float _dashCooldown = 0.5f;
|
|
|
|
[SerializeField]
|
|
private ParticleSystem _dashParticle;
|
|
|
|
[Title("사운드")]
|
|
[SerializeField]
|
|
private string _walkingSfxName = "TycoonPlayerWalking";
|
|
|
|
[SerializeField]
|
|
private string _dashSfxName = "TycoonPlayerDashing";
|
|
|
|
private bool _isDashEnabled = true;
|
|
private bool _isDashing;
|
|
private bool _isDashCoolDownActive;
|
|
|
|
private Vector3 _inputDirection;
|
|
|
|
private Vector3 _currentDirection = Vector3.back;
|
|
|
|
private InputAction _moveAction;
|
|
private InputAction _dashAction;
|
|
private Coroutine _dashInstance;
|
|
private float _finalSpeed;
|
|
|
|
#endregion
|
|
|
|
// Unity events
|
|
|
|
#region Unity events
|
|
|
|
private void Awake()
|
|
{
|
|
InitializeComponents();
|
|
}
|
|
|
|
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
|
|
|
|
// Initialize Methods
|
|
|
|
#region Initialize Methods
|
|
|
|
private void InitializeComponents()
|
|
{
|
|
_rigidbody = GetComponent<Rigidbody>();
|
|
_visualLook = transform.Find("VisualLook");
|
|
}
|
|
|
|
#endregion
|
|
|
|
// Methods
|
|
|
|
#region Methods
|
|
|
|
// Event methods
|
|
public void SetCurrentDirection(Vector3 normalDirection)
|
|
{
|
|
if (normalDirection == Vector3.zero) return;
|
|
|
|
_currentDirection = normalDirection;
|
|
}
|
|
|
|
// Methods
|
|
private void FlipVisualLook()
|
|
{
|
|
var 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;
|
|
}
|
|
|
|
// Move
|
|
public void OnMove(InputAction.CallbackContext context)
|
|
{
|
|
var movementInput = _moveAction.ReadValue<Vector2>();
|
|
_inputDirection = new Vector3(movementInput.x, 0, movementInput.y).normalized;
|
|
}
|
|
|
|
public bool CanMove()
|
|
{
|
|
return _isMoveEnabled && !_isDashing;
|
|
}
|
|
|
|
public void Move()
|
|
{
|
|
SetCurrentDirection(_inputDirection);
|
|
_isMoving = _inputDirection != Vector3.zero;
|
|
|
|
var finalVelocity = _inputDirection * (_moveSpeed * _moveSpeedMultiplier);
|
|
if (!_rigidbody.isKinematic)
|
|
{
|
|
_rigidbody.linearVelocity = finalVelocity;
|
|
}
|
|
}
|
|
|
|
// Dash
|
|
public void OnDash(InputAction.CallbackContext context)
|
|
{
|
|
if (!CanDash()) return;
|
|
|
|
Dash();
|
|
}
|
|
|
|
public bool CanDash()
|
|
{
|
|
return _isDashEnabled && !_isDashing && !_isDashCoolDownActive;
|
|
}
|
|
|
|
public void Dash()
|
|
{
|
|
Utils.StartUniqueCoroutine(this, ref _dashInstance, DashCoroutine());
|
|
}
|
|
|
|
private IEnumerator DashCoroutine()
|
|
{
|
|
_isDashing = true;
|
|
_isDashCoolDownActive = true;
|
|
if (_dashParticle)
|
|
{
|
|
_dashParticle.Play();
|
|
}
|
|
|
|
AudioManager.Instance.PlaySfx(_dashSfxName);
|
|
|
|
var dashDirection = _inputDirection;
|
|
if (dashDirection == Vector3.zero)
|
|
{
|
|
dashDirection = _currentDirection;
|
|
}
|
|
|
|
var elapsedTime = 0f;
|
|
while (elapsedTime <= _dashTime)
|
|
{
|
|
var finalVelocity = dashDirection * _dashSpeed;
|
|
_rigidbody.linearVelocity = finalVelocity;
|
|
|
|
elapsedTime += Time.fixedDeltaTime;
|
|
yield return new WaitForFixedUpdate();
|
|
}
|
|
|
|
//var newDashCooldown = DashCooldown - TycoonManager.Instance.TycoonStatus.PlayerDashCooldownReduction;
|
|
EndDash(_dashCooldown);
|
|
}
|
|
|
|
public void EndDash(float dashCooldown = float.PositiveInfinity)
|
|
{
|
|
Utils.EndUniqueCoroutine(this, ref _dashInstance);
|
|
_rigidbody.linearVelocity = Vector3.zero;
|
|
_isDashing = false;
|
|
|
|
if (float.IsPositiveInfinity(dashCooldown))
|
|
{
|
|
dashCooldown = _dashCooldown;
|
|
}
|
|
|
|
// TODO : ui 연동
|
|
//EventManager.InvokeDashCooldown(dashCooldown);
|
|
StartCoroutine(Utils.CoolDownCoroutine(dashCooldown, () => _isDashCoolDownActive = false));
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |