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

169 lines
4.5 KiB
C#
Raw Normal View History

2023-08-08 07:08:41 +00:00
using Cinemachine;
using Sirenix.OdinInspector;
2023-08-03 01:03:08 +00:00
using UnityEngine;
using UnityEngine.InputSystem;
namespace _02.Scripts.WaterAndShip
{
2023-08-10 13:33:10 +00:00
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(PlayerInput))]
2023-08-03 01:03:08 +00:00
public class Player : MonoBehaviour
{
2023-08-15 08:29:24 +00:00
[Title("Component")]
private Rigidbody rb;
private Vector2 movementInput;
[Title("Child Object")]
private GameObject character;
2023-08-11 16:21:26 +00:00
[Title("쉽의 기본 설정")]
2023-08-15 08:29:24 +00:00
[Tooltip("최대 스피드")]
2023-08-03 02:54:21 +00:00
public float maxSpeed = 10f;
2023-08-15 08:29:24 +00:00
[Tooltip("가속 수치")]
2023-08-03 02:54:21 +00:00
public float acceleration = 2f;
2023-08-15 08:29:24 +00:00
[Tooltip("감속 수치")]
2023-08-03 02:54:21 +00:00
public float deceleration = 2f;
2023-08-15 08:29:24 +00:00
[Tooltip("회전 속도")]
2023-08-03 01:03:08 +00:00
public float turnSpeed = 10f;
2023-08-11 16:21:26 +00:00
2023-08-15 08:29:24 +00:00
[Title("캐릭터의 기본 설정")]
[Tooltip("캐릭터의 이동 속도")]
public float characterSpeed = 10f;
2023-08-11 16:21:26 +00:00
public bool IsAssaultMode { get; set; }
public bool IsInShipMode { get; set; }
public bool IsdredgeMode { get; set; }
public bool IsTakeAim { get; set; }
2023-08-03 01:03:08 +00:00
2023-08-15 08:29:24 +00:00
private void Init()
2023-08-03 01:03:08 +00:00
{
2023-08-15 08:29:24 +00:00
character = transform.Find("Character").gameObject;
2023-08-03 01:03:08 +00:00
rb = GetComponent<Rigidbody>();
}
2023-08-15 08:29:24 +00:00
2023-08-17 03:42:12 +00:00
#region Unity Function
2023-08-15 08:29:24 +00:00
private void Awake()
{
Init();
}
2023-08-08 07:08:41 +00:00
private void FixedUpdate()
{
2023-08-15 08:29:24 +00:00
if (IsInShipMode)
{
MoveCharacterPlayer();
}
else
{
MoveShipPlayer();
RotatePlayer();
}
2023-08-08 07:08:41 +00:00
}
2023-08-17 03:42:12 +00:00
#endregion
2023-08-11 16:21:26 +00:00
#region Movement
public void OnMove(InputValue value)
{
movementInput = value.Get<Vector2>();
}
2023-08-15 08:29:24 +00:00
private void MoveShipPlayer()
2023-08-11 16:21:26 +00:00
{
2023-08-16 01:38:47 +00:00
var desiredVelocity = transform.forward * (movementInput.y * maxSpeed);
2023-08-11 16:21:26 +00:00
var speedChange = (movementInput.y != 0 ? acceleration : deceleration) * Time.fixedDeltaTime;
rb.velocity = Vector3.MoveTowards(rb.velocity, desiredVelocity, speedChange);
}
2023-08-08 07:08:41 +00:00
2023-08-11 16:21:26 +00:00
private void RotatePlayer()
{
var turn = movementInput.x;
var turnRotation = Quaternion.Euler(0f, turn * turnSpeed, 0f);
rb.MoveRotation(rb.rotation * turnRotation);
}
2023-08-15 08:29:24 +00:00
private void MoveCharacterPlayer()
{
2023-08-16 01:38:47 +00:00
Vector3 movement = character.transform.rotation * new Vector3(-movementInput.y, 0, movementInput.x) * (characterSpeed * Time.deltaTime);
2023-08-15 08:29:24 +00:00
character.transform.position += movement;
}
2023-08-11 16:21:26 +00:00
#endregion
2023-08-08 07:08:41 +00:00
#region AssaultMode/DreadgeMode Switch
2023-08-11 16:21:26 +00:00
private void OnAssaultMode(InputValue value)
2023-08-08 07:08:41 +00:00
{
2023-08-11 16:21:26 +00:00
if (IsAssaultMode)
2023-08-08 07:08:41 +00:00
{
SwitchToDredgeMode();
}
else
{
SwitchToAssaultMode();
}
}
private void SwitchToDredgeMode()
{
GameManager.Inst.CameraController.CamDredgeMode();
2023-08-17 03:42:12 +00:00
UiManager.Inst.CardLayoutGroupAnimator.Reverse();
2023-08-11 16:21:26 +00:00
IsAssaultMode = false;
IsdredgeMode = true;
2023-08-08 07:08:41 +00:00
}
2023-08-03 01:03:08 +00:00
2023-08-08 07:08:41 +00:00
private void SwitchToAssaultMode()
{
GameManager.Inst.CameraController.CamAssaultMode();
2023-08-17 03:42:12 +00:00
UiManager.Inst.CardLayoutGroupAnimator.Play();
2023-08-11 16:21:26 +00:00
IsAssaultMode = true;
IsdredgeMode = false;
2023-08-08 07:08:41 +00:00
}
#endregion
2023-08-11 16:21:26 +00:00
#region Interaction Key
private void OnInteraction(InputValue value)
2023-08-03 01:03:08 +00:00
{
2023-08-11 16:21:26 +00:00
2023-08-03 01:03:08 +00:00
}
2023-08-08 07:08:41 +00:00
2023-08-11 16:21:26 +00:00
private void OnInteractionHold(InputValue value)
2023-08-03 01:03:08 +00:00
{
2023-08-11 16:21:26 +00:00
if (IsInShipMode)
{
GameManager.Inst.CameraController.CamDredgeMode();
IsdredgeMode = true;
IsAssaultMode = false;
IsInShipMode = false;
}
else
{
GameManager.Inst.CameraController.CamInShipMode();
IsInShipMode = true;
IsAssaultMode = false;
IsdredgeMode = false;
}
2023-08-08 07:08:41 +00:00
}
2023-08-03 01:03:08 +00:00
2023-08-08 07:08:41 +00:00
#endregion
2023-08-17 03:42:12 +00:00
private void OnZkey(InputValue value)
{
UiManager.Inst.AddCard();
}
private void OnTakeAim(InputValue value)
{
IsTakeAim = !IsTakeAim;
GameManager.Inst.CameraController.CamTakeAim(IsTakeAim);
UiManager.Inst.AimOnOff(IsTakeAim);
}
2023-08-03 01:03:08 +00:00
}
2023-08-03 02:54:21 +00:00
}