Files
Fishing2/Assets/Scripts/Fishing/Item/FishingLine/FLineLogicNode.cs
2026-05-06 21:11:55 +08:00

102 lines
2.7 KiB
C#

using System;
using System.Collections;
using UnityEngine;
namespace NBF
{
public enum FLineLogicNodeType
{
Start,
Bobber,
Weight,
End
}
public class FLineLogicNode : MonoBehaviour
{
[Header("节点设置")] public FLineLogicNodeType NodeType = FLineLogicNodeType.Bobber;
[SerializeField] private Rope rope;
[SerializeField] private Rigidbody preRigidbody;
private Rigidbody _rb;
private ConfigurableJoint _joint;
private FLine _parentCable;
private float _lenght;
public Rigidbody PreRigidbody => preRigidbody;
public Rigidbody Rigidbody => _rb;
public FLine ParentCable => _parentCable;
public ConfigurableJoint Joint => _joint;
public float Lenght => _lenght;
public Rope Rope => rope;
private void Awake()
{
_rb = GetComponent<Rigidbody>();
_parentCable = GetComponentInParent<FLine>();
_joint = GetComponent<ConfigurableJoint>();
}
public void SetLenght(float lenght)
{
_lenght = lenght;
if (_joint)
{
_joint.linearLimit = new SoftJointLimit() { limit = lenght };
}
if (rope)
{
rope.SetTargetLength(lenght);
}
}
/// <summary>
/// 切换约束模式
/// </summary>
/// <param name="mode"></param>
public void ChangeMode(LineMode mode)
{
if (mode == LineMode.Joint)
{
if (_joint)
{
StartCoroutine(ReconnectedBody());
}
}
else if (mode == LineMode.Constraint)
{
if (_joint) _joint.connectedBody = null;
if (NodeType == FLineLogicNodeType.End)
{
Rigidbody.isKinematic = true;
}
}
}
private IEnumerator ReconnectedBody()
{
_joint.connectedBody = preRigidbody;
yield return 1;
Rigidbody.position = preRigidbody.position;
preRigidbody.isKinematic = true;
preRigidbody.linearVelocity = Vector3.zero;
preRigidbody.angularVelocity = Vector3.zero;
if (NodeType != FLineLogicNodeType.Start)
{
preRigidbody.isKinematic = false;
}
yield return 1;
preRigidbody.linearVelocity = Vector3.zero;
preRigidbody.angularVelocity = Vector3.zero;
if (NodeType == FLineLogicNodeType.End)
{
Rigidbody.isKinematic = false;
}
}
}
}