using TMPro;
using UnityEngine;//этот сприкт сделан для того что бы начислять деньги после полёта, и забирать их
using UnityEngine.UI;
using System.Collections;
public class MoneyFlight : MonoBehaviour
{
private int pointsForFlight; //очки начесляються во время полёта
[SerializeField] private int requiredPoints;
[SerializeField] FlightManager flightManager;
[SerializeField] Slider slider;
public int PointsForFlight => pointsForFlight;//get для pointsForFlight
public int RequiredPoints => requiredPoints; //get для RequiredPoints
[SerializeField] TMP_Text text;//переменная для текста в которой будет количество очков
private void OnTriggerEnter(Collider collision)
{
if (collision.GetComponent<SpikeScript>() != null && flightManager.StartFlight)//првоеряет наличе сскрипта спайк скрипт на обьекте
{
Destroy(collision.gameObject);//уничтожение обьекта
PointsCounter();//запускает метод подсчёта очков
}
else if(collision.GetComponentInChildren<SpikeScript>() != null && flightManager.StartFlight)//проверяет наличие скрипта у дочернего обьекта
{
Destroy(collision.gameObject.transform.parent.gameObject);//уничтожение обьекта
PointsCounter();//запускает метод подсчёта очков
}
}
public void ResetPoint()
{
pointsForFlight = 0;
slider.value = 0;
StopAllCoroutines();
}
void PointsCounter()//подсчёт очков
{
pointsForFlight++;//прибавление очков
UpdateText();//обнавление текста
StartCoroutine(StepChangeCoroutine(pointsForFlight, 0.2f, 0.07f));
if (requiredPoints <= pointsForFlight)
{
flightManager.FlightEnding(true);
}
}
public void UpdateText()
{
if (text != null)//проверка закинули ли мы в переменую текст
{
text.text = "Points: " + pointsForFlight;//заполняет текст
}
}
IEnumerator StepChangeCoroutine(float targetValue, float stepSize, float delay)
{
float current = slider.value;
float direction = Mathf.Sign(targetValue - current);
while (Mathf.Abs(slider.value - targetValue) > 0.01f)
{
float newValue = slider.value + direction * stepSize;
// Ограничиваем, чтобы не перескочить целевое значение
if (direction > 0)
newValue = Mathf.Min(newValue, targetValue);
else
newValue = Mathf.Max(newValue, targetValue);
slider.value = newValue;
Debug.Log(slider.value);
yield return new WaitForSeconds(delay);
}
// Финальная установка для точности
slider.value = targetValue;
}
}