390 lines
9.6 KiB
C#
390 lines
9.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BitStrap;
|
|
using UnityEngine;
|
|
|
|
public class BaitAnimation : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
public class MovingPart
|
|
{
|
|
public Rigidbody rigidbody;
|
|
|
|
[ReadOnly]
|
|
public ConfigurableJoint configurableJoint;
|
|
|
|
public Vector3 startPosition = Vector3.zero;
|
|
|
|
public Vector3 startRotation = Vector3.zero;
|
|
}
|
|
|
|
[Serializable]
|
|
public class AnimPart
|
|
{
|
|
public bool update = true;
|
|
|
|
[Space(10f)]
|
|
public Transform animationParent;
|
|
|
|
public float maxRotation = -1f;
|
|
|
|
public Vector2 animationMinMaxSpeed = new Vector2(300f, 2000f);
|
|
|
|
[ReadOnly]
|
|
public float animationTargetSpeed;
|
|
|
|
[ReadOnly]
|
|
public float animationCurrentSpeed;
|
|
|
|
[ReadOnly]
|
|
public Vector3 startEulerAngles = Vector3.zero;
|
|
|
|
public Vector3 animationDir = Vector3.zero;
|
|
|
|
public float idleMaxRotation = -1f;
|
|
|
|
public float idleSpeed;
|
|
|
|
public bool resetInIdle;
|
|
|
|
public bool fixRotation;
|
|
}
|
|
|
|
[Space(10f)]
|
|
public List<MovingPart> movingParts = new List<MovingPart>();
|
|
|
|
[Space(10f)]
|
|
public List<AnimPart> animParts = new List<AnimPart>();
|
|
|
|
[ReadOnly]
|
|
public Transform baitAnimationParent;
|
|
|
|
public Vector3 baitAnimationParentStartPos = Vector3.zero;
|
|
|
|
public Vector3 baitAnimationParentStartRot = Vector3.zero;
|
|
|
|
[Space(10f)]
|
|
public float resetSpeed = 360f;
|
|
|
|
public bool useTargetXRotation;
|
|
|
|
public float targetXrotation;
|
|
|
|
public bool useTargetYRotation;
|
|
|
|
public float targetYrotation;
|
|
|
|
public bool useTargetZRotation;
|
|
|
|
public float targetZrotation;
|
|
|
|
[Space(10f)]
|
|
public bool useIdleXRotation;
|
|
|
|
public float idleXrotation;
|
|
|
|
public bool useIdleYRotation;
|
|
|
|
public float idleYrotation;
|
|
|
|
public bool useIdleZRotation;
|
|
|
|
public float idleZrotation;
|
|
|
|
public AnimationCurve animationCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
|
|
|
|
[Space(10f)]
|
|
public Vector2 forceStrength = Vector2.zero;
|
|
|
|
public Vector2 forceDelay = Vector2.zero;
|
|
|
|
public Vector3 forceDirection = Vector3.zero;
|
|
|
|
[ReadOnly]
|
|
public float forceDir = 1f;
|
|
|
|
[ReadOnly]
|
|
public Coroutine forceCoroutine;
|
|
|
|
[HideInInspector]
|
|
public Bait bait;
|
|
|
|
private void Awake()
|
|
{
|
|
bait = GetComponent<Bait>();
|
|
for (int i = 0; i < animParts.Count; i++)
|
|
{
|
|
if (!animParts[i].animationParent)
|
|
{
|
|
return;
|
|
}
|
|
animParts[i].startEulerAngles = animParts[i].animationParent.transform.localEulerAngles;
|
|
}
|
|
for (int j = 0; j < movingParts.Count; j++)
|
|
{
|
|
movingParts[j].configurableJoint = movingParts[j].rigidbody.GetComponent<ConfigurableJoint>();
|
|
}
|
|
for (int k = 0; k < animParts.Count; k++)
|
|
{
|
|
if (animParts[k].animationParent.GetComponent<Rigidbody>() != null)
|
|
{
|
|
baitAnimationParent = animParts[k].animationParent.transform;
|
|
baitAnimationParentStartPos = baitAnimationParent.localPosition;
|
|
baitAnimationParentStartRot = baitAnimationParent.localEulerAngles;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UpdateAnimation()
|
|
{
|
|
if (Time.timeScale == 0f || animParts.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
float num = Vector3.Distance(bait.prevPosition, bait.transform.position);
|
|
num *= 7f;
|
|
bool flag = UtilitiesInput.isReelingIn || ((bool)bait.fishingHands.fishingPlayer.boatSimulator && bait.fishingHands.fishingPlayer.boatSimulator.keepMovingForward);
|
|
for (int i = 0; i < animParts.Count; i++)
|
|
{
|
|
AnimPart animPart = animParts[i];
|
|
if (animPart.animationParent == null || !animPart.update)
|
|
{
|
|
continue;
|
|
}
|
|
float num2 = 0f;
|
|
if (!bait.isOnWater)
|
|
{
|
|
num2 = 0f;
|
|
}
|
|
else if (!flag)
|
|
{
|
|
num2 = ((!(num > 0.01f)) ? 0f : (-1f));
|
|
}
|
|
else if (num > 0.01f)
|
|
{
|
|
num2 = Mathf.InverseLerp(0f, 0.1f, num);
|
|
}
|
|
if (num2 != 0f || !flag)
|
|
{
|
|
SetAnimationSpeed(animPart, num2);
|
|
}
|
|
animPart.animationCurrentSpeed = Mathf.MoveTowards(animPart.animationCurrentSpeed, animPart.animationTargetSpeed, ((animPart.animationTargetSpeed != 0f) ? 2000f : 900f) * Time.deltaTime);
|
|
float num3 = ((!flag) ? animPart.idleMaxRotation : animPart.maxRotation);
|
|
num3 = animPart.maxRotation;
|
|
if (animPart.maxRotation > 0f)
|
|
{
|
|
Vector3 vectorAngles180Range = Utilities.GetVectorAngles180Range(animPart.animationParent.localEulerAngles);
|
|
if (animPart.animationDir.x != 0f)
|
|
{
|
|
if (flag && animPart.idleMaxRotation > 0f)
|
|
{
|
|
vectorAngles180Range.x = 0f;
|
|
animPart.animationDir.x = 1f;
|
|
}
|
|
vectorAngles180Range.x += animPart.animationDir.x * animPart.animationCurrentSpeed * Time.deltaTime;
|
|
if ((animPart.animationDir.x > 0f && vectorAngles180Range.x >= num3) || (animPart.animationDir.x < 0f && vectorAngles180Range.x <= 0f - num3))
|
|
{
|
|
vectorAngles180Range.x = Utilities.PingPongValue(vectorAngles180Range.x, 0f - num3, num3);
|
|
animPart.animationDir.x *= -1f;
|
|
}
|
|
if (!flag)
|
|
{
|
|
}
|
|
if (animPart.resetInIdle && !flag)
|
|
{
|
|
vectorAngles180Range.x = Mathf.MoveTowards(vectorAngles180Range.x, 0f, resetSpeed * Time.deltaTime);
|
|
}
|
|
}
|
|
else if (animPart.fixRotation)
|
|
{
|
|
vectorAngles180Range.x = animPart.startEulerAngles.x;
|
|
}
|
|
if (animPart.animationDir.y != 0f)
|
|
{
|
|
if (flag && animPart.idleMaxRotation > 0f)
|
|
{
|
|
vectorAngles180Range.y = 0f;
|
|
animPart.animationDir.y = 1f;
|
|
}
|
|
vectorAngles180Range.y += animPart.animationDir.y * animPart.animationCurrentSpeed * Time.deltaTime;
|
|
if ((animPart.animationDir.y > 0f && vectorAngles180Range.y >= num3) || (animPart.animationDir.y < 0f && vectorAngles180Range.y <= 0f - num3))
|
|
{
|
|
vectorAngles180Range.y = Utilities.PingPongValue(vectorAngles180Range.y, 0f - num3, num3);
|
|
animPart.animationDir.y *= -1f;
|
|
}
|
|
if (!flag)
|
|
{
|
|
}
|
|
if (animPart.resetInIdle && !flag)
|
|
{
|
|
vectorAngles180Range.y = Mathf.MoveTowards(vectorAngles180Range.y, 0f, resetSpeed * Time.deltaTime);
|
|
}
|
|
}
|
|
else if (animPart.fixRotation)
|
|
{
|
|
vectorAngles180Range.y = animPart.startEulerAngles.y;
|
|
}
|
|
if (animPart.animationDir.z != 0f)
|
|
{
|
|
if (flag && animPart.idleMaxRotation > 0f)
|
|
{
|
|
vectorAngles180Range.z = 0f;
|
|
animPart.animationDir.z = 1f;
|
|
}
|
|
vectorAngles180Range.z += animPart.animationDir.z * animPart.animationCurrentSpeed * Time.deltaTime;
|
|
if ((animPart.animationDir.z > 0f && vectorAngles180Range.z >= num3) || (animPart.animationDir.z < 0f && vectorAngles180Range.z <= 0f - num3))
|
|
{
|
|
vectorAngles180Range.z = Utilities.PingPongValue(vectorAngles180Range.z, 0f - num3, num3);
|
|
animPart.animationDir.z *= -1f;
|
|
}
|
|
if (!flag)
|
|
{
|
|
}
|
|
if (animPart.resetInIdle && !flag)
|
|
{
|
|
vectorAngles180Range.z = Mathf.MoveTowards(vectorAngles180Range.z, 0f, resetSpeed * Time.deltaTime);
|
|
}
|
|
}
|
|
else if (animPart.fixRotation)
|
|
{
|
|
vectorAngles180Range.z = animPart.startEulerAngles.z;
|
|
}
|
|
animPart.animationParent.localEulerAngles = vectorAngles180Range;
|
|
}
|
|
else
|
|
{
|
|
animPart.animationParent.localEulerAngles += animPart.animationDir * animPart.animationCurrentSpeed * Time.deltaTime;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetAnimationSpeed(AnimPart animPart, float speed)
|
|
{
|
|
if (speed > 0f)
|
|
{
|
|
animPart.animationTargetSpeed = Mathf.Lerp(animPart.animationMinMaxSpeed.x, animPart.animationMinMaxSpeed.y, speed);
|
|
}
|
|
else if (speed < 0f)
|
|
{
|
|
animPart.animationTargetSpeed = animPart.idleSpeed;
|
|
}
|
|
else
|
|
{
|
|
animPart.animationTargetSpeed = 0f;
|
|
}
|
|
}
|
|
|
|
public void StopAnimation()
|
|
{
|
|
for (int i = 0; i < animParts.Count; i++)
|
|
{
|
|
animParts[i].animationCurrentSpeed = (animParts[i].animationTargetSpeed = 0f);
|
|
}
|
|
}
|
|
|
|
public void ResetAnimation()
|
|
{
|
|
for (int i = 0; i < animParts.Count; i++)
|
|
{
|
|
Vector3 vectorAngles180Range = Utilities.GetVectorAngles180Range(animParts[i].animationParent.localEulerAngles);
|
|
if (animParts[i].animationDir.x != 0f)
|
|
{
|
|
vectorAngles180Range.x = 0f;
|
|
}
|
|
if (animParts[i].animationDir.y != 0f)
|
|
{
|
|
vectorAngles180Range.y = 0f;
|
|
}
|
|
if (animParts[i].animationDir.z != 0f)
|
|
{
|
|
vectorAngles180Range.z = 0f;
|
|
}
|
|
animParts[i].animationParent.localEulerAngles = vectorAngles180Range;
|
|
}
|
|
}
|
|
|
|
public void ResetMovingParts(bool freeze)
|
|
{
|
|
for (int i = 0; i < movingParts.Count; i++)
|
|
{
|
|
movingParts[i].rigidbody.isKinematic = freeze;
|
|
movingParts[i].rigidbody.transform.localEulerAngles = movingParts[i].startRotation;
|
|
}
|
|
}
|
|
|
|
public void SetMovingPartsPhysics(bool idle)
|
|
{
|
|
if (movingParts.Count != 0)
|
|
{
|
|
JointDrive jointDrive = new JointDrive
|
|
{
|
|
positionSpring = ((!idle) ? 0.5f : 0f),
|
|
positionDamper = 0f,
|
|
maximumForce = float.MaxValue
|
|
};
|
|
for (int i = 0; i < movingParts.Count; i++)
|
|
{
|
|
movingParts[i].configurableJoint.angularXDrive = jointDrive;
|
|
movingParts[i].configurableJoint.angularYZDrive = jointDrive;
|
|
movingParts[i].rigidbody.drag = ((!idle) ? 0.5f : 0.5f);
|
|
}
|
|
if (idle)
|
|
{
|
|
FixHooks();
|
|
}
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void FixHooks()
|
|
{
|
|
for (int i = 0; i < movingParts.Count; i++)
|
|
{
|
|
if ((bool)movingParts[i].configurableJoint)
|
|
{
|
|
movingParts[i].configurableJoint.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
LeanTween.delayedCall(0.1f, (Action)delegate
|
|
{
|
|
for (int j = 0; j < movingParts.Count; j++)
|
|
{
|
|
if ((bool)movingParts[j].configurableJoint)
|
|
{
|
|
movingParts[j].configurableJoint.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public IEnumerator SwimForce()
|
|
{
|
|
while (true)
|
|
{
|
|
yield return new WaitForSeconds(Mathf.Lerp(forceDelay.x, forceDelay.y, 1f - bait.fishingHands.currentUserReelSpeed));
|
|
forceDir *= -1f;
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void AddSwimForce()
|
|
{
|
|
if (forceStrength.y > 0f)
|
|
{
|
|
bait.rigidbody.AddForce(base.transform.right * forceStrength.y * forceDir);
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void StartSwimForceCoroutine()
|
|
{
|
|
if (forceCoroutine != null)
|
|
{
|
|
StopCoroutine(forceCoroutine);
|
|
}
|
|
forceCoroutine = StartCoroutine(SwimForce());
|
|
}
|
|
}
|