Sprint 4
This commit is contained in:
134
Assets/Minigames/Scripts/AbstractGameEndedPanel.cs
Normal file
134
Assets/Minigames/Scripts/AbstractGameEndedPanel.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
/// <summary>
|
||||
/// Abstract class for all minigame-gameEndedPanels
|
||||
/// </summary>
|
||||
public abstract class AbstractGameEndedPanel : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// The index of minigame that needs a GameEndedPanel
|
||||
/// </summary>
|
||||
protected abstract MinigameIndex minigameIndex
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the scoreboard entries container
|
||||
/// </summary>
|
||||
public Transform scoreboardEntriesContainer;
|
||||
|
||||
/// <summary>
|
||||
/// The GameObjects representing the letters
|
||||
/// </summary>
|
||||
private List<GameObject> scoreboardEntries = new List<GameObject>();
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the ScoreboardEntry prefab
|
||||
/// </summary>
|
||||
public GameObject scoreboardEntry;
|
||||
|
||||
/// <summary>
|
||||
/// Sets the scoreboard
|
||||
/// </summary>
|
||||
protected void SetScoreBoard()
|
||||
{
|
||||
// Clean the previous scoreboard entries
|
||||
for (int i = 0; i < scoreboardEntries.Count; i++)
|
||||
{
|
||||
Destroy(scoreboardEntries[i]);
|
||||
}
|
||||
scoreboardEntries.Clear();
|
||||
|
||||
// Instantiate new entries
|
||||
// Get all scores from all users
|
||||
List<Tuple<string, Score>> allScores = new List<Tuple<string, Score>>();
|
||||
foreach (User user in UserList.GetUsers())
|
||||
{
|
||||
// Get user's progress for this minigame
|
||||
var progress = user.GetMinigameProgress(minigameIndex);
|
||||
if (progress != null)
|
||||
{
|
||||
// Add scores to dictionary
|
||||
List<Score> scores = progress.highestScores;
|
||||
foreach (Score score in scores)
|
||||
{
|
||||
allScores.Add(new Tuple<string, Score>(user.GetUsername(), score));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort allScores based on Score.scoreValue
|
||||
allScores.Sort((a, b) => b.Item2.scoreValue.CompareTo(a.Item2.scoreValue));
|
||||
|
||||
// Instantiate scoreboard entries
|
||||
int rank = 1;
|
||||
foreach (Tuple<string, Score> tup in allScores.Take(10))
|
||||
{
|
||||
string username = tup.Item1;
|
||||
Score score = tup.Item2;
|
||||
|
||||
GameObject entry = Instantiate(scoreboardEntry, scoreboardEntriesContainer);
|
||||
scoreboardEntries.Add(entry);
|
||||
|
||||
// Set the player icon
|
||||
entry.transform.Find("Image").GetComponent<Image>().sprite = UserList.GetUserByUsername(username).GetAvatar();
|
||||
|
||||
// Set the player name
|
||||
entry.transform.Find("PlayerName").GetComponent<TMP_Text>().text = username;
|
||||
|
||||
// Set the score
|
||||
entry.transform.Find("Score").GetComponent<TMP_Text>().text = score.scoreValue.ToString();
|
||||
|
||||
// Set the rank
|
||||
entry.transform.Find("Rank").GetComponent<TMP_Text>().text = rank.ToString();
|
||||
|
||||
// Set the ago
|
||||
// Convert the score.time to Datetime
|
||||
DateTime time = DateTime.Parse(score.time);
|
||||
DateTime currentTime = DateTime.Now;
|
||||
TimeSpan diff = currentTime.Subtract(time);
|
||||
|
||||
string formatted;
|
||||
if (diff.Days > 0)
|
||||
{
|
||||
formatted = $"{diff.Days}d ";
|
||||
}
|
||||
else if (diff.Hours > 0)
|
||||
{
|
||||
formatted = $"{diff.Hours}h ";
|
||||
}
|
||||
else if (diff.Minutes > 0)
|
||||
{
|
||||
formatted = $"{diff.Minutes}m ";
|
||||
}
|
||||
else
|
||||
{
|
||||
formatted = "now";
|
||||
}
|
||||
|
||||
entry.transform.Find("Ago").GetComponent<TMP_Text>().text = formatted;
|
||||
|
||||
|
||||
// Alternating colors looks nice
|
||||
if (rank % 2 == 0)
|
||||
{
|
||||
Image image = entry.transform.GetComponent<Image>();
|
||||
image.color = new Color(image.color.r, image.color.g, image.color.b, 0f);
|
||||
}
|
||||
|
||||
// Make new score stand out
|
||||
if (diff.TotalSeconds < 1)
|
||||
{
|
||||
Image image = entry.transform.GetComponent<Image>();
|
||||
image.color = new Color(0, 229, 255, 233);
|
||||
}
|
||||
|
||||
rank++;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Minigames/Scripts/AbstractGameEndedPanel.cs.meta
Normal file
11
Assets/Minigames/Scripts/AbstractGameEndedPanel.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73d452eb6e118ec4091d6cdd82f3550c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
221
Assets/Minigames/Scripts/AbstractMinigameController.cs
Normal file
221
Assets/Minigames/Scripts/AbstractMinigameController.cs
Normal file
@@ -0,0 +1,221 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
/// <summary>
|
||||
/// Shared abstract class for the minigameControllers
|
||||
/// </summary>
|
||||
public abstract class AbstractMinigameController : AbstractFeedback
|
||||
{
|
||||
[Header("AbstractVariables")]
|
||||
/// <summary>
|
||||
/// We keep the minigamelist so that the minigame-index doesn't get reset
|
||||
/// DO NOT REMOVE
|
||||
/// </summary>
|
||||
public MinigameList minigamelist;
|
||||
|
||||
/// <summary>
|
||||
/// A bool to denote whether or not the game is still being played
|
||||
/// </summary>
|
||||
protected bool gameIsActive;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the progress bar
|
||||
/// </summary>
|
||||
public Slider feedbackProgress;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the current user
|
||||
/// </summary>
|
||||
private User user;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the minigame ScriptableObject
|
||||
/// </summary>
|
||||
protected Minigame minigame;
|
||||
|
||||
/// <summary>
|
||||
/// Each minigame has a webcamTexture, this will be used in children-methods
|
||||
/// </summary>
|
||||
public RawImage webcamScreen;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the gameEnded panel, so we can update its display
|
||||
/// </summary>
|
||||
public GameObject gameEndedPanel;
|
||||
|
||||
/// <summary>
|
||||
/// The theme that will be used by the signpredictor, this needs to be passed from the concrete class.
|
||||
/// This theme CAN be different from the theme that words are fetched from (Think SpellingBee and Hangman)
|
||||
/// </summary>
|
||||
protected abstract Theme signPredictorTheme
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start is called before the first frame update, seal it to prevent minigames from changing it
|
||||
/// </summary>
|
||||
protected void Start()
|
||||
{
|
||||
// Get the scriptable of the current minigame
|
||||
minigame = minigamelist.minigames[minigamelist.currentMinigameIndex];
|
||||
|
||||
// Start the game-specific start-logic
|
||||
StartController();
|
||||
|
||||
// Prepare the signPredictor
|
||||
signPredictor.SetModel(signPredictorTheme.modelIndex);
|
||||
signPredictor.SwapScreen(webcamScreen);
|
||||
signPredictor.SetSignsList(GetSignsList());
|
||||
AddSelfAsListener();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// All minigames use the same principle, they grab the most probable sign and use said sign to show feedback to the user
|
||||
/// Because we don't want minigames to write their own UpdateFeedbacks this function will be sealed
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="System.NotImplementedException"></exception>
|
||||
protected override sealed IEnumerator UpdateFeedback()
|
||||
{
|
||||
// Get the predicted sign
|
||||
if (signPredictor != null && signPredictor.learnableProbabilities != null && gameIsActive)
|
||||
{
|
||||
// Get highest predicted sign
|
||||
string predictedSign = signPredictor.learnableProbabilities.Aggregate((a, b) => a.Value > b.Value ? a : b).Key;
|
||||
float accuracy = signPredictor.learnableProbabilities[predictedSign];
|
||||
|
||||
// vvv TEMPORARY STUFF vvv
|
||||
if (predictedSign == "J" && accuracy <= 0.97f)
|
||||
{
|
||||
predictedSign = signPredictor.learnableProbabilities.Aggregate((x, y) => x.Value > y.Value && x.Key != "J" ? x : y).Key;
|
||||
}
|
||||
accuracy = signPredictor.learnableProbabilities[predictedSign];
|
||||
// ^^^ TEMPORARY STUFF ^^^
|
||||
|
||||
ProcessMostProbableSign(accuracy, predictedSign);
|
||||
}
|
||||
|
||||
// This part is the only reason that feedbackProgress is needed in the abstract
|
||||
else if (feedbackProgress != null)
|
||||
{
|
||||
|
||||
feedbackProgress.value = 0.0f;
|
||||
}
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Each game keeps a score, this score needs to be saved at some point
|
||||
/// </summary>
|
||||
public void SaveScores()
|
||||
{
|
||||
// Calculate new score
|
||||
int newScore = CalculateScore();
|
||||
|
||||
// Save the score as a tuple: < int score, string time ago>
|
||||
Score score = new Score();
|
||||
score.scoreValue = newScore;
|
||||
score.time = DateTime.Now.ToString();
|
||||
|
||||
// Save the new score
|
||||
var progress = user.GetMinigameProgress(minigame.index);
|
||||
|
||||
// Get the current list of scores
|
||||
List<Score> latestScores = progress.latestScores;
|
||||
List<Score> highestScores = progress.highestScores;
|
||||
|
||||
// Add the new score
|
||||
latestScores.Add(score);
|
||||
highestScores.Add(score);
|
||||
|
||||
// Sort the scores
|
||||
highestScores.Sort((a, b) => b.scoreValue.CompareTo(a.scoreValue));
|
||||
|
||||
// Only save the top 10 scores, so this list doesn't keep growing endlessly
|
||||
progress.latestScores = latestScores.Take(10).ToList();
|
||||
progress.highestScores = highestScores.Take(10).ToList();
|
||||
|
||||
PersistentDataController.GetInstance().Save();
|
||||
}
|
||||
/// <summary>
|
||||
/// The function that activates when the game ends, handles some endgame logic and displays the EndPanel
|
||||
/// </summary>
|
||||
/// <param name="won"></param>
|
||||
public void ActivateEnd(bool victory)
|
||||
{
|
||||
EndGameLogic(victory);
|
||||
SaveScores();
|
||||
SetScoreBoard(victory);
|
||||
|
||||
gameEndedPanel.SetActive(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Once the most probable sign has been fetched, they can be processed
|
||||
/// </summary>
|
||||
/// <param name="accuracy">The accuracy of the passed sign</param>
|
||||
/// <param name="predictedSign">The name of the passed sign</param>
|
||||
protected abstract void ProcessMostProbableSign(float accuracy, string predictedSign);
|
||||
|
||||
/// <summary>
|
||||
/// Each minigame has their own way of calculating their score
|
||||
/// </summary>
|
||||
/// <returns>The score that the user has at that point</returns>
|
||||
public abstract int CalculateScore();
|
||||
|
||||
/// <summary>
|
||||
/// Each minigame has an AbstractGameEndedPanel at the end, but they each have their own unique concrete instance
|
||||
/// </summary>
|
||||
/// <param name="victory">1 if the player won, 0 if they lost. Some games need this</param>
|
||||
protected abstract void SetScoreBoard(bool victory);
|
||||
|
||||
/// <summary>
|
||||
/// Each minigame puts their GameLogic to be called at (re)start in a seperate function from Start()
|
||||
/// </summary>
|
||||
public void StartController()
|
||||
{
|
||||
StartGameLogic();
|
||||
|
||||
// Create entry in current user for keeping track of progress
|
||||
user = UserList.GetCurrentUser();
|
||||
var progress = user.GetMinigameProgress(minigame.index);
|
||||
if (progress == null)
|
||||
{
|
||||
progress = new PersistentDataController.SavedMinigameProgress();
|
||||
progress.minigameIndex = minigame.index;
|
||||
user.AddMinigameProgress(progress);
|
||||
}
|
||||
UserList.Save();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logic to be called at the start of the game
|
||||
/// </summary>
|
||||
protected abstract void StartGameLogic();
|
||||
|
||||
/// <summary>
|
||||
/// Function that contains all the logic to end the game
|
||||
/// </summary>
|
||||
/// <param name="victory">1 if the player won, 0 if they lost. Some games need this</param>
|
||||
protected abstract void EndGameLogic(bool victory);
|
||||
|
||||
/// <summary>
|
||||
/// All non-fingerspelling-minigames have the same logic for the GetSignsList
|
||||
/// </summary>
|
||||
/// <returns>The signsList that needs to be passed to the signPredictor</returns>
|
||||
private List<string> GetSignsList()
|
||||
{
|
||||
List<string> signsList = new List<string>();
|
||||
foreach (Learnable learnable in signPredictorTheme.learnables)
|
||||
{
|
||||
signsList.Add(learnable.name);
|
||||
}
|
||||
|
||||
return signsList;
|
||||
}
|
||||
}
|
||||
11
Assets/Minigames/Scripts/AbstractMinigameController.cs.meta
Normal file
11
Assets/Minigames/Scripts/AbstractMinigameController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a32c0ecc5507e4542a79c1b96a47b0a7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
20
Assets/Minigames/Scripts/MinigameScripts.asmdef
Normal file
20
Assets/Minigames/Scripts/MinigameScripts.asmdef
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "MinigameScripts",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:d0b6b39a21908f94fbbd9f2c196a9725",
|
||||
"GUID:e83ddf9a537a96b4a804a16bb7872ec1",
|
||||
"GUID:1631ed2680c61245b8211d943c1639a8",
|
||||
"GUID:7f2d0ee6dd21e1d4eb25b71b7a749d25",
|
||||
"GUID:6055be8ebefd69e48b49212b09b47b2f"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
7
Assets/Minigames/Scripts/MinigameScripts.asmdef.meta
Normal file
7
Assets/Minigames/Scripts/MinigameScripts.asmdef.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 403dd94a93598934eb522dc36df43d7b
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user