2023-07-24 16:38:13 +03:00
|
|
|
using Dreamteck.Splines;
|
|
|
|
using PathCreation;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Events;
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
2023-08-15 17:38:54 +03:00
|
|
|
public class ShipPathFollower : MonoBehaviour, ITextChangable
|
2023-07-24 16:38:13 +03:00
|
|
|
{
|
|
|
|
public float speed = 10f;
|
|
|
|
public float minSpeed = 1f;
|
|
|
|
public float maxSpeed = 20f;
|
|
|
|
public float frictionForce = 0.1f;
|
|
|
|
public float gravityForce = 1f;
|
|
|
|
public float slopeRange = 60f;
|
2023-09-05 17:38:11 +03:00
|
|
|
|
|
|
|
public float cutsceneSpeed = 50f;
|
|
|
|
|
2023-08-01 11:28:14 +03:00
|
|
|
[SerializeField]
|
|
|
|
private SplineFollower follower;
|
2023-07-24 16:38:13 +03:00
|
|
|
public AnimationCurve speedGain;
|
|
|
|
public AnimationCurve speedLoss;
|
|
|
|
public float brakeSpeed = 0f;
|
|
|
|
public float brakeReleaseSpeed = 0f;
|
|
|
|
|
|
|
|
private float brakeTime = 0f;
|
|
|
|
private float brakeForce = 0f;
|
|
|
|
private float addForce = 0f;
|
|
|
|
|
|
|
|
public float soundFadeLength = 0.15f;
|
2023-08-15 17:38:54 +03:00
|
|
|
public UnityEvent<object> OnTextChange => _OnTextChange;
|
|
|
|
private UnityEvent<object> _OnTextChange = new UnityEvent<object>();
|
|
|
|
|
2023-09-04 12:48:24 +03:00
|
|
|
public UnityEvent<float> OnSpeedChange;
|
2023-10-13 14:13:44 +03:00
|
|
|
public UnityEvent<float> OnSpeedPercentChange;
|
2023-10-17 15:12:51 +03:00
|
|
|
public UnityEvent<float> OnBoostPercentChange;
|
2023-09-04 12:48:24 +03:00
|
|
|
|
2023-10-02 19:12:35 +03:00
|
|
|
public float SpeedPercent;
|
|
|
|
|
|
|
|
private float _boostSpeed;
|
2023-10-18 11:59:26 +03:00
|
|
|
[SerializeField]
|
|
|
|
private float _boostBreakSpeed;
|
|
|
|
private PlayerInputHandler playerInput;
|
2023-10-02 19:12:35 +03:00
|
|
|
|
|
|
|
public bool IsBot = false;
|
2023-07-24 16:38:13 +03:00
|
|
|
// Update is called once per frame
|
2023-10-18 11:59:26 +03:00
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
playerInput = GetComponent<PlayerInputHandler>();
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:38:13 +03:00
|
|
|
void Update()
|
|
|
|
{
|
2023-09-05 17:38:11 +03:00
|
|
|
if (_isCutsceneMovement)
|
|
|
|
return;
|
|
|
|
|
2023-10-18 11:59:26 +03:00
|
|
|
if (!IsBot && playerInput.Boost)
|
2023-07-24 16:38:13 +03:00
|
|
|
{
|
|
|
|
AddForce(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
float dot = Vector3.Dot(this.transform.forward, Vector3.down);
|
|
|
|
float dotPercent = Mathf.Lerp(-slopeRange / 90f, slopeRange / 90f, (dot + 1f) / 2f);
|
|
|
|
speed -= Time.deltaTime * frictionForce * (1f - brakeForce);
|
|
|
|
float speedAdd = 0f;
|
|
|
|
float speedPercent = Mathf.InverseLerp(minSpeed, maxSpeed, speed);
|
|
|
|
if (dotPercent > 0f)
|
|
|
|
{
|
|
|
|
speedAdd = gravityForce * dotPercent * speedGain.Evaluate(speedPercent) * Time.deltaTime;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
speedAdd = gravityForce * dotPercent * speedLoss.Evaluate(1f - speedPercent) * Time.deltaTime;
|
|
|
|
}
|
|
|
|
speed += speedAdd * (1f - brakeForce);
|
2023-09-05 17:38:11 +03:00
|
|
|
|
2023-10-02 19:12:35 +03:00
|
|
|
_boostSpeed -= Time.deltaTime * _boostBreakSpeed;
|
|
|
|
_boostSpeed = Mathf.Clamp(_boostSpeed, 0, float.MaxValue);
|
2023-09-05 17:38:11 +03:00
|
|
|
|
|
|
|
speed = Mathf.Clamp(speed, minSpeed, maxSpeed + _boostSpeed);
|
2023-07-24 16:38:13 +03:00
|
|
|
if (addForce > 0f)
|
|
|
|
{
|
|
|
|
float lastAdd = addForce;
|
|
|
|
addForce = Mathf.MoveTowards(addForce, 0f, Time.deltaTime * 30f);
|
|
|
|
speed += lastAdd - addForce;
|
|
|
|
}
|
|
|
|
follower.followSpeed = speed;
|
|
|
|
follower.followSpeed *= (1f - brakeForce);
|
2023-08-01 11:28:14 +03:00
|
|
|
|
|
|
|
if (brakeTime > Time.time)
|
|
|
|
brakeForce = Mathf.MoveTowards(brakeForce, 1f, Time.deltaTime * brakeSpeed);
|
|
|
|
else
|
|
|
|
brakeForce = Mathf.MoveTowards(brakeForce, 0f, Time.deltaTime * brakeReleaseSpeed);
|
2023-07-24 16:38:13 +03:00
|
|
|
|
2023-10-02 19:12:35 +03:00
|
|
|
SpeedPercent = Mathf.Clamp01(speed / maxSpeed) * (1f - brakeForce);
|
2023-10-13 14:13:44 +03:00
|
|
|
|
2023-09-04 12:48:24 +03:00
|
|
|
_OnTextChange?.Invoke((int)speed);
|
|
|
|
OnSpeedChange?.Invoke(speed);
|
2023-10-13 14:13:44 +03:00
|
|
|
OnSpeedPercentChange?.Invoke(SpeedPercent);
|
2023-07-24 16:38:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public void AddBrake(float time)
|
|
|
|
{
|
|
|
|
brakeTime = Time.time + time;
|
|
|
|
}
|
|
|
|
|
2023-08-01 11:28:14 +03:00
|
|
|
public void FastBreak(float multiplier)
|
|
|
|
{
|
|
|
|
speed *= multiplier;
|
|
|
|
}
|
|
|
|
|
2023-10-02 19:12:35 +03:00
|
|
|
public void FastBoost(float value)
|
2023-09-05 17:38:11 +03:00
|
|
|
{
|
2023-10-02 19:12:35 +03:00
|
|
|
_boostSpeed = value;
|
2023-09-05 17:38:11 +03:00
|
|
|
speed += _boostSpeed;
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:38:13 +03:00
|
|
|
public void RemoveBrake()
|
|
|
|
{
|
|
|
|
brakeTime = 0f;
|
|
|
|
}
|
|
|
|
|
2023-10-02 19:12:35 +03:00
|
|
|
public void ChangeMaxSpeed(float newSpeed)
|
|
|
|
{
|
|
|
|
maxSpeed = newSpeed;
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:38:13 +03:00
|
|
|
public void AddForce(float amount)
|
|
|
|
{
|
|
|
|
addForce = amount;
|
|
|
|
}
|
2023-09-05 17:38:11 +03:00
|
|
|
|
|
|
|
private bool _isCutsceneMovement = false;
|
|
|
|
|
|
|
|
public void CutsceneMovement()
|
|
|
|
{
|
|
|
|
_isCutsceneMovement = true;
|
|
|
|
speed = cutsceneSpeed;
|
|
|
|
}
|
2023-07-24 16:38:13 +03:00
|
|
|
}
|