146 lines
4.3 KiB
C#
146 lines
4.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// Manager infopage for the Minigames
|
|
/// </summary>
|
|
public class MinigameActivityScreen : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// Reference to the Minigames
|
|
/// </summary>
|
|
public MinigameList minigameList;
|
|
|
|
/// <summary>
|
|
/// Title Display
|
|
/// </summary>
|
|
public TMP_Text title;
|
|
|
|
/// <summary>
|
|
/// Description Display
|
|
/// </summary>
|
|
public TMP_Text description;
|
|
|
|
/// <summary>
|
|
/// Image Display (Thumbnail)
|
|
/// </summary>
|
|
public Image gameImage;
|
|
|
|
/// <summary>
|
|
/// PlayButton to get to the Minigame
|
|
/// </summary>
|
|
public Button button;
|
|
|
|
/// <summary>
|
|
/// Control explanation for the Display
|
|
/// </summary>
|
|
public TMP_Text controls;
|
|
|
|
/// <summary>
|
|
/// Reference to the Minigame progress
|
|
/// </summary>
|
|
private PersistentDataController.SavedMinigameProgress progress;
|
|
|
|
/// <summary>
|
|
/// Reference to the users-high-scores container object
|
|
/// </summary>
|
|
public Transform userContainer;
|
|
|
|
/// <summary>
|
|
/// Prefab for the high-score-entries
|
|
/// </summary>
|
|
public GameObject prefab;
|
|
|
|
/// <summary>
|
|
/// Reference to the ranking title
|
|
/// </summary>
|
|
public GameObject rankingTitle;
|
|
|
|
/// <summary>
|
|
/// Sets the infopage for a given minigame
|
|
/// </summary>
|
|
void Start()
|
|
{
|
|
PersistentDataController.GetInstance().Load();
|
|
GenerateContent();
|
|
GenerateHighScores();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates all content except for the HighScores
|
|
/// </summary>
|
|
private void GenerateContent()
|
|
{
|
|
// Get current minigame
|
|
int index = minigameList.currentMinigameIndex;
|
|
Minigame minigame = minigameList.minigames[index];
|
|
|
|
// Set main screen
|
|
title.text = minigame.title;
|
|
description.text = minigame.description;
|
|
gameImage.sprite = minigame.thumbnail;
|
|
controls.text = minigame.controls;
|
|
|
|
// Add click
|
|
if (minigame.themeList != null)
|
|
{
|
|
button.onClick.AddListener(() => SystemController.GetInstance().LoadNextScene("Common/Scenes/ThemeSelectionScreen"));
|
|
}
|
|
else
|
|
{
|
|
button.onClick.AddListener(() => SystemController.GetInstance().LoadNextScene(minigame.minigameEntryPoint));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the High Scores (Top 3)
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// TODO: Maybe write a getter for the sorted scores as this is just a copy of the logic implemented in SpellingBee/.../GameController
|
|
/// </remarks>
|
|
private void GenerateHighScores()
|
|
{
|
|
// Get current minigame
|
|
int index = minigameList.currentMinigameIndex;
|
|
Minigame minigame = minigameList.minigames[index];
|
|
|
|
List<Tuple<string, Sprite, Score>> allScores = new List<Tuple<string, Sprite, Score>>();
|
|
foreach (User user in UserList.GetUsers())
|
|
{
|
|
// Get user's progress for this minigame
|
|
progress = user.GetMinigameProgress(minigame.index);
|
|
if (progress != null)
|
|
{
|
|
// Add scores to dictionary
|
|
List<Score> scores = progress.highestScores;
|
|
foreach (Score score in scores)
|
|
{
|
|
allScores.Add(new Tuple<string, Sprite, Score>(user.GetUsername(), user.GetAvatar(), score));
|
|
}
|
|
}
|
|
}
|
|
|
|
rankingTitle.SetActive(allScores.Count > 0);
|
|
|
|
// Sort allScores based on Score.scoreValue
|
|
allScores.Sort((a, b) => b.Item3.scoreValue.CompareTo(a.Item3.scoreValue));
|
|
|
|
// Instantiate scoreboard entries
|
|
foreach (Tuple<string, Sprite, Score> tup in allScores.Take(3))
|
|
{
|
|
string username = tup.Item1;
|
|
Sprite sprite = tup.Item2;
|
|
Score score = tup.Item3;
|
|
|
|
GameObject instance = GameObject.Instantiate(prefab, userContainer);
|
|
instance.transform.Find("Title").GetComponent<TMP_Text>().text = username;
|
|
instance.transform.Find("Avatar").GetComponent<Image>().sprite = sprite;
|
|
instance.transform.Find("Score").GetComponent<TMP_Text>().text = score.scoreValue.ToString();
|
|
}
|
|
}
|
|
}
|