using System; using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; using UnityEngine.UI; /// /// Class to handle minigame list progress display /// public class PanelMinigameProgress : MonoBehaviour { /// /// Reference to the current user /// private User user; /// /// Reference to the minigames list /// public MinigameList minigameList; /// /// Prefab of a minigame card /// public GameObject minigameCardPrefab; /// /// UI reference to the container holding all the minigame cards /// public Transform minigamesContainer; /// /// UI reference to the minigame info panel /// public GameObject minigameInfo; /// /// UI reference to the message that displays when no minigame progress is present /// public GameObject emptyMinigames; /// /// UI reference to the plot /// public ProgressGraph progressGraph; /// /// Reference to the title of the minigame of the info panel /// public TMP_Text minigameTitle; /// /// Reference to the text that will display when an empty minigame progress object is selected /// public GameObject emptyHighscore; /// /// Current selected course /// private int selectedMinigame = 0; /// /// List of course backgrounds and indices /// private List> minigameCards = new List>(); /// /// Start is called before the first frame update /// void Start() { PersistentDataController.GetInstance().Load(); user = UserList.GetCurrentUser(); var minigames = user.GetMinigames(); minigameInfo.SetActive(minigames.Count > 0); emptyMinigames.SetActive(minigames.Count <= 0); int i = 0; foreach (var minigameProgress in minigames) { // Create instance of prefab GameObject instance = GameObject.Instantiate(minigameCardPrefab, minigamesContainer); int j = i++; // Initialize card MinigameProgressCard mpc = instance.GetComponent(); mpc.minigameProgress = minigameProgress; mpc.selectActivity = () => UpdateSelection(j); // Store reference to background so we can apply fancy coloring Image background = instance.GetComponent(); background.color = Color.gray; minigameCards.Add(Tuple.Create(background, minigameProgress.minigameIndex)); } if (0 < minigames.Count) UpdateSelection(0); } /// /// Update the current selected course /// /// Index to the new course private void UpdateSelection(int newMinigame) { minigameCards[selectedMinigame].Item1.color = Color.gray; selectedMinigame = newMinigame; minigameCards[selectedMinigame].Item1.color = Color.blue; var progress = user.GetMinigameProgress(minigameCards[selectedMinigame].Item2); minigameTitle.text = minigameList.GetMinigameByIndex(progress.minigameIndex).title; List latestScores = progress.latestScores; List highestScores = progress.highestScores; if (0 < highestScores.Count) { emptyHighscore.SetActive(false); progressGraph.gameObject.SetActive(true); progressGraph.Plot(latestScores.ConvertAll((s) => (double)s.scoreValue), highestScores.Max((s) => s.scoreValue)); } else { emptyHighscore.SetActive(true); progressGraph.gameObject.SetActive(false); } } }