196 lines
5.4 KiB
C#
196 lines
5.4 KiB
C#
using System;
|
|
using ECM2;
|
|
using UnityEngine;
|
|
|
|
namespace NBF
|
|
{
|
|
public class PlayerCharacter : Character
|
|
{
|
|
public GameObject cameraParent;
|
|
|
|
private float _cameraPitch;
|
|
private FPlayer _player;
|
|
[HideInInspector] public int nextShowSlotIndex = -1;
|
|
|
|
|
|
[Space(15.0f)] public bool invertLook = true;
|
|
[Tooltip("视角旋转敏感度")] public Vector2 sensitivity = new Vector2(0.05f, 0.05f);
|
|
|
|
[Space(15.0f)] public float minPitch = -80.0f;
|
|
public float maxPitch = 80.0f;
|
|
|
|
[Tooltip("手上下最大角度")] public Vector2 handMaxAngle = new Vector2(0, 0);
|
|
|
|
public bool IsSelf;
|
|
|
|
protected override void Start()
|
|
{
|
|
base.Start();
|
|
|
|
_player = GetComponent<FPlayer>();
|
|
IsSelf = _player.Data.PlayerID == GameModel.RoleID;
|
|
cameraParent = _player.CameraRoot.gameObject;
|
|
if (IsSelf)
|
|
{
|
|
camera = BaseCamera.Main;
|
|
}
|
|
|
|
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()
|
|
{
|
|
UpdatePlayerPositionAndRotation();
|
|
UpdateGear();
|
|
}
|
|
|
|
protected virtual void LateUpdate()
|
|
{
|
|
UpdateCameraParentRotation();
|
|
UpdatePlayerHandView();
|
|
}
|
|
|
|
|
|
protected override void Reset()
|
|
{
|
|
base.Reset();
|
|
SetRotationMode(RotationMode.None);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
InputManager.OnQuickIndexAction -= OnQuickIndexAction;
|
|
InputManager.OnUseTorchAction -= OnUseTorchAction;
|
|
InputManager.OnUseTelescopeAction -= OnUseTelescopeAction;
|
|
}
|
|
|
|
#region 肩膀控制
|
|
|
|
private void UpdatePlayerHandView()
|
|
{
|
|
// var backValue = 0f;
|
|
|
|
var angle = cameraParent.transform.localEulerAngles.x;
|
|
angle = (angle > 180f) ? angle - 360f : angle;
|
|
angle *= -1;
|
|
if (angle > handMaxAngle.x)
|
|
{
|
|
angle = handMaxAngle.x;
|
|
}
|
|
else if (angle < handMaxAngle.y)
|
|
{
|
|
angle = handMaxAngle.y;
|
|
}
|
|
|
|
Debug.Log(angle);
|
|
_player.BackSpine.localRotation *= Quaternion.Euler(0, 0, angle);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 钓组控制
|
|
|
|
private void UpdateGear()
|
|
{
|
|
if (_player.CanChangeGear())
|
|
{
|
|
if (nextShowSlotIndex > 0)
|
|
{
|
|
Debug.LogError("切换钓组=========");
|
|
var data = Fishing.Inst.Datasource;
|
|
data.SetSelfTestGear(nextShowSlotIndex);
|
|
nextShowSlotIndex = -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 角色位移和旋转控制
|
|
|
|
private void UpdatePlayerPositionAndRotation()
|
|
{
|
|
// 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加输入(影响偏航)。
|
|
/// 此操作应用于角色的旋转。
|
|
/// </summary>
|
|
public virtual void AddControlYawInput(float value)
|
|
{
|
|
if (value != 0.0f)
|
|
AddYawInput(value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加输入(影响俯仰)。
|
|
/// 此操作应用于相机父级的本地旋转。
|
|
/// </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>
|
|
/// 根据当前的 _cameraPitch 值更新 cameraParent 的本地旋转。
|
|
/// </summary>
|
|
protected virtual void UpdateCameraParentRotation()
|
|
{
|
|
cameraParent.transform.localRotation = Quaternion.Euler(_cameraPitch, 0.0f, 0.0f);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 按键输入事件
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |