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.

142 lines
3.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
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. [SerializeField,NaughtyAttributes.Scene]
  24. public string m_MainMenuScene;
  25. private int Count;
  26. private void Start()
  27. {
  28. StartCoroutine(DisplaySlow(waitTime));
  29. StartCoroutine(DisplayCredits(creditDisplayTime));
  30. }
  31. [ContextMenu("display All players")]
  32. private void DisplayAllPlayers()
  33. {
  34. int max = m_allPlayers.AllPlayers.Select(p => p.Score).Max();
  35. int min = m_allPlayers.AllPlayers.Select(p => p.Score).Min();
  36. foreach (var data in m_allPlayers.AllPlayers)
  37. {
  38. DisplayScore(data, max, min);
  39. }
  40. }
  41. private IEnumerator DisplaySlow(float timebetween = 1)
  42. {
  43. int max = m_allPlayers.AllPlayers.Select(p => p.Score).Max();
  44. int min = m_allPlayers.AllPlayers.Select(p => p.Score).Min();
  45. foreach (var data in m_allPlayers.AllPlayers.OrderBy(p => p.Score))
  46. {
  47. yield return new WaitForSeconds(timebetween);
  48. DisplayScore(data, max, min);
  49. }
  50. }
  51. private IEnumerator DisplayCredits(float displayTime)
  52. {
  53. float totalTime = displayTime / m_CreditReferences.Count;
  54. while (true)
  55. {
  56. CreditReferences reference = m_CreditReferences[Count%m_CreditReferences.Count];
  57. CreditData data = m_Credits[Count % m_Credits.Count];
  58. Count++;
  59. reference.Title.text = data.Title;
  60. reference.Credit.text = data.Credit;
  61. yield return new WaitForSeconds(totalTime);
  62. }
  63. }
  64. private void DisplayScore(PlayerData player, int maxScore, int minScore)
  65. {
  66. player.Input.SwitchCurrentActionMap("JoinMenu");
  67. float ratio = 0.5f;
  68. if (maxScore != minScore)
  69. ratio = (float)(player.Score - minScore) / (maxScore - minScore);
  70. Vector3 pos = Vector3.Lerp(losePos.position, winnerPos.position, ratio);
  71. pos.x = Random.Range((Screen.width / 4f), Screen.width);
  72. GameObject newPlayer = Instantiate(m_playerPrefab);
  73. newPlayer.transform.parent = (m_playerPrefab.transform.parent);
  74. newPlayer.transform.position = pos;
  75. newPlayer.transform.Rotate(Vector3.forward, Random.Range(-20.0f, 20.0f));
  76. if (Random.Range(0, 2) > 0)
  77. {
  78. newPlayer.transform.localScale.Scale(new Vector3(-1, 1, 1));
  79. newPlayer.GetComponentInChildren<TMPro.TextMeshProUGUI>().transform.localScale.Scale(new Vector3(-1, 1, 1));
  80. }
  81. newPlayer.GetComponent<PlayerJoinIcon>().Initialise(player);
  82. newPlayer.GetComponentInChildren<TMPro.TextMeshProUGUI>().text = player.Score.ToString();
  83. newPlayer.SetActive(true);
  84. }
  85. public void OnMenuClick()
  86. {
  87. PlayerManager.Clear();
  88. UnityEngine.SceneManagement.SceneManager.LoadScene(m_MainMenuScene);
  89. }
  90. [System.Serializable]
  91. public struct CreditReferences
  92. {
  93. public TMPro.TextMeshProUGUI Title;
  94. public TMPro.TextMeshProUGUI Credit;
  95. }
  96. [System.Serializable]
  97. public struct CreditData
  98. {
  99. public string Title;
  100. public string Credit;
  101. }
  102. }