2024-06-06 11:07:07 +00:00
|
|
|
using BlueWater.Interfaces;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace BlueWater.Players.Tycoons
|
|
|
|
{
|
|
|
|
public class TycoonMovement : MonoBehaviour, IPhysicMovable
|
|
|
|
{
|
|
|
|
// Variables
|
|
|
|
#region Variables
|
|
|
|
|
|
|
|
// Components
|
|
|
|
public Rigidbody Rigidbody { get; private set; }
|
|
|
|
private Transform _visualLook;
|
2024-06-07 17:31:08 +00:00
|
|
|
private SpineController _spineController;
|
2024-06-06 11:07:07 +00:00
|
|
|
|
|
|
|
// Move
|
|
|
|
[field: SerializeField, Range(1f, 10f), Tooltip("이동 속도")]
|
|
|
|
public float MoveSpeed { get; private set; } = 7f;
|
|
|
|
|
|
|
|
public bool EnableMove { get; private set; } = true;
|
|
|
|
|
|
|
|
private bool _isMoving;
|
|
|
|
public bool IsMoving
|
|
|
|
{
|
|
|
|
get => _isMoving;
|
|
|
|
private set
|
|
|
|
{
|
|
|
|
if (_isMoving == value) return;
|
|
|
|
|
|
|
|
_isMoving = value;
|
2024-06-07 17:31:08 +00:00
|
|
|
_spineController.PlayAnimation(_isMoving
|
|
|
|
? TycoonPlayerSpineAnimation.run.ToString()
|
|
|
|
: TycoonPlayerSpineAnimation.idle.ToString(), true);
|
2024-06-06 11:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Vector3 _inputDirection;
|
|
|
|
|
|
|
|
private Vector3 _currentDirection = Vector3.back;
|
|
|
|
public Vector3 CurrentDirection
|
|
|
|
{
|
|
|
|
get => _currentDirection;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == Vector3.zero) return;
|
|
|
|
|
|
|
|
_currentDirection = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private float _finalSpeed;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
// Unity events
|
|
|
|
#region Unity events
|
2024-06-07 17:31:08 +00:00
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
_spineController = GetComponent<SpineController>();
|
|
|
|
}
|
|
|
|
|
2024-06-06 11:07:07 +00:00
|
|
|
private void Update()
|
|
|
|
{
|
|
|
|
FlipVisualLook();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
|
|
|
if (!CanMove()) return;
|
|
|
|
|
|
|
|
ApplyMovement();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
// Initialize Methods
|
|
|
|
#region Initialize Methods
|
|
|
|
|
2024-06-07 17:31:08 +00:00
|
|
|
public void InitializeComponents(Rigidbody rigidbody, Transform visualLook)
|
2024-06-06 11:07:07 +00:00
|
|
|
{
|
|
|
|
Rigidbody = rigidbody;
|
|
|
|
_visualLook = visualLook;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
// Methods
|
|
|
|
#region Methods
|
|
|
|
|
|
|
|
// Event methods
|
|
|
|
public void HandleInputMovement(Vector2 movementInput)
|
|
|
|
{
|
|
|
|
_inputDirection = new Vector3(movementInput.x, 0, movementInput.y).normalized;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void HandleEnableMove() => EnableMove = true;
|
|
|
|
public void HandleDisableMove() => EnableMove = false;
|
|
|
|
|
|
|
|
// 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 bool CanMove()
|
|
|
|
{
|
|
|
|
return EnableMove;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddForce(Vector3 force, ForceMode forceMode)
|
|
|
|
{
|
|
|
|
Rigidbody.AddForce(force, forceMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ApplyMovement()
|
|
|
|
{
|
|
|
|
CurrentDirection = _inputDirection;
|
|
|
|
IsMoving = _inputDirection != Vector3.zero;
|
|
|
|
|
|
|
|
var finalVelocity = _inputDirection * (MoveSpeed * Time.deltaTime);
|
|
|
|
Rigidbody.MovePosition(transform.position + finalVelocity);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|