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

210 lines
6.0 KiB
C#
Raw Normal View History

2023-08-31 06:46:13 +00:00
using Blobcreate.ProjectileToolkit;
using Sirenix.OdinInspector;
2023-08-03 01:03:08 +00:00
using UnityEngine;
using UnityEngine.InputSystem;
2023-08-31 06:46:13 +00:00
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
2023-08-03 01:03:08 +00:00
{
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-31 06:46:13 +00:00
[Title("레이더")]
public Collider[] radar = new Collider[10];
public Transform[] inCameraRadar = new Transform[10];
public Transform target;
2023-08-11 16:21:26 +00:00
public bool IsAssaultMode { get; set; }
public bool IsInShipMode { get; set; }
2023-08-29 04:23:27 +00:00
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
2023-08-23 07:32:24 +00:00
public void OnMove(InputValue value) // WASD
2023-08-11 16:21:26 +00:00
{
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-23 07:32:24 +00:00
private void OnAssaultMode(InputValue value) // V
2023-08-08 07:08:41 +00:00
{
2023-08-29 04:23:27 +00:00
SwitchAssaultMode(!IsAssaultMode);
}
private void SwitchAssaultMode(bool isOn)
{
if (isOn)
2023-08-08 07:08:41 +00:00
{
2023-08-29 04:23:27 +00:00
SwitchTakeAim(false);
SwitchInShipMode(false);
GameManager.Inst.CameraController.CamAssaultMode();
UiManager.Inst.CardLayoutGroupAnimator.Play();
IsAssaultMode = true;
2023-08-08 07:08:41 +00:00
}
2023-08-29 04:23:27 +00:00
else if (IsAssaultMode)
2023-08-08 07:08:41 +00:00
{
2023-08-29 04:23:27 +00:00
GameManager.Inst.CameraController.CamDredgeMode();
UiManager.Inst.CardLayoutGroupAnimator.Reverse();
IsAssaultMode = false;
2023-08-08 07:08:41 +00:00
}
}
#endregion
2023-08-11 16:21:26 +00:00
#region Interaction Key
2023-09-04 03:20:01 +00:00
private void OnInteractionE(InputValue value)
{
2023-09-04 04:21:57 +00:00
Fire();
2023-09-04 03:20:01 +00:00
}
2023-08-23 07:32:24 +00:00
private void OnInteraction(InputValue value) //F
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-23 07:32:24 +00:00
private void OnInteractionHold(InputValue value) //F Hold
2023-08-03 01:03:08 +00:00
{
2023-08-29 04:23:27 +00:00
SwitchInShipMode(!IsInShipMode);
}
private void SwitchInShipMode(bool isOn)
{
if (isOn)
2023-08-11 16:21:26 +00:00
{
2023-08-29 04:23:27 +00:00
SwitchTakeAim(false);
SwitchAssaultMode(false);
2023-08-11 16:21:26 +00:00
GameManager.Inst.CameraController.CamInShipMode();
IsInShipMode = true;
2023-08-29 04:23:27 +00:00
}
else if (IsInShipMode)
{
GameManager.Inst.CameraController.CamDredgeMode();
IsInShipMode = false;
2023-08-11 16:21:26 +00:00
}
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();
}
2023-08-23 07:32:24 +00:00
private void OnTakeAim(InputValue value) // Space
{
2023-08-29 04:23:27 +00:00
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);
}
2023-08-30 05:17:26 +00:00
[SerializeField] Rigidbody projectilePrefab;
[SerializeField] Transform launchPoint;
[SerializeField] float timeOfFlight;
[SerializeField] Transform predictedPos;
private void Fire()
{
2023-09-04 04:21:57 +00:00
var myRigid = Instantiate(projectilePrefab, launchPoint.position, launchPoint.rotation);
var v = Projectile.VelocityByTime(myRigid.transform.position, predictedPos.position, timeOfFlight);
myRigid.AddForce(v, ForceMode.VelocityChange);
2023-08-30 05:17:26 +00:00
}
2023-08-03 01:03:08 +00:00
}
2023-08-03 02:54:21 +00:00
}