Untitled

 avatar
unknown
plain_text
2 months ago
3.7 kB
3
Indexable
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using DG.Tweening;

public class PlayerVehicle : MonoBehaviour
{
    [SerializeField] private DraggyRoad draggyRoad;
    [SerializeField] private Slider fuelSlider;
    [SerializeField] private Image fuelSliderFill;
    [SerializeField] private TextMeshProUGUI fuelText;
    [SerializeField] private float moveSpeed = 5f;
    [SerializeField] private float maxFuelCapacity = 100f;
    [SerializeField] private float fuelConsumptionRate = 1f;

    private float currentFuel;
    private GameObject currentTarget;
    private bool isMoving = false;
    private bool isBraking = false;

    private static readonly Color Green = Color.green;
    private static readonly Color Yellow = Color.yellow;
    private static readonly Color Red = Color.red;

    void Start()
    {
        currentFuel = maxFuelCapacity;
        fuelSlider.maxValue = maxFuelCapacity;
        fuelSlider.value = currentFuel;

        UpdateFuelUI(true);
        currentTarget = draggyRoad.GetNextSection();
    }

    void Update()
    {
        if (currentFuel <= 0f)
        {
            fuelText.text = "Empty!";
            currentFuel = 0f;
            isMoving = false;
            return;
        }

        if (!isBraking)
        {
            if (!isMoving)
            {
                if (currentTarget == null)
                {
                    currentTarget = draggyRoad.GetNextSection();
                }
                if (currentTarget != null)
                {
                    StartCoroutine(MoveToNextSection());
                }
            }
            currentFuel -= fuelConsumptionRate * Time.deltaTime;
            currentFuel = Mathf.Max(currentFuel, 0f);
            UpdateFuelUI();
        }
    }

    IEnumerator MoveToNextSection()
    {
        isMoving = true;

        if (currentTarget != null)
        {
            Vector3 targetPosition = currentTarget.transform.position;
            transform.right = targetPosition - transform.position;

            while (Vector3.Distance(transform.position, targetPosition) > 0.1f)
            {
                if (isBraking || currentFuel <= 0f)
                {
                    isMoving = false;
                    yield break;
                }

                transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);
                yield return null;
            }

            transform.position = targetPosition;
        }

        currentTarget = null;
        isMoving = false;
    }

    private void UpdateFuelUI(bool instant = false)
    {
        if (instant)
        {
            fuelSlider.value = currentFuel;
        }
        else
        {
            fuelSlider.DOValue(currentFuel, 0.5f);
        }

        float fuelPercentage = currentFuel / maxFuelCapacity * 100;
        fuelText.text = $"{Mathf.RoundToInt(fuelPercentage)}%";
        fuelSliderFill.color = GetFuelColor(fuelPercentage);
    }

    private Color GetFuelColor(float fuelPercentage)
    {
        if (fuelPercentage > 60f) return Green;
        if (fuelPercentage > 30f) return Yellow;
        return Red;
    }

    public void RefillFuel(float amount)
    {
        currentFuel = Mathf.Clamp(currentFuel + amount, 0f, maxFuelCapacity);
        Debug.Log($"Fuel Refilled. Current Fuel: {currentFuel}, Max Capacity: {maxFuelCapacity}");
        UpdateFuelUI(true);
    }

    public void HoldBrake()
    {
        isBraking = true;
    }

    public void ReleaseBrake()
    {
        isBraking = false;
    }
}
Leave a Comment