Files
unity-application/Assets/Accounts/Scripts/PanelMinigameProgress.cs
2023-05-04 09:09:49 +00:00

128 lines
3.9 KiB
C#

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