120 lines
3.3 KiB
C#
120 lines
3.3 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Threading.Tasks;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
|
||
public class DriftScore : MonoBehaviour
|
||
{
|
||
[SerializeField] private Rigidbody playerRB;
|
||
[SerializeField] private TMP_Text totalScoreText;
|
||
[SerializeField] private TMP_Text currentScoreText;
|
||
[SerializeField] private TMP_Text factorText;
|
||
[SerializeField] private TMP_Text driftAngleText;
|
||
|
||
private float speed = 0;
|
||
private float driftAngle = 0;
|
||
private float driftFactor = 1;
|
||
private float currentScore;
|
||
private float totalScore;
|
||
|
||
private bool isDrifting = false;
|
||
|
||
[SerializeField] private float minimumSpeed = 5;
|
||
[SerializeField] private float minimumAngle = 10;
|
||
[SerializeField] private float driftingDelay = 0.2f;
|
||
[SerializeField] private GameObject driftingObject;
|
||
[SerializeField] private Color normalDriftColor;
|
||
[SerializeField] private Color nearStopColor;
|
||
[SerializeField] private Color driftEndedColor;
|
||
|
||
private IEnumerator stopDriftingCoroutine = null;
|
||
|
||
private void Start()
|
||
{
|
||
driftingObject.SetActive(false);
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
ManagerDrift();
|
||
ManageUI();
|
||
}
|
||
void ManagerDrift()
|
||
{
|
||
speed = playerRB.velocity.magnitude;
|
||
driftAngle = Vector3.Angle(playerRB.transform.forward, (playerRB.velocity + playerRB.transform.forward).normalized);
|
||
if (driftAngle > 120)
|
||
{
|
||
driftAngle = 0;
|
||
}
|
||
if (driftAngle >= minimumAngle && speed > minimumSpeed)
|
||
{
|
||
if (!isDrifting || stopDriftingCoroutine != null)
|
||
{
|
||
StartDrift();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (isDrifting && stopDriftingCoroutine == null)
|
||
{
|
||
StopDrift();
|
||
}
|
||
}
|
||
if (isDrifting)
|
||
{
|
||
currentScore += Time.deltaTime * driftAngle * driftFactor;
|
||
driftFactor += Time.deltaTime;
|
||
driftingObject.SetActive(true);
|
||
}
|
||
}
|
||
|
||
async void StartDrift()
|
||
{
|
||
if (!isDrifting)
|
||
{
|
||
await Task.Delay(Mathf.RoundToInt(1000 * driftingDelay));
|
||
driftFactor = 1;
|
||
}
|
||
if (stopDriftingCoroutine != null)
|
||
{
|
||
StopCoroutine(stopDriftingCoroutine);
|
||
stopDriftingCoroutine = null;
|
||
}
|
||
currentScoreText.color = normalDriftColor;
|
||
isDrifting = true;
|
||
|
||
}
|
||
void StopDrift()
|
||
{
|
||
stopDriftingCoroutine = StoppingDrift();
|
||
StartCoroutine(stopDriftingCoroutine);
|
||
|
||
}
|
||
private IEnumerator StoppingDrift()
|
||
{
|
||
yield return new WaitForSeconds(0.1f);
|
||
currentScoreText.color = Color.yellow;
|
||
|
||
yield return new WaitForSeconds(driftingDelay * 4f);
|
||
totalScore += currentScore;
|
||
isDrifting = false;
|
||
currentScoreText.color = Color.red;
|
||
|
||
yield return new WaitForSeconds(0.5f);
|
||
currentScore = 0;
|
||
driftingObject.SetActive(false);
|
||
|
||
}
|
||
|
||
void ManageUI()
|
||
{
|
||
totalScoreText.text = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: " + (totalScore).ToString("###,###,000");
|
||
factorText.text = driftFactor.ToString("###,###,##0.0") + "X";
|
||
currentScoreText.text = currentScore.ToString("###,###,000");
|
||
driftAngleText.text = driftAngle.ToString("###,##0");
|
||
}
|
||
|
||
}
|