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

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class CreditUIController : MonoBehaviour
{
[SerializeField]
private PlayerList m_allPlayers;
[SerializeField]
private GameObject m_playerPrefab;
[SerializeField]
private RectTransform winnerPos;
[SerializeField]
private RectTransform losePos;
[SerializeField]
private float waitTime = 1;
[SerializeField]
private float creditDisplayTime = 1;
[SerializeField]
public List<CreditReferences> m_CreditReferences;
[SerializeField]
public List<CreditData> m_Credits;
private int Count;
private void Start()
{
StartCoroutine(DisplaySlow(waitTime));
StartCoroutine(DisplayCredits(creditDisplayTime));
}
[ContextMenu("display All players")]
private void DisplayAllPlayers()
{
int max = m_allPlayers.AllPlayers.Select(p => p.Score).Max();
int min = m_allPlayers.AllPlayers.Select(p => p.Score).Min();
foreach (var data in m_allPlayers.AllPlayers)
{
DisplayScore(data, max, min);
}
}
private IEnumerator DisplaySlow(float timebetween = 1)
{
int max = m_allPlayers.AllPlayers.Select(p => p.Score).Max();
int min = m_allPlayers.AllPlayers.Select(p => p.Score).Min();
foreach (var data in m_allPlayers.AllPlayers.OrderBy(p => p.Score))
{
yield return new WaitForSeconds(timebetween);
DisplayScore(data, max, min);
}
}
private IEnumerator DisplayCredits(float displayTime)
{
float totalTime = displayTime / m_CreditReferences.Count;
while (true)
{
CreditReferences reference = m_CreditReferences[Count%m_CreditReferences.Count];
CreditData data = m_Credits[Count % m_Credits.Count];
Count++;
reference.Title.text = data.Title;
reference.Credit.text = data.Credit;
yield return new WaitForSeconds(totalTime);
}
}
private void DisplayScore(PlayerData player, int maxScore, int minScore)
{
player.Input.SwitchCurrentActionMap("JoinMenu");
float ratio = 0.5f;
if (maxScore != minScore)
ratio = (float)(player.Score - minScore) / (maxScore - minScore);
Vector3 pos = Vector3.Lerp(losePos.position, winnerPos.position, ratio);
pos.x = Random.Range((Screen.width / 4f), Screen.width);
GameObject newPlayer = Instantiate(m_playerPrefab);
newPlayer.transform.parent = (m_playerPrefab.transform.parent);
newPlayer.transform.position = pos;
newPlayer.transform.Rotate(Vector3.forward, Random.Range(-20.0f, 20.0f));
if (Random.Range(0, 2) > 0)
{
newPlayer.transform.localScale.Scale(new Vector3(-1, 1, 1));
newPlayer.GetComponentInChildren<TMPro.TextMeshProUGUI>().transform.localScale.Scale(new Vector3(-1, 1, 1));
}
newPlayer.GetComponent<PlayerJoinIcon>().Initialise(player);
newPlayer.GetComponentInChildren<TMPro.TextMeshProUGUI>().text = player.Score.ToString();
newPlayer.SetActive(true);
}
[System.Serializable]
public struct CreditReferences
{
public TMPro.TextMeshProUGUI Title;
public TMPro.TextMeshProUGUI Credit;
}
[System.Serializable]
public struct CreditData
{
public string Title;
public string Credit;
}
}