152 lines
4.1 KiB
C#
152 lines
4.1 KiB
C#
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;
|
|
private SpineController _spineController;
|
|
|
|
// Move
|
|
[field: SerializeField, Range(1f, 20f), Tooltip("이동 속도")]
|
|
public float MoveSpeed { get; private set; } = 7f;
|
|
|
|
public float MoveSpeedCoefficient { get; private set; } = 1f;
|
|
|
|
public bool IsMoveEnabled { get; private set; } = true;
|
|
|
|
private bool _isMoving;
|
|
public bool IsMoving
|
|
{
|
|
get => _isMoving;
|
|
private set
|
|
{
|
|
if (_isMoving == value) return;
|
|
|
|
_isMoving = value;
|
|
_spineController.PlayAnimation(_isMoving
|
|
? TycoonPlayerSpineAnimation.run.ToString()
|
|
: TycoonPlayerSpineAnimation.idle.ToString(), true);
|
|
}
|
|
}
|
|
|
|
private Vector3 _inputDirection;
|
|
|
|
private Vector3 _currentDirection = Vector3.back;
|
|
public Vector3 CurrentDirection
|
|
{
|
|
get => _currentDirection;
|
|
private set
|
|
{
|
|
if (value == Vector3.zero) return;
|
|
|
|
_currentDirection = value;
|
|
}
|
|
}
|
|
|
|
public Vector3 PushDirection { get; private set; }
|
|
public float PushPower { get; private set; }
|
|
public float PushPowerReduction { get; private set; }
|
|
|
|
private float _finalSpeed;
|
|
|
|
#endregion
|
|
|
|
// Unity events
|
|
#region Unity events
|
|
|
|
private void Awake()
|
|
{
|
|
_spineController = GetComponent<SpineController>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
FlipVisualLook();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (!CanMove()) return;
|
|
|
|
Move();
|
|
}
|
|
|
|
#endregion
|
|
|
|
// Initialize Methods
|
|
#region Initialize Methods
|
|
|
|
public void InitializeComponents(Rigidbody rigidbody, Transform visualLook)
|
|
{
|
|
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 SetMoveSpeedCoefficient(float value) => MoveSpeedCoefficient = value;
|
|
public void ResetMoveSpeedCoefficient() => MoveSpeedCoefficient = 1f;
|
|
public void EnableMove() => IsMoveEnabled = true;
|
|
public void DisableMove() => IsMoveEnabled = false;
|
|
public void SetCurrentDirection(Vector3 normalDirection) => 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 bool CanMove()
|
|
{
|
|
return IsMoveEnabled;
|
|
}
|
|
|
|
public void AddForce(Vector3 force, ForceMode forceMode)
|
|
{
|
|
Rigidbody.AddForce(force, forceMode);
|
|
}
|
|
|
|
public void SetPush(Vector3 pushDirection, float pushPower)
|
|
{
|
|
throw new System.NotImplementedException();
|
|
}
|
|
|
|
public void Move()
|
|
{
|
|
CurrentDirection = _inputDirection;
|
|
IsMoving = _inputDirection != Vector3.zero;
|
|
|
|
var finalVelocity = _inputDirection * MoveSpeed;
|
|
if (!Rigidbody.isKinematic)
|
|
{
|
|
Rigidbody.linearVelocity = finalVelocity;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |