提交修改
This commit is contained in:
@@ -44,7 +44,17 @@ namespace NBF
|
||||
set => nodeType = value;
|
||||
}
|
||||
|
||||
public float Lenght => _joint != null ? _joint.linearLimit.limit : 0f;
|
||||
public float Lenght { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 真实实际长度
|
||||
/// </summary>
|
||||
public float RealLength { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前逻辑链总长度超出配置总长度的部分,小于等于零时记为 0。
|
||||
/// </summary>
|
||||
public float StretchLength { get; private set; }
|
||||
|
||||
public Rigidbody Body => body;
|
||||
|
||||
@@ -77,6 +87,7 @@ namespace NBF
|
||||
{
|
||||
EnsureFeatureCache();
|
||||
UpdateMotionControl(Time.fixedDeltaTime);
|
||||
UpdateLenght();
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
@@ -87,6 +98,28 @@ namespace NBF
|
||||
}
|
||||
|
||||
segmentLengthToNext = Mathf.Max(0f, segmentLengthToNext);
|
||||
|
||||
}
|
||||
|
||||
private void UpdateLenght()
|
||||
{
|
||||
//更新长度
|
||||
Lenght = 0;
|
||||
RealLength = 0;
|
||||
StretchLength = 0;
|
||||
if (_joint)
|
||||
{
|
||||
Lenght = _joint.linearLimit.limit;
|
||||
if (_joint && _joint.connectedBody)
|
||||
{
|
||||
RealLength = Vector3.Distance(transform.position, _joint.connectedBody.transform.position);
|
||||
}
|
||||
}
|
||||
|
||||
if (RealLength > Lenght)
|
||||
{
|
||||
StretchLength = RealLength - Lenght;
|
||||
}
|
||||
}
|
||||
|
||||
#region Line
|
||||
|
||||
@@ -53,7 +53,7 @@ namespace NBF
|
||||
private void FixedUpdate()
|
||||
{
|
||||
UpdateAnchorNode();
|
||||
UpdateStretchLength();
|
||||
UpdateBreakCountdown(Time.fixedDeltaTime);
|
||||
}
|
||||
|
||||
#region Start Node
|
||||
@@ -221,7 +221,7 @@ namespace NBF
|
||||
/// </summary>
|
||||
[Header("Limit Detection")]
|
||||
public float CurrentStretchLength { get; private set; }
|
||||
|
||||
|
||||
[Min(0f)]
|
||||
// 极限判定的长度容差,允许链路在总长或单段长度上存在少量误差。
|
||||
[SerializeField]
|
||||
@@ -230,7 +230,12 @@ namespace NBF
|
||||
[Min(0f)]
|
||||
// 达到极限后,只有当前超长值大于该阈值时,才开始进入断线候选计时。
|
||||
[SerializeField]
|
||||
private float breakStretchThreshold = 0.05f;
|
||||
private float breakStretchThreshold = 0.08f;
|
||||
|
||||
[Min(0f)]
|
||||
// 断线候选状态允许持续的最大时间;超过后会发出一次断线消息。
|
||||
[SerializeField]
|
||||
private float breakLimitDuration = 3f;
|
||||
|
||||
/// <summary>
|
||||
/// 当鱼线达到断线条件时发出的一次性消息。
|
||||
@@ -238,12 +243,24 @@ namespace NBF
|
||||
/// </summary>
|
||||
public event Action<FishingLineSolver> OnLineBreakRequested;
|
||||
|
||||
/// <summary>
|
||||
/// 当前是否处于极限状态。
|
||||
/// 只要整链超出总长度容差,或任一逻辑段超出单段容差,即认为到达极限。
|
||||
/// </summary>
|
||||
public bool IsAtLimit { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前断线候选状态的累计时间。
|
||||
/// 只有在处于极限状态,且 CurrentStretchLength 大于断线阈值时才会累加;否则重置为 0。
|
||||
/// </summary>
|
||||
public float LimitStateTime { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前极限断线消息是否已经发出过。
|
||||
/// 在退出断线候选状态前只会发一次,避免重复通知。
|
||||
/// </summary>
|
||||
public bool HasBreakNotificationSent { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前拉力极限百分比。
|
||||
/// 当超长值小于等于 lengthLimitTolerance 时为 0;
|
||||
@@ -252,6 +269,10 @@ namespace NBF
|
||||
/// </summary>
|
||||
public float CurrentBreakStretchPercent => EvaluateBreakStretchPercent(CurrentStretchLength);
|
||||
|
||||
/// <summary>
|
||||
/// 当前是否正在进行断线候选计时。
|
||||
/// </summary>
|
||||
public bool IsBreakCountdownActive => IsAtLimit && CurrentStretchLength > breakStretchThreshold;
|
||||
|
||||
private float EvaluateBreakStretchPercent(float stretchLength)
|
||||
{
|
||||
@@ -273,9 +294,61 @@ namespace NBF
|
||||
return Mathf.InverseLerp(lengthLimitTolerance, breakStretchThreshold, stretchLength) * 100f;
|
||||
}
|
||||
|
||||
private void UpdateStretchLength()
|
||||
private void SetLimitState(bool isAtLimit)
|
||||
{
|
||||
|
||||
IsAtLimit = isAtLimit;
|
||||
}
|
||||
|
||||
private void UpdateBreakCountdown(float deltaTime)
|
||||
{
|
||||
if (logicalNodes == null || logicalNodes.Length == 0)
|
||||
{
|
||||
SetLimitState(false);
|
||||
ResetLimitState();
|
||||
return;
|
||||
}
|
||||
|
||||
CurrentStretchLength = 0;
|
||||
//计算长度
|
||||
foreach (var node in logicalNodes)
|
||||
{
|
||||
CurrentStretchLength += node.StretchLength;
|
||||
}
|
||||
|
||||
SetLimitState(CurrentStretchLength > lengthLimitTolerance);
|
||||
|
||||
if (!IsBreakCountdownActive)
|
||||
{
|
||||
LimitStateTime = 0f;
|
||||
HasBreakNotificationSent = false;
|
||||
return;
|
||||
}
|
||||
|
||||
LimitStateTime += Mathf.Max(0f, deltaTime);
|
||||
if (HasBreakNotificationSent || LimitStateTime < breakLimitDuration)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
HasBreakNotificationSent = true;
|
||||
NotifyLineBreakRequested();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发出鱼线达到断线条件的消息。
|
||||
/// 这里预留给外部订阅,当前不在求解器内部直接执行断线逻辑。
|
||||
/// </summary>
|
||||
private void NotifyLineBreakRequested()
|
||||
{
|
||||
OnLineBreakRequested?.Invoke(this);
|
||||
}
|
||||
|
||||
private void ResetLimitState()
|
||||
{
|
||||
CurrentStretchLength = 0f;
|
||||
IsAtLimit = false;
|
||||
LimitStateTime = 0f;
|
||||
HasBreakNotificationSent = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -79,9 +79,9 @@ namespace NBF
|
||||
solver.SetLenght(targetFirstSegmentLength);
|
||||
}
|
||||
|
||||
if (solver.CurrentBreakStretchPercent > 0)
|
||||
if (solver.CurrentBreakStretchPercent > 10)
|
||||
{
|
||||
// Debug.LogError(solver.CurrentBreakStretchPercent);
|
||||
Debug.LogError($"当前极限情况,CurrentBreakStretchPercent={solver.CurrentBreakStretchPercent} CurrentStretchLength={solver.CurrentStretchLength} LimitStateTime={solver.LimitStateTime}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user