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.

81 lines
1.9 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. [RequireComponent(typeof(TextMeshProUGUI))]
  6. public class RoundsLeft : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private GameModeReference reference;
  10. [SerializeField]
  11. private float DisplayTime;
  12. [SerializeField]
  13. private AnimationCurve AnimationCurve;
  14. private ColorGameMode gameMode;
  15. private TextMeshProUGUI Text;
  16. private void Awake()
  17. {
  18. Text = GetComponent<TextMeshProUGUI>();
  19. gameMode = reference.Value as ColorGameMode;
  20. Text.enabled = false;
  21. }
  22. [ContextMenu("Do Animation")]
  23. public void StartAnimation()
  24. {
  25. UpdateText();
  26. Vector3 startPosition = new Vector3(Screen.width * 2, 0, 0);
  27. Vector3 endPosition = new Vector3(-Screen.width * 2, 0 , 0);
  28. StartCoroutine(AnimationRoutine(startPosition, endPosition));
  29. }
  30. private void UpdateText()
  31. {
  32. if (gameMode == null) {
  33. Text.text = "New Round";
  34. return;
  35. }
  36. int RoundsLeft = gameMode.MaxRound - gameMode.RoundCount;
  37. if (RoundsLeft == 1)
  38. Text.text = "Last Round";
  39. else
  40. Text.text = RoundsLeft + " Rounds Left";
  41. }
  42. public IEnumerator AnimationRoutine(Vector3 startPos, Vector3 endPos)
  43. {
  44. RectTransform rt = transform as RectTransform;
  45. Text.enabled = true;
  46. float ratio;
  47. float elapsedTime = 0;
  48. while (elapsedTime < DisplayTime)
  49. {
  50. ratio = AnimationCurve.Evaluate(elapsedTime/DisplayTime);
  51. rt.anchoredPosition = Vector3.Lerp(startPos, endPos, ratio);
  52. yield return new WaitForEndOfFrame();
  53. elapsedTime += Time.deltaTime;
  54. }
  55. ratio = AnimationCurve.Evaluate(1);
  56. rt.anchoredPosition = Vector3.Lerp(startPos, endPos, ratio);
  57. Text.enabled = false;
  58. }
  59. }