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.

133 lines
3.4 KiB

3 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. public class CreditUIController : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private PlayerList m_allPlayers;
  9. [SerializeField]
  10. private GameObject m_playerPrefab;
  11. [SerializeField]
  12. private RectTransform winnerPos;
  13. [SerializeField]
  14. private RectTransform losePos;
  15. [SerializeField]
  16. private float waitTime = 1;
  17. [SerializeField]
  18. private float creditDisplayTime = 1;
  19. [SerializeField]
  20. public List<CreditReferences> m_CreditReferences;
  21. [SerializeField]
  22. public List<CreditData> m_Credits;
  23. private int Count;
  24. private void Start()
  25. {
  26. StartCoroutine(DisplaySlow(waitTime));
  27. StartCoroutine(DisplayCredits(creditDisplayTime));
  28. }
  29. [ContextMenu("display All players")]
  30. private void DisplayAllPlayers()
  31. {
  32. int max = m_allPlayers.AllPlayers.Select(p => p.Score).Max();
  33. int min = m_allPlayers.AllPlayers.Select(p => p.Score).Min();
  34. foreach (var data in m_allPlayers.AllPlayers)
  35. {
  36. DisplayScore(data, max, min);
  37. }
  38. }
  39. private IEnumerator DisplaySlow(float timebetween = 1)
  40. {
  41. int max = m_allPlayers.AllPlayers.Select(p => p.Score).Max();
  42. int min = m_allPlayers.AllPlayers.Select(p => p.Score).Min();
  43. foreach (var data in m_allPlayers.AllPlayers.OrderBy(p => p.Score))
  44. {
  45. yield return new WaitForSeconds(timebetween);
  46. DisplayScore(data, max, min);
  47. }
  48. }
  49. private IEnumerator DisplayCredits(float displayTime)
  50. {
  51. float totalTime = displayTime / m_CreditReferences.Count;
  52. while (true)
  53. {
  54. CreditReferences reference = m_CreditReferences[Count%m_CreditReferences.Count];
  55. CreditData data = m_Credits[Count % m_Credits.Count];
  56. Count++;
  57. reference.Title.text = data.Title;
  58. reference.Credit.text = data.Credit;
  59. yield return new WaitForSeconds(totalTime);
  60. }
  61. }
  62. private void DisplayScore(PlayerData player, int maxScore, int minScore)
  63. {
  64. player.Input.SwitchCurrentActionMap("JoinMenu");
  65. float ratio = 0.5f;
  66. if (maxScore != minScore)
  67. ratio = (float)(player.Score - minScore) / (maxScore - minScore);
  68. Vector3 pos = Vector3.Lerp(losePos.position, winnerPos.position, ratio);
  69. pos.x = Random.Range((Screen.width / 4f), Screen.width);
  70. GameObject newPlayer = Instantiate(m_playerPrefab);
  71. newPlayer.transform.parent = (m_playerPrefab.transform.parent);
  72. newPlayer.transform.position = pos;
  73. newPlayer.transform.Rotate(Vector3.forward, Random.Range(-20.0f, 20.0f));
  74. if (Random.Range(0, 2) > 0)
  75. {
  76. newPlayer.transform.localScale.Scale(new Vector3(-1, 1, 1));
  77. newPlayer.GetComponentInChildren<TMPro.TextMeshProUGUI>().transform.localScale.Scale(new Vector3(-1, 1, 1));
  78. }
  79. newPlayer.GetComponent<PlayerJoinIcon>().Initialise(player);
  80. newPlayer.GetComponentInChildren<TMPro.TextMeshProUGUI>().text = player.Score.ToString();
  81. newPlayer.SetActive(true);
  82. }
  83. [System.Serializable]
  84. public struct CreditReferences
  85. {
  86. public TMPro.TextMeshProUGUI Title;
  87. public TMPro.TextMeshProUGUI Credit;
  88. }
  89. [System.Serializable]
  90. public struct CreditData
  91. {
  92. public string Title;
  93. public string Credit;
  94. }
  95. }