178 lines
4.9 KiB
C#
178 lines
4.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using NBC;
|
|
// using Obi;
|
|
using UnityEngine;
|
|
|
|
namespace NBF
|
|
{
|
|
public enum LineType
|
|
{
|
|
Hand,
|
|
HandDouble,
|
|
Spinning,
|
|
SpinningFloat,
|
|
}
|
|
|
|
public class FLine : FGearBase
|
|
{
|
|
public LineType LineType;
|
|
|
|
|
|
[Header("连接点配置")] [SerializeField] private Transform anchorTransform;
|
|
[SerializeField] private List<FLineLogicNode> lineNodes = new List<FLineLogicNode>();
|
|
|
|
|
|
[Header("物理参数")] [SerializeField] private float positionCorrectionForce = 100f;
|
|
[SerializeField] private float dampingCoefficient = 10f;
|
|
[SerializeField] private int constraintIterations = 10;
|
|
[SerializeField] private bool useMassWeighting = true;
|
|
[SerializeField] private bool showDebugInfo = true;
|
|
[Header("动态间距设置")] [SerializeField] private float defaultTransitionSpeed = 2f; // 默认长度变化速度(单位/秒)
|
|
|
|
public FLineLogicNode StartNode { get; private set; }
|
|
public FLineLogicNode BobberNode => GetNode(FLineLogicNodeType.Bobber);
|
|
public FLineLogicNode EndNode { get; private set; }
|
|
|
|
private bool _isFixed = false;
|
|
// public LineMode LineMode => _lineMode;
|
|
|
|
|
|
public float LinelenghtDiferent;
|
|
|
|
|
|
protected override void OnInit()
|
|
{
|
|
if (anchorTransform == null)
|
|
{
|
|
var tipRb = Rod.Asset.LineConnectorRigidbody;
|
|
|
|
anchorTransform = tipRb.transform;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
StartNode = GetNode(FLineLogicNodeType.Start);
|
|
EndNode = GetNode(FLineLogicNodeType.End);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// LinelenghtDiferent = GetLineDistance();
|
|
|
|
//非钓鱼状态
|
|
if (Rod) Rod.Tension = Mathf.Clamp(LinelenghtDiferent, 0f, 0.05f);
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
UpdateAnchorNode();
|
|
}
|
|
|
|
public List<FLineLogicNode> GetLineNodes()
|
|
{
|
|
return new List<FLineLogicNode>(lineNodes);
|
|
}
|
|
|
|
public void Print()
|
|
{
|
|
// Log.Info($"当前线情况 TotalLength={TotalLength} CurrentStretchLength={CurrentStretchLength}");
|
|
}
|
|
|
|
|
|
public void SetFixed(bool isFixed)
|
|
{
|
|
_isFixed = isFixed;
|
|
var totalLenght = 0f;
|
|
foreach (var fLineLogicNode in lineNodes)
|
|
{
|
|
totalLenght += fLineLogicNode.Lenght;
|
|
}
|
|
|
|
// var startNode = StartNode;
|
|
if (isFixed)
|
|
{
|
|
// EndNode.Joint.connectedBody = StartNode.Rigidbody;
|
|
BobberNode.gameObject.SetActive(false);
|
|
BobberNode.Rope.gameObject.SetActive(false);
|
|
EndNode.SetConnectedBody(StartNode.Rigidbody);
|
|
EndNode.SetLenght(totalLenght + 0.2f);
|
|
}
|
|
else
|
|
{
|
|
BobberNode.gameObject.SetActive(true);
|
|
BobberNode.Rope.gameObject.SetActive(true);
|
|
EndNode.SetConnectedBody(BobberNode.Rigidbody);
|
|
EndNode.SetLenght(Rod.FloatLength);
|
|
}
|
|
// var totalLenght = lineNodes
|
|
}
|
|
|
|
#region 连接点
|
|
|
|
private void UpdateAnchorNode()
|
|
{
|
|
if (anchorTransform == null || lineNodes.Count < 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var startNode = lineNodes[0].Rigidbody;
|
|
startNode.transform.SetPositionAndRotation(anchorTransform.position, anchorTransform.rotation);
|
|
|
|
if (!startNode.isKinematic)
|
|
{
|
|
startNode.linearVelocity = Vector3.zero;
|
|
startNode.angularVelocity = Vector3.zero;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取一个节点
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
/// <returns></returns>
|
|
public FLineLogicNode GetNode(FLineLogicNodeType type)
|
|
{
|
|
foreach (var node in lineNodes)
|
|
{
|
|
if (node.NodeType == type)
|
|
{
|
|
return node;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public void SetLenght(float lenght, FLineLogicNodeType type = FLineLogicNodeType.Bobber)
|
|
{
|
|
var node = GetNode(type);
|
|
if (node != null)
|
|
{
|
|
node.SetLenght(lenght);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Tension
|
|
|
|
private float GetLineDistance()
|
|
{
|
|
//第一个节点到竿稍的位置-第一段鱼线长度
|
|
return Vector3.Distance(StartNode.transform.position, BobberNode.transform.position) -
|
|
BobberNode.Lenght;
|
|
}
|
|
|
|
public float GetTension(float weight)
|
|
{
|
|
return weight * GetLineDistance();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |