OldBlueWater/BlueWater/Assets/02.Scripts/Player/Player.cs
2023-08-30 14:17:26 +09:00

203 lines
5.8 KiB
C#

using Cinemachine;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.InputSystem;
using Blobcreate. ProjectileToolkit;
namespace _02.Scripts.WaterAndShip
{
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(PlayerInput))]
public class Player : MonoBehaviour
{
[Title("Component")]
private Rigidbody rb;
private Vector2 movementInput;
[Title("Child Object")]
private GameObject character;
[Title("쉽의 기본 설정")]
[Tooltip("최대 스피드")]
public float maxSpeed = 10f;
[Tooltip("가속 수치")]
public float acceleration = 2f;
[Tooltip("감속 수치")]
public float deceleration = 2f;
[Tooltip("회전 속도")]
public float turnSpeed = 10f;
[Title("캐릭터의 기본 설정")]
[Tooltip("캐릭터의 이동 속도")]
public float characterSpeed = 10f;
public bool IsAssaultMode { get; set; }
public bool IsInShipMode { get; set; }
public bool IsDredgeMode { get; set; }
public bool IsTakeAim { get; set; }
private void Init()
{
character = transform.Find("Character").gameObject;
rb = GetComponent<Rigidbody>();
}
#region Unity Function
private void Awake()
{
Init();
}
private void FixedUpdate()
{
if (IsInShipMode)
{
MoveCharacterPlayer();
}
else
{
MoveShipPlayer();
RotatePlayer();
}
}
#endregion
#region Movement
public void OnMove(InputValue value) // WASD
{
movementInput = value.Get<Vector2>();
}
private void MoveShipPlayer()
{
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);
}
private void MoveCharacterPlayer()
{
Vector3 movement = character.transform.rotation * new Vector3(-movementInput.y, 0, movementInput.x) * (characterSpeed * Time.deltaTime);
character.transform.position += movement;
}
#endregion
#region AssaultMode/DreadgeMode Switch
private void OnAssaultMode(InputValue value) // V
{
SwitchAssaultMode(!IsAssaultMode);
}
private void SwitchAssaultMode(bool isOn)
{
if (isOn)
{
SwitchTakeAim(false);
SwitchInShipMode(false);
GameManager.Inst.CameraController.CamAssaultMode();
UiManager.Inst.CardLayoutGroupAnimator.Play();
IsAssaultMode = true;
}
else if (IsAssaultMode)
{
GameManager.Inst.CameraController.CamDredgeMode();
UiManager.Inst.CardLayoutGroupAnimator.Reverse();
IsAssaultMode = false;
}
}
#endregion
#region Interaction Key
private void OnInteraction(InputValue value) //F
{
}
private void OnInteractionHold(InputValue value) //F Hold
{
SwitchInShipMode(!IsInShipMode);
}
private void SwitchInShipMode(bool isOn)
{
if (isOn)
{
SwitchTakeAim(false);
SwitchAssaultMode(false);
GameManager.Inst.CameraController.CamInShipMode();
IsInShipMode = true;
}
else if (IsInShipMode)
{
GameManager.Inst.CameraController.CamDredgeMode();
IsInShipMode = false;
}
}
#endregion
private void OnZkey(InputValue value)
{
UiManager.Inst.AddCard();
}
private void OnTakeAim(InputValue value) // Space
{
SwitchTakeAim(!IsTakeAim);
}
private void SwitchTakeAim(bool isOn)
{
if (isOn)
{
SwitchAssaultMode(false);
SwitchInShipMode(false);
GameManager.Inst.CameraController.CamTakeAim(true);
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
IsTakeAim = true;
}
else if (IsTakeAim)
{
GameManager.Inst.CameraController.CamTakeAim(false);
Cursor.visible = true;
Cursor.lockState = CursorLockMode.Confined;
IsTakeAim = false;
}
UiManager.Inst.AimOnOff(isOn);
}
[SerializeField] Rigidbody projectilePrefab;
[SerializeField] Transform launchPoint;
[SerializeField] float timeOfFlight;
[SerializeField] Transform predictedPos;
private void Fire()
{
if (Input.GetMouseButtonDown(0) && IsTakeAim)
{
var myRigid = Instantiate(projectilePrefab, launchPoint.position, launchPoint.rotation);
var v = Projectile.VelocityByTime(myRigid.transform.position, predictedPos.position, timeOfFlight);
myRigid.AddForce(v, ForceMode.VelocityChange);
}
}
}
}