Files
unity-application/Assets/Accounts/Scripts/UserProgressScreen.cs
2023-04-30 15:51:41 +00:00

190 lines
6.0 KiB
C#

using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// UserProgressScreen scene manager
/// </summary>
public class UserProgressScreen : MonoBehaviour
{
/// <summary>
/// Reference to the current user
/// </summary>
private User user;
/// <summary>
/// UI reference to the username
/// </summary>
public TMP_Text username;
/// <summary>
/// UI reference to the user's avatar
/// </summary>
public Image avatar;
/// <summary>
/// UI reference to the user total playtime
/// </summary>
public TMP_Text playtime;
/// <summary>
/// Prefab of the highscore marker to display on the graph
/// </summary>
public GameObject highscoreMarker;
/// <summary>
/// Prefab of a course card
/// </summary>
public GameObject courseCardPrefab;
/// <summary>
/// UI reference to the container holding all course cards
/// </summary>
public GameObject coursesContainer;
/// <summary>
/// UI reference to the message that displays when no course progress is present
/// </summary>
public GameObject emptyCourses;
/// <summary>
/// Prefab of a minigame card
/// </summary>
public GameObject minigameCardPrefab;
/// <summary>
/// UI reference to the container holding all the minigame cards
/// </summary>
public GameObject minigamesContainer;
/// <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 RawImage progressGraph;
/// <summary>
/// Current selected activity draw to the graph
/// </summary>
private int selectedActivity = -1;
/// <summary>
/// List of activity backgrounds and indices
/// </summary>
private List<Tuple<Image, int>> activities = new List<Tuple<Image, int>>();
/// <summary>
/// Start is called before the first frame update
/// </summary>
void Start()
{
// Assign the current user
PersistentDataController.GetInstance().Load();
user = UserList.GetCurrentUser();
// Set correct displayed items
username.text = user.GetUsername();
avatar.sprite = user.GetAvatar();
playtime.text = $"Totale speeltijd: {user.GetPlaytime().ToString("0.00")}";
// Set graph inactive
progressGraph.gameObject.SetActive(false);
int i = 0;
// Display courses
var courses = user.GetCourses();
coursesContainer.SetActive(courses.Count > 0);
emptyCourses.SetActive(courses.Count <= 0);
foreach (var courseProgress in courses)
{
// Create instance of prefab
GameObject instance = GameObject.Instantiate(courseCardPrefab, coursesContainer.transform.Find("Viewport").Find("Content").transform);
int j = i++;
// Initialize card
CourseProgressCard cpc = instance.GetComponent<CourseProgressCard>();
cpc.courseProgress = courseProgress;
cpc.selectActivity = () => UpdateSelection(j);
// Store reference to background so we can apply fancy coloring
Image background = instance.GetComponent<Image>();
background.color = Color.gray;
activities.Add(Tuple.Create(background, (int)courseProgress.courseIndex));
}
// Display minigames
var minigames = user.GetMinigames();
minigamesContainer.SetActive(minigames.Count > 0);
emptyMinigames.SetActive(minigames.Count <= 0);
foreach (var minigameProgress in minigames)
{
// Create instance of prefab
GameObject instance = GameObject.Instantiate(minigameCardPrefab, minigamesContainer.transform.Find("Viewport").Find("Content").transform);
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;
activities.Add(Tuple.Create(background, (int)minigameProgress.minigameIndex));
}
}
/// <summary>
/// Update the current selected activity
/// </summary>
/// <param name="newActivity">Index to the new activity</param>
private void UpdateSelection(int newActivity)
{
if (selectedActivity < 0)
{
progressGraph.gameObject.SetActive(true);
}
else
{
activities[selectedActivity].Item1.color = Color.gray;
}
selectedActivity = newActivity;
activities[selectedActivity].Item1.color = Color.blue;
if (selectedActivity < user.GetCourses().Count)
{
// TODO: create a better graph
//DisplayCourseGraph((CourseIndex)activities[selectedActivity].Item2);
// For now: just deactivate graph rendering
progressGraph.gameObject.SetActive(false);
}
else
{
DisplayMinigameGraph((MinigameIndex)activities[selectedActivity].Item2);
// TODO: remove line, this is only because courses deactivates the graph
progressGraph.gameObject.SetActive(true);
}
}
/// <summary>
/// Plot the graph of a course
/// </summary>
/// <param name="index">Index of the course</param>
/// <remarks>TODO: create a better plot</remarks>
private void DisplayCourseGraph(CourseIndex index) { }
/// <summary>
/// Plot the graph of a minigame
/// </summary>
/// <param name="minigameIndex">Index of the minigame</param>
/// <remarks>TODO: reworking </remarks>
private void DisplayMinigameGraph(MinigameIndex minigameIndex) { }
}