Global Game Jam 2021
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.7 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using TMPro;
  6. using Variables;
  7. public class StartScreenUI : MonoBehaviour
  8. {
  9. [SerializeField]
  10. private Reference<float> m_StartTime;
  11. [SerializeField]
  12. private TextMeshProUGUI m_countdownText;
  13. [SerializeField]
  14. private AnimationCurve m_lerpCurve;
  15. [SerializeField]
  16. private Transform m_startPosition;
  17. [SerializeField]
  18. private Transform m_endPosition;
  19. [SerializeField]
  20. private List<NumberAlias> m_aliases;
  21. private void OnEnable()
  22. {
  23. m_StartTime.OnValueChanged += UpdateCountDown;
  24. }
  25. private void OnDisable()
  26. {
  27. m_StartTime.OnValueChanged -= UpdateCountDown;
  28. }
  29. private void UpdateCountDown(float value)
  30. {
  31. int number = (int)value;
  32. if (m_aliases.Any(p => p.Number == number))
  33. {
  34. m_countdownText.text = m_aliases.First(p => p.Number == number).Name;
  35. }
  36. else
  37. {
  38. m_countdownText.text = number.ToString();
  39. }
  40. float ratio = m_lerpCurve.Evaluate(value % 1);
  41. m_countdownText.transform.position = Vector3.Lerp(m_endPosition.position, m_startPosition.position, ratio);
  42. }
  43. [ContextMenu("Do Count Down")]
  44. private void CountDown()
  45. {
  46. StartCoroutine(CountToZero(m_StartTime));
  47. }
  48. private IEnumerator CountToZero(float start)
  49. {
  50. m_StartTime.Value = start;
  51. while (m_StartTime > 0)
  52. {
  53. m_StartTime.Value -= Time.deltaTime;
  54. yield return new WaitForEndOfFrame();
  55. }
  56. }
  57. [System.Serializable]
  58. public struct NumberAlias
  59. {
  60. public int Number;
  61. public string Name;
  62. }
  63. }