OldBlueWater/BlueWater/Assets/02.Scripts/WaterAndShip/Player.cs

89 lines
2.4 KiB
C#

using Cinemachine;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.InputSystem;
namespace _02.Scripts.WaterAndShip
{
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(PlayerInput))]
public class Player : MonoBehaviour
{
[InfoBox("최대 스피드")]
public float maxSpeed = 10f;
[InfoBox("가속 수치")]
public float acceleration = 2f;
[InfoBox("감속 수치")]
public float deceleration = 2f;
[InfoBox("회전 속도")]
public float turnSpeed = 10f;
private Rigidbody rb;
private Vector2 movementInput;
private bool isAssaultMode;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
MovePlayer();
RotatePlayer();
}
#region AssaultMode/DreadgeMode Switch
public void OnAssaultMode(InputValue value)
{
if (isAssaultMode)
{
SwitchToDredgeMode();
}
else
{
SwitchToAssaultMode();
}
}
private void SwitchToDredgeMode()
{
GameManager.Inst.CameraController.CamDredgeMode();
GameManager.Inst.UiController.uiAnimator.Reverse();
isAssaultMode = false;
}
private void SwitchToAssaultMode()
{
GameManager.Inst.CameraController.CamAssaultMode();
GameManager.Inst.UiController.uiAnimator.Play();
isAssaultMode = true;
}
#endregion
#region Movement
public void OnMove(InputValue value)
{
movementInput = value.Get<Vector2>();
}
private void MovePlayer()
{
var desiredVelocity = transform.forward * movementInput.y * maxSpeed;
var speedChange = (movementInput.y != 0 ? acceleration : deceleration) * Time.fixedDeltaTime;
rb.velocity = Vector3.MoveTowards(rb.velocity, desiredVelocity, speedChange);
}
private void RotatePlayer()
{
var turn = movementInput.x;
var turnRotation = Quaternion.Euler(0f, turn * turnSpeed, 0f);
rb.MoveRotation(rb.rotation * turnRotation);
}
#endregion
}
}