提交修改
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user