去掉复杂结构,一切从简
This commit is contained in:
137
Assets/Scripts/Fishing/Player/Player.cs
Normal file
137
Assets/Scripts/Fishing/Player/Player.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ECM2;
|
||||
using ECM2.Examples.FirstPerson;
|
||||
using Fantasy.Async;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
[Header("角色参数")] public float EyeAngle;
|
||||
public bool IsGrounded;
|
||||
public float Speed;
|
||||
public float RotationSpeed;
|
||||
public Vector2 MoveInput;
|
||||
public bool Run;
|
||||
|
||||
[Header("角色节点")] public Transform Root;
|
||||
public Transform Eye;
|
||||
public Transform FppLook;
|
||||
public Transform IK;
|
||||
|
||||
[Header("角色控制")] public PlayerModelAsset ModelAsset;
|
||||
public CharacterMovement Character;
|
||||
public FirstPersonCharacter FirstPerson;
|
||||
|
||||
|
||||
[Header("视角相关")] public float MouseSensitivity = 0.1f;
|
||||
[Space(15f)] public bool invertLook = true;
|
||||
|
||||
public float minPitch = -60f;
|
||||
|
||||
public float maxPitch = 60f;
|
||||
|
||||
public bool IsLureRod => false;
|
||||
|
||||
public bool IsSelf;
|
||||
|
||||
#region 生命周期
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
CreatePlayerModel();
|
||||
if (IsSelf)
|
||||
{
|
||||
gameObject.AddComponent<PlayerInput>();
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 手持物品
|
||||
|
||||
private FHandItem HandItem;
|
||||
private bool IsChangeItemIng;
|
||||
|
||||
public async FTask UseItem(int configId, List<int> bindItems)
|
||||
{
|
||||
if (IsChangeItemIng) return;
|
||||
IsChangeItemIng = true;
|
||||
|
||||
if (HandItem != null)
|
||||
{
|
||||
await UnUseItem();
|
||||
}
|
||||
|
||||
// ModelAsset.PlayerAnimator.OnUseItem(itemView);
|
||||
|
||||
// if (currItem == null)
|
||||
// {
|
||||
// //收起物品
|
||||
// await UnUseItem(prevItem);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// // 先收起旧的物品
|
||||
// await UnUseItemConfirm(prevItem);
|
||||
// var handItem = Player.HandItem;
|
||||
// //拿起新物品
|
||||
// var itemType = handItem.ConfigID.GetItemType();
|
||||
// if (itemType == ItemType.Rod)
|
||||
// {
|
||||
// var itemView = handItem.GetOrAddComponent<PlayerItemView>();
|
||||
// await itemView.InitShow(handItem);
|
||||
// Unity.ModelAsset.PlayerAnimator.OnUseItem(itemView);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// var stateView = Player.GetComponent<PlayerStateView>();
|
||||
// if (stateView != null && stateView.CurrentStateView is PlayerStageViewIdle playerStageViewIdle)
|
||||
// {
|
||||
// playerStageViewIdle.TakeLine();
|
||||
// }
|
||||
|
||||
IsChangeItemIng = false;
|
||||
}
|
||||
|
||||
public async FTask UnUseItem()
|
||||
{
|
||||
if (HandItem != null)
|
||||
{
|
||||
ModelAsset.PlayerAnimator.OnUnUseItem();
|
||||
await FTask.Wait(Game.Main, 100);
|
||||
}
|
||||
|
||||
Destroy(HandItem.gameObject);
|
||||
HandItem = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 模型创建
|
||||
|
||||
private void CreatePlayerModel()
|
||||
{
|
||||
var modelObject = PrefabsHelper.CreatePlayer(Root, "Human_Male");
|
||||
modelObject.transform.localPosition = Vector3.zero;
|
||||
ModelAsset = modelObject.GetComponent<PlayerModelAsset>();
|
||||
ModelAsset.SetPlayer(FppLook);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Fishing/Player/Player.cs.meta
Normal file
3
Assets/Scripts/Fishing/Player/Player.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7c69e72330154a86b756d89e82d276e9
|
||||
timeCreated: 1777437830
|
||||
205
Assets/Scripts/Fishing/Player/PlayerInput.cs
Normal file
205
Assets/Scripts/Fishing/Player/PlayerInput.cs
Normal file
@@ -0,0 +1,205 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Fantasy;
|
||||
using Fantasy.Entitas.Interface;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public class PlayerInput : PlayerMonoBehaviour
|
||||
{
|
||||
#region 生命周期
|
||||
|
||||
protected override void OnAwake()
|
||||
{
|
||||
AddInputEvent();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
UpdateInput();
|
||||
UpdateMove();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
RemoveInputEvent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Input
|
||||
|
||||
private void UpdateInput()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Alpha0))
|
||||
{
|
||||
// SetLineLength(lineLength);
|
||||
}
|
||||
else if (Input.GetKeyDown(KeyCode.Plus) || Input.GetKeyDown(KeyCode.Equals))
|
||||
{
|
||||
// Player.HandItem.LineLength += 0.1f;
|
||||
}
|
||||
else if (Input.GetKeyDown(KeyCode.Minus))
|
||||
{
|
||||
// Player.HandItem.LineLength -= 0.1f;
|
||||
}
|
||||
}
|
||||
|
||||
private void AddInputEvent()
|
||||
{
|
||||
InputManager.OnPlayerPerformed += OnPlayerCanceled;
|
||||
InputManager.OnPlayerPerformed += OnPlayerPerformed;
|
||||
|
||||
InputManager.OnPlayerValueCanceled += OnPlayerValueCanceled;
|
||||
InputManager.OnPlayerValuePerformed += OnPlayerValuePerformed;
|
||||
}
|
||||
|
||||
private void RemoveInputEvent()
|
||||
{
|
||||
InputManager.OnPlayerPerformed += OnPlayerCanceled;
|
||||
InputManager.OnPlayerPerformed += OnPlayerPerformed;
|
||||
|
||||
InputManager.OnPlayerValueCanceled += OnPlayerValueCanceled;
|
||||
InputManager.OnPlayerValuePerformed += OnPlayerValuePerformed;
|
||||
}
|
||||
|
||||
private void OnPlayerPerformed(string action)
|
||||
{
|
||||
if (action == InputDef.Player.Run)
|
||||
{
|
||||
Player.Run = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPlayerCanceled(string action)
|
||||
{
|
||||
if (action == InputDef.Player.Run)
|
||||
{
|
||||
Player.Run = false;
|
||||
}
|
||||
else if (action == InputDef.Player.ToBag)
|
||||
{
|
||||
//取消手持物品
|
||||
Log.Info($"取消手持物品");
|
||||
Player.UnUseItem().Coroutine();
|
||||
}
|
||||
else if (action.StartsWith(InputDef.Player.QuickStarts))
|
||||
{
|
||||
var index = int.Parse(action.Replace(InputDef.Player.QuickStarts, string.Empty));
|
||||
Log.Info($"快速使用===={index}");
|
||||
var item = RoleModel.Instance.GetSlotItem(index - 1);
|
||||
if (item != null)
|
||||
{
|
||||
List<ItemInfo> children = RoleModel.Instance.GetBindItems(item.Id);
|
||||
List<int> bindItems = children.Select(itemInfo => itemInfo.ConfigId).ToList();
|
||||
Player.UseItem(item.ConfigId, bindItems).Coroutine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPlayerValueCanceled(InputAction.CallbackContext context)
|
||||
{
|
||||
var actionName = context.action.name;
|
||||
if (actionName == InputDef.Player.Move)
|
||||
{
|
||||
Player.MoveInput = Vector2.zero;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPlayerValuePerformed(InputAction.CallbackContext context)
|
||||
{
|
||||
var actionName = context.action.name;
|
||||
if (actionName == InputDef.Player.Move)
|
||||
{
|
||||
var v2 = context.ReadValue<Vector2>();
|
||||
Player.MoveInput = v2;
|
||||
}
|
||||
else if (actionName == InputDef.Player.Look)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Move
|
||||
|
||||
private Quaternion _lastRotation;
|
||||
|
||||
private void UpdateMove()
|
||||
{
|
||||
UpdateGrounded();
|
||||
ProcessMoveStates();
|
||||
UpdateLookInput();
|
||||
}
|
||||
|
||||
private void ProcessMoveStates()
|
||||
{
|
||||
{
|
||||
var num2 = Player.Run ? 7 : 5;
|
||||
Vector3 vector2 = Player.FirstPerson.GetRightVector() * Player.MoveInput.x * num2;
|
||||
vector2 += Player.FirstPerson.GetForwardVector() * Player.MoveInput.y * num2;
|
||||
// if (checkWaterBound)
|
||||
// {
|
||||
// SetMovementDirectionWithRaycastCheck(vector2);
|
||||
// }
|
||||
// else
|
||||
{
|
||||
Player.FirstPerson.SetMovementDirection(vector2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateGrounded()
|
||||
{
|
||||
Player.IsGrounded = Player.FirstPerson.IsGrounded();
|
||||
Player.Speed = Player.FirstPerson.velocity.magnitude;
|
||||
|
||||
Quaternion rotation = Player.FirstPerson.transform.rotation;
|
||||
|
||||
// 计算当前帧与上一帧的旋转差异
|
||||
Quaternion rotationDelta = rotation * Quaternion.Inverse(_lastRotation);
|
||||
|
||||
// 将四元数转换为角度轴表示
|
||||
rotationDelta.ToAngleAxis(out float angle, out Vector3 axis);
|
||||
|
||||
// 确保角度在0-360范围内
|
||||
if (angle > 180f) angle -= 360f;
|
||||
|
||||
// 获取Y轴旋转分量(归一化处理)
|
||||
float yRotation = 0f;
|
||||
if (Mathf.Abs(angle) > 0.001f && Mathf.Abs(axis.y) > 0.1f)
|
||||
{
|
||||
// 计算Y轴方向的旋转角度(考虑旋转轴方向)
|
||||
yRotation = angle * Mathf.Sign(axis.y);
|
||||
}
|
||||
|
||||
float maxTurnSpeed = 180f; // 度/秒
|
||||
// 转换为角速度并归一化到[-1, 1]
|
||||
float angularSpeed = yRotation / Time.deltaTime;
|
||||
float turnValue = Mathf.Clamp(angularSpeed / maxTurnSpeed, -1f, 1f);
|
||||
|
||||
|
||||
Player.RotationSpeed = turnValue;
|
||||
|
||||
_lastRotation = rotation;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Look
|
||||
|
||||
private void UpdateLookInput()
|
||||
{
|
||||
Vector2 value = InputManager.GetLookInput();
|
||||
var u3d = Player;
|
||||
u3d.FirstPerson.AddControlYawInput(value.x * u3d.MouseSensitivity);
|
||||
u3d.FirstPerson.AddControlPitchInput((u3d.invertLook ? 0f - value.y : value.y) * u3d.MouseSensitivity,
|
||||
u3d.minPitch, u3d.maxPitch);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Fishing/Player/PlayerInput.cs.meta
Normal file
3
Assets/Scripts/Fishing/Player/PlayerInput.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b0ff16b93bf44060b69e6b052a4f3d63
|
||||
timeCreated: 1777449292
|
||||
8
Assets/Scripts/Fishing/Player/RemotePlayer.cs
Normal file
8
Assets/Scripts/Fishing/Player/RemotePlayer.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public class RemotePlayer : MonoBehaviour
|
||||
{
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Fishing/Player/RemotePlayer.cs.meta
Normal file
3
Assets/Scripts/Fishing/Player/RemotePlayer.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 83d5c2102a7749f0acb36f871d75566e
|
||||
timeCreated: 1777438074
|
||||
Reference in New Issue
Block a user