283 lines
7.5 KiB
C#
283 lines
7.5 KiB
C#
using Obi;
|
|
using Obvious.Soap;
|
|
using UFS2.Gameplay;
|
|
using UFS2.Helpers;
|
|
using UnityEngine;
|
|
|
|
public class FLineHandler : MonoBehaviour
|
|
{
|
|
public enum LineType
|
|
{
|
|
None = 0,
|
|
OneSegment = 1,
|
|
TwoSegment = 2,
|
|
ThereSegment = 3
|
|
}
|
|
|
|
public LineType lineType = LineType.TwoSegment;
|
|
|
|
public ObiRope obiRopeSegment_1;
|
|
|
|
public ObiRope obiRopeSegment_2;
|
|
|
|
public ObiRope obiRopeSegment_3;
|
|
|
|
public FixedJoint LineConnector_0;
|
|
|
|
public SpringJoint LineConnector_1;
|
|
|
|
public SpringJoint LineConnector_2;
|
|
|
|
public SpringJoint LineConnector_3;
|
|
|
|
[HideInInspector]
|
|
public FFishingLine currentRodFishingLineComponent;
|
|
|
|
[SerializeField]
|
|
private BoolVariable isFishCanTaken;
|
|
|
|
public float PhisicsLineOut;
|
|
|
|
public float ObiLineOut;
|
|
|
|
[HideInInspector]
|
|
public Rigidbody EndLineRigidbody_0;
|
|
|
|
[HideInInspector]
|
|
public Rigidbody EndLineRigidbody_1;
|
|
|
|
[HideInInspector]
|
|
public Rigidbody EndLineRigidbody_2;
|
|
|
|
[HideInInspector]
|
|
public Rigidbody EndLineRigidbody_3;
|
|
|
|
[SerializeField]
|
|
private GameObject _LineRaycaster;
|
|
|
|
public Rigidbody EndLineRigidbody
|
|
{
|
|
get
|
|
{
|
|
if (lineType != LineType.OneSegment)
|
|
{
|
|
return EndLineRigidbody_2;
|
|
}
|
|
return EndLineRigidbody_1;
|
|
}
|
|
}
|
|
|
|
public float GetRopeLenght()
|
|
{
|
|
return PhisicsLineOut;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
ObiLineOut = obiRopeSegment_1.stretchingScale;
|
|
if ((bool)LineConnector_0)
|
|
{
|
|
EndLineRigidbody_0 = LineConnector_0.GetComponent<Rigidbody>();
|
|
}
|
|
if ((bool)LineConnector_1)
|
|
{
|
|
EndLineRigidbody_1 = LineConnector_1.GetComponent<Rigidbody>();
|
|
}
|
|
if ((bool)LineConnector_2)
|
|
{
|
|
EndLineRigidbody_2 = LineConnector_2.GetComponent<Rigidbody>();
|
|
}
|
|
if ((bool)LineConnector_3)
|
|
{
|
|
EndLineRigidbody_3 = LineConnector_3.GetComponent<Rigidbody>();
|
|
}
|
|
switch (lineType)
|
|
{
|
|
case LineType.OneSegment:
|
|
Generate(obiRopeSegment_1, new Transform[2] { LineConnector_0.transform, LineConnector_1.transform });
|
|
break;
|
|
case LineType.TwoSegment:
|
|
Generate(obiRopeSegment_1, new Transform[2] { LineConnector_0.transform, LineConnector_1.transform });
|
|
Generate(obiRopeSegment_2, new Transform[2] { LineConnector_1.transform, LineConnector_2.transform });
|
|
break;
|
|
case LineType.ThereSegment:
|
|
Generate(obiRopeSegment_1, new Transform[2] { LineConnector_0.transform, LineConnector_1.transform });
|
|
Generate(obiRopeSegment_2, new Transform[2] { LineConnector_1.transform, LineConnector_2.transform });
|
|
Generate(obiRopeSegment_3, new Transform[2] { LineConnector_1.transform, LineConnector_3.transform });
|
|
break;
|
|
default:
|
|
Debug.LogError("FLineHandler don't have implementation");
|
|
break;
|
|
case LineType.None:
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void Generate(ObiRope obiRope, Transform[] points)
|
|
{
|
|
if (points.Length < 2)
|
|
{
|
|
Debug.LogError("Rope line Must have more than two points");
|
|
return;
|
|
}
|
|
_ = points[0];
|
|
_ = points[^1];
|
|
ObiRopeBlueprint blueprint = ScriptableObject.CreateInstance<ObiRopeBlueprint>();
|
|
int filter = ObiUtils.MakeFilter(65535, 0);
|
|
int num = 25;
|
|
Transform transform = points[0];
|
|
Transform transform2 = points[^1];
|
|
AddControlPoint(transform, transform2, transform.name);
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
AddControlPoint(transform, transform2, $"middle-point{i}");
|
|
}
|
|
AddControlPoint(transform, transform2, transform2.name);
|
|
blueprint.path.FlushEvents();
|
|
blueprint.GenerateImmediate();
|
|
AddAttachment(points[0], blueprint.groups[0]);
|
|
AddAttachment(points[^1], blueprint.groups[^1]);
|
|
obiRope.ropeBlueprint = blueprint;
|
|
void AddAttachment(Transform target, ObiParticleGroup obiGroup)
|
|
{
|
|
ObiParticleAttachment obiParticleAttachment = obiRope.gameObject.AddComponent<ObiParticleAttachment>();
|
|
obiParticleAttachment.target = target;
|
|
obiParticleAttachment.particleGroup = obiGroup;
|
|
}
|
|
void AddControlPoint(Transform startTransform, Transform endTransform, string name)
|
|
{
|
|
Vector3 vector = base.transform.InverseTransformPoint(startTransform.position);
|
|
Vector3 normalized = (endTransform.InverseTransformPoint(endTransform.position) - vector).normalized;
|
|
blueprint.path.AddControlPoint(vector, -normalized, normalized, Vector3.up, 0.01f, 0.1f, 0.058f, filter, Color.white, name);
|
|
}
|
|
}
|
|
|
|
private void WaterCutEffectHandler()
|
|
{
|
|
if (!(_LineRaycaster == null))
|
|
{
|
|
Ray ray = new Ray(LineConnector_0.transform.position, LineConnector_1.transform.position - LineConnector_0.transform.position);
|
|
float maxDistance = Vector3.Distance(LineConnector_0.transform.position, LineConnector_1.transform.position);
|
|
if (Physics.Raycast(ray, out var hitInfo, maxDistance, LayerMask.GetMask("Water")))
|
|
{
|
|
_LineRaycaster.transform.position = hitInfo.point;
|
|
}
|
|
else
|
|
{
|
|
_LineRaycaster.transform.position = LineConnector_1.transform.position;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CanTakeFishHandler()
|
|
{
|
|
if (!(FishEntity.CurrentFishInFight == null))
|
|
{
|
|
Vector3 position = FishEntity.CurrentFishInFight.transform.position;
|
|
position.y = 0f;
|
|
Vector3 position2 = LineConnector_0.transform.position;
|
|
position2.y = 0f;
|
|
float num = Vector3.Distance(position, position2);
|
|
float depth = GameWaterSystem.GetDepth(FishEntity.CurrentFishInFight.transform.position);
|
|
isFishCanTaken.Value = num < 3f && depth < 0.5f;
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
WaterCutEffectHandler();
|
|
CanTakeFishHandler();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
FishCatchState.OnEnter += FishCatchState_OnEnter;
|
|
}
|
|
|
|
private void FishCatchState_OnEnter()
|
|
{
|
|
SetRopeLenght(5f, currentRodFishingLineComponent.rod);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
FishCatchState.OnEnter -= FishCatchState_OnEnter;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
ObiLineOut = 0.1f + PhisicsLineOut;
|
|
float target = (0f - Mathf.Clamp(currentRodFishingLineComponent.linelenghtDiferent, -1f, 0f)) * 0.1f;
|
|
if (currentRodFishingLineComponent.linelenghtDiferent >= 0f)
|
|
{
|
|
obiRopeSegment_1.stretchCompliance = Mathf.MoveTowards(obiRopeSegment_1.stretchCompliance, target, Time.smoothDeltaTime * (1f * currentRodFishingLineComponent.linelenghtDiferent));
|
|
}
|
|
else
|
|
{
|
|
obiRopeSegment_1.stretchCompliance = Mathf.MoveTowards(obiRopeSegment_1.stretchCompliance, target, Time.smoothDeltaTime * 0.1f);
|
|
}
|
|
if (PhisicsLineOut == 0f)
|
|
{
|
|
obiRopeSegment_1.stretchCompliance = 0f;
|
|
}
|
|
if ((bool)obiRopeSegment_2)
|
|
{
|
|
if (!currentRodFishingLineComponent.rod.currentFish)
|
|
{
|
|
obiRopeSegment_2.stretchCompliance = obiRopeSegment_2.stretchingScale * 0.004f;
|
|
}
|
|
else
|
|
{
|
|
obiRopeSegment_2.stretchCompliance = 0f;
|
|
}
|
|
}
|
|
obiRopeSegment_1.stretchingScale = ObiLineOut;
|
|
HandleMaxDistanceLineConnector();
|
|
}
|
|
|
|
private void HandleMaxDistanceLineConnector()
|
|
{
|
|
if (FishEntity.CurrentFishInFight == null)
|
|
{
|
|
LineConnector_1.maxDistance = 0.1f + PhisicsLineOut;
|
|
}
|
|
else
|
|
{
|
|
LineConnector_1.maxDistance = 0.6f + PhisicsLineOut;
|
|
}
|
|
}
|
|
|
|
public void SetSegmentTwoLenght(float lenght)
|
|
{
|
|
LineConnector_2.maxDistance = lenght;
|
|
}
|
|
|
|
public void TryStretchLine(float force)
|
|
{
|
|
}
|
|
|
|
public void SetRopeLenght(float amount, FRod rod = null)
|
|
{
|
|
rod = rod ?? RefferenceHelper.GetCurrentRod();
|
|
float lenght = Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerSlotsEquip[rod.indexOfslot].line.lenght;
|
|
PhisicsLineOut += amount;
|
|
PhisicsLineOut = Mathf.Clamp(PhisicsLineOut, 0f, lenght);
|
|
}
|
|
|
|
public void ToggleLineVisibility(bool setEnable)
|
|
{
|
|
if ((bool)obiRopeSegment_1 && obiRopeSegment_1.TryGetComponent<Renderer>(out var component))
|
|
{
|
|
component.enabled = setEnable;
|
|
}
|
|
if ((bool)obiRopeSegment_2 && obiRopeSegment_2.TryGetComponent<Renderer>(out component))
|
|
{
|
|
component.enabled = setEnable;
|
|
}
|
|
if ((bool)obiRopeSegment_3 && obiRopeSegment_3.TryGetComponent<Renderer>(out component))
|
|
{
|
|
component.enabled = setEnable;
|
|
}
|
|
}
|
|
}
|