using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class SpeedDisplay : MonoBehaviour
{
    [Header("Local Player")]
    [SerializeField] RacerCore m_localPlayer;

    [Header("Speed Display")]
    [SerializeField] TextMeshProUGUI m_speedDisplay;

    // Start is called before the first frame update
    void Start()
    {
        if (!m_localPlayer)
            StartCoroutine(TryGetPlayer());
    }

    IEnumerator TryGetPlayer()
    {
        while (!m_localPlayer)
        {
            m_localPlayer = RacerCore.LocalRacer;

            yield return null;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (!m_localPlayer || !m_speedDisplay)
            return;

        m_speedDisplay.text = Mathf.FloorToInt(m_localPlayer.RigidBD.velocity.magnitude) + " MPH";

        float _speed = m_localPlayer.RigidBD.velocity.magnitude;

        if (_speed > 35)
            transform.localRotation = Quaternion.identity * Quaternion.Euler(0, 0, Random.Range(35 - _speed, -35 + _speed) / 5);
        else
            transform.localRotation = Quaternion.identity;
    }
}
