222 lines
7.4 KiB
C#
222 lines
7.4 KiB
C#
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;
|
|
}
|
|
}
|