Files
Fishing2/Assets/Scripts/Fishing/Player/PlayerCharacter/PlayerCharacter.cs
2025-05-17 23:56:24 +08:00

159 lines
4.7 KiB
C#

using System;
using ECM2;
using UnityEngine;
namespace NBF
{
public class PlayerCharacter : Character
{
[Tooltip("The first person camera parent.")]
public GameObject cameraParent;
private float _cameraPitch;
private FPlayer _player;
[HideInInspector] public int nextShowSlotIndex = -1;
[Space(15.0f)] public bool invertLook = true;
[Tooltip("Look sensitivity")] public Vector2 sensitivity = new Vector2(0.05f, 0.05f);
[Space(15.0f)] [Tooltip("How far in degrees can you move the camera down.")]
public float minPitch = -80.0f;
[Tooltip("How far in degrees can you move the camera up.")]
public float maxPitch = 80.0f;
protected override void Start()
{
base.Start();
camera = BaseCamera.Main;
_player = GetComponent<FPlayer>();
transform.position = _player.Data.position;
transform.rotation = _player.Data.rotation;
App.Inst.SetMouseCurrsor(false);
InputManager.OnQuickIndexAction += OnQuickIndexAction;
InputManager.OnUseTorchAction += OnUseTorchAction;
InputManager.OnUseTelescopeAction += OnUseTelescopeAction;
}
private void Update()
{
// Move
Vector2 movementInput = InputManager.GetMovementInput();
Vector3 movementDirection = Vector3.zero;
movementDirection += Vector3.forward * movementInput.y;
movementDirection += Vector3.right * movementInput.x;
movementDirection =
movementDirection.relativeTo(cameraTransform, GetUpVector());
SetMovementDirection(movementDirection);
// Look
Vector2 lookInput = InputManager.GetLookInput() * sensitivity;
AddControlYawInput(lookInput.x);
AddControlPitchInput(invertLook ? -lookInput.y : lookInput.y, minPitch, maxPitch);
if (_player.CanChangeGear())
{
if (nextShowSlotIndex > 0)
{
Debug.LogError("切换钓组=========");
var data = Fishing.Inst.Datasource;
data.SetSelfTestGear(nextShowSlotIndex);
nextShowSlotIndex = -1;
}
}
}
private void OnDestroy()
{
InputManager.OnQuickIndexAction -= OnQuickIndexAction;
InputManager.OnUseTorchAction -= OnUseTorchAction;
InputManager.OnUseTelescopeAction -= OnUseTelescopeAction;
}
private void OnQuickIndexAction(int index)
{
nextShowSlotIndex = index;
}
private void OnUseTorchAction(bool ret)
{
if (!ret)
{
_player.Data.openLight = !_player.Data.openLight;
}
}
private void OnUseTelescopeAction(bool ret)
{
if (!ret)
{
_player.Data.openTelescope = !_player.Data.openTelescope;
_player.ToggleTelescope();
}
}
/// <summary>
/// Add input (affecting Yaw).
/// This is applied to the Character's rotation.
/// </summary>
public virtual void AddControlYawInput(float value)
{
if (value != 0.0f)
AddYawInput(value);
}
/// <summary>
/// Add input (affecting Pitch).
/// This is applied to the cameraParent's local rotation.
/// </summary>
public virtual void AddControlPitchInput(float value, float minPitch = -80.0f, float maxPitch = 80.0f)
{
if (value != 0.0f)
_cameraPitch = MathLib.ClampAngle(_cameraPitch + value, minPitch, maxPitch);
}
/// <summary>
/// Update cameraParent local rotation applying current _cameraPitch value.
/// </summary>
protected virtual void UpdateCameraParentRotation()
{
cameraParent.transform.localRotation = Quaternion.Euler(_cameraPitch, 0.0f, 0.0f);
}
/// <summary>
/// If overriden, base method MUST be called.
/// </summary>
protected virtual void LateUpdate()
{
UpdateCameraParentRotation();
}
/// <summary>
/// If overriden, base method MUST be called.
/// </summary>
protected override void Reset()
{
// Call base method implementation
base.Reset();
// Disable character's rotation,
// it is handled by the AddControlYawInput method
SetRotationMode(RotationMode.None);
}
}
}