209 lines
4.5 KiB
C#
209 lines
4.5 KiB
C#
using System.Collections;
|
|
using Obi;
|
|
using Obvious.Soap;
|
|
using SoapCustomVariable;
|
|
using UnityEngine;
|
|
|
|
public class RopeLengthController : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private BoolVariable isBailOpen;
|
|
|
|
[SerializeField]
|
|
private BoolVariable isCasting;
|
|
|
|
[SerializeField]
|
|
private FloatVariable ropeReelSpeed;
|
|
|
|
[SerializeField]
|
|
private FloatVariable lineLength;
|
|
|
|
[SerializeField]
|
|
private FloatVariable throwDistance;
|
|
|
|
[SerializeField]
|
|
private FloatVariable lineTension;
|
|
|
|
[SerializeField]
|
|
private FloatVariable attachmentSpeed;
|
|
|
|
[SerializeField]
|
|
private Vector3Variable playerForward;
|
|
|
|
[SerializeField]
|
|
private ObiRopeCursor lineCursor;
|
|
|
|
[SerializeField]
|
|
private ObiRope rope;
|
|
|
|
[SerializeField]
|
|
private KeyCode reelInKey;
|
|
|
|
[SerializeField]
|
|
private KeyCode reelOutKey;
|
|
|
|
[Header("Is line can be casted, it's used for unwind rope and control main length")]
|
|
[SerializeField]
|
|
private bool isCastable;
|
|
|
|
[SerializeField]
|
|
private AttachmentBody attachedBody;
|
|
|
|
[SerializeField]
|
|
public Transform rodTipTarget;
|
|
|
|
[SerializeField]
|
|
private ScriptableEventFloat onCastedLine;
|
|
|
|
[SerializeField]
|
|
private FloatVariable attachedTotalMass;
|
|
|
|
[SerializeField]
|
|
private FSMVariable playerState;
|
|
|
|
[SerializeField]
|
|
private ScriptableEventFloat onUnwindRope;
|
|
|
|
[SerializeField]
|
|
private Vector3Variable currentAttachmentPosition;
|
|
|
|
[SerializeField]
|
|
private FloatVariable attachmentDistance;
|
|
|
|
[SerializeField]
|
|
private FloatVariable calculatedRopeLength;
|
|
|
|
private void Start()
|
|
{
|
|
rope = rope ?? GetComponent<ObiRope>();
|
|
lineCursor = lineCursor ?? GetComponent<ObiRopeCursor>();
|
|
lineCursor.ChangeLength(lineLength);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (isCastable)
|
|
{
|
|
attachedBody.OnCollide += AttachedBody_OnCollide;
|
|
}
|
|
if (isCastable)
|
|
{
|
|
onUnwindRope.OnRaised += OnUnwindRope_OnRaised;
|
|
}
|
|
if (isCastable)
|
|
{
|
|
playerState.OnValueChanged += PlayerState_OnValueChanged;
|
|
}
|
|
if (isCastable)
|
|
{
|
|
currentAttachmentPosition.OnValueChanged += CurrentAttachmentPosition_OnValueChanged;
|
|
}
|
|
}
|
|
|
|
private void CurrentAttachmentPosition_OnValueChanged(Vector3 position)
|
|
{
|
|
attachmentDistance.Value = Vector3.Distance(rodTipTarget.position, position);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (isCastable)
|
|
{
|
|
attachedBody.OnCollide -= AttachedBody_OnCollide;
|
|
}
|
|
if (isCastable)
|
|
{
|
|
onUnwindRope.OnRaised -= OnUnwindRope_OnRaised;
|
|
}
|
|
if (isCastable)
|
|
{
|
|
playerState.OnValueChanged -= PlayerState_OnValueChanged;
|
|
}
|
|
if (isCastable)
|
|
{
|
|
currentAttachmentPosition.OnValueChanged -= CurrentAttachmentPosition_OnValueChanged;
|
|
}
|
|
}
|
|
|
|
private void PlayerState_OnValueChanged(State state)
|
|
{
|
|
if (state == State.casting)
|
|
{
|
|
StopAllCoroutines();
|
|
ThrowAtDistance((float)attachmentSpeed * Time.fixedDeltaTime * 30f);
|
|
}
|
|
}
|
|
|
|
private void OnUnwindRope_OnRaised(float obj)
|
|
{
|
|
UnwindLine(obj);
|
|
}
|
|
|
|
private void AttachedBody_OnCollide()
|
|
{
|
|
StopAllCoroutines();
|
|
if ((State)playerState == State.casting)
|
|
{
|
|
playerState.Value = State.fishing;
|
|
}
|
|
}
|
|
|
|
private void OnCastingRope(float value)
|
|
{
|
|
Debug.Log("CHANGED");
|
|
if (value > 0f)
|
|
{
|
|
StopAllCoroutines();
|
|
ThrowAtDistance((float)attachmentSpeed * Time.fixedDeltaTime * 30f);
|
|
}
|
|
}
|
|
|
|
private void ThrowAtDistance(float value)
|
|
{
|
|
if ((bool)isBailOpen)
|
|
{
|
|
StartCoroutine(ThrowCoroutine(0f));
|
|
}
|
|
IEnumerator ThrowCoroutine(float distance)
|
|
{
|
|
float startLength = 0.5f;
|
|
Debug.Log($"REST LENGTH : {rope.restLength}");
|
|
do
|
|
{
|
|
float a = Vector3.Distance(rodTipTarget.position, attachedBody.transform.position);
|
|
attachedBody.RBody.AddForce(playerForward.Value, ForceMode.VelocityChange);
|
|
startLength = Mathf.Max(a, startLength);
|
|
UnwindLine(attachedBody.RBody.linearVelocity.magnitude * Time.deltaTime);
|
|
yield return null;
|
|
}
|
|
while ((bool)isBailOpen);
|
|
}
|
|
}
|
|
|
|
private void UnwindLine(float value)
|
|
{
|
|
rope.stretchingScale += value;
|
|
lineLength.Value = rope.stretchingScale;
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
currentAttachmentPosition.Value = attachedBody.transform.position;
|
|
}
|
|
|
|
private void TensionHandler()
|
|
{
|
|
float num = Vector3.Distance(rodTipTarget.position, attachedBody.transform.position);
|
|
float num2 = Mathf.Clamp(0f - (rope.CalculateLength() - 0.1f - num), 0f, 1000f);
|
|
lineTension.Value = Mathf.MoveTowards(lineTension.Value, (num2 > 0f) ? attachedTotalMass.Value : 0f, Time.deltaTime * attachedTotalMass.Value);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (playerState.Value != State.prepare && playerState.Value != State.casting && isBailOpen.Value)
|
|
{
|
|
UnwindLine((float)attachmentSpeed * Time.deltaTime);
|
|
}
|
|
}
|
|
}
|