Resolve WES-132-Common-Abstract-Minigame

This commit is contained in:
Jerome Coudron
2023-04-23 21:20:12 +00:00
committed by Jelle De Geest
parent 4fdb8f95cb
commit 966475455a
30 changed files with 1046 additions and 1078 deletions

View File

@@ -7,7 +7,7 @@ using TMPro;
using UnityEngine;
using UnityEngine.UI;
public partial class SpellingBeeController : AbstractFeedback
public partial class SpellingBeeController : AbstractMinigameController
{
/// <summary>
/// All of the words that can be used in this session
@@ -45,11 +45,6 @@ public partial class SpellingBeeController : AbstractFeedback
/// </summary>
private float timerValue;
/// <summary>
/// Indicates if the game is still going
/// </summary>
private bool gameEnded;
/// <summary>
/// List of learnables to get the threshold for the letters
/// </summary>
@@ -81,22 +76,6 @@ public partial class SpellingBeeController : AbstractFeedback
/// </summary>
private DateTime startTime;
/// <summary>
/// Reference to the current user
/// </summary>
private User user;
/// <summary>
/// Reference to the minigame ScriptableObject
/// </summary>
public Minigame minigame;
/// <summary>
/// We keep the minigamelist as well so that the minigame-index doesn't get reset
/// DO NOT REMOVE
/// </summary>
public MinigameList minigamelist;
/// <summary>
/// Letter prefab
/// </summary>
@@ -137,21 +116,11 @@ public partial class SpellingBeeController : AbstractFeedback
/// </summary>
public Transform Scoreboard;
/// <summary>
/// Reference to the gameEnded panel, so we can update its display
/// </summary>
public GameObject gameEndedPanel;
/// <summary>
/// Reference to the feedback field
/// </summary>
public TMP_Text feedbackText;
/// <summary>
/// Reference to the progress bar
/// </summary>
public Slider feedbackProgress;
/// <summary>
/// Reference to the progress bar image, so we can add fancy colors
/// </summary>
@@ -172,11 +141,6 @@ public partial class SpellingBeeController : AbstractFeedback
/// </summary>
protected string previousIncorrectSign = null;
/// <summary>
/// Reference used to set the webcam for the SignPredictor
/// </summary>
public RawImage webcamScreen;
/// <summary>
/// Reference to display the score
/// </summary>
@@ -198,57 +162,11 @@ public partial class SpellingBeeController : AbstractFeedback
private int incorrectLettersScore = -5;
/// <summary>
/// Start is called before the first frame update
/// Set the AbstractMinigameController variable to inform it of the theme for the signPredictor
/// </summary>
public void Start()
protected override Theme signPredictorTheme
{
signPredictor.SetModel(ModelIndex.FINGERSPELLING);
signPredictor.SwapScreen(webcamScreen);
AddSelfAsListener();
StartController();
}
/// <summary>
/// Is called at the start of the scene AND when the game is replayed
/// </summary>
public void StartController()
{
correctLetters = 0;
incorrectLetters = 0;
words.Clear();
// We use -1 instead of 0 so SetNextWord can simply increment it each time
spelledWords = -1;
wordIndex = 0;
gameEnded = false;
timerValue = 30.0f;
bonusActiveRemaining = 0.0f;
startTime = DateTime.Now;
gameEndedPanel.SetActive(false);
bonusTimeText.SetActive(false);
// 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 = MinigameIndex.SPELLING_BEE;
user.AddMinigameProgress(progress);
}
UserList.Save();
currentTheme = minigame.themeList.themes[minigame.themeList.currentThemeIndex];
//feedback.signPredictor.SetModel(currentTheme.modelIndex);
words.AddRange(currentTheme.learnables);
ShuffleWords();
NextWord();
scoreDisplay.text = $"Score: {CalculateScore()}";
scoreBonus.text = "";
get { return fingerspelling; }
}
/// <summary>
@@ -256,7 +174,7 @@ public partial class SpellingBeeController : AbstractFeedback
/// </summary>
public void Update()
{
if (!gameEnded)
if (gameIsActive)
{
timerValue -= Time.deltaTime;
if (bonusActiveRemaining <= 0.0 && bonusTimeText.activeSelf)
@@ -272,7 +190,8 @@ public partial class SpellingBeeController : AbstractFeedback
if (timerValue <= 0.0f)
{
timerValue = 0.0f;
ActivateGameOver();
//ActivateGameOver();
ActivateEnd(false);
}
int minutes = Mathf.FloorToInt(timerValue / 60.0f);
@@ -302,89 +221,11 @@ public partial class SpellingBeeController : AbstractFeedback
/// Calculate the score
/// </summary>
/// <returns>The calculated score</returns>
public int CalculateScore()
public override int CalculateScore()
{
return correctLetters * correctLettersScore + incorrectLetters * incorrectLettersScore;
}
/// <summary>
/// Displays the game over panel and score values
/// </summary>
public void ActivateGameOver()
{
gameEnded = true;
DeleteWord();
// Save the scores and show the scoreboard
SaveScores();
gameEndedPanel.GetComponent<GameEndedPanel>().GenerateContent(
startTime: startTime,
totalWords: spelledWords,
correctLetters: correctLetters,
incorrectLetters: incorrectLetters,
result: "VERLOREN",
score: CalculateScore()
);
gameEndedPanel.SetActive(true);
}
/// <summary>
/// Display win screen
/// </summary>
public void ActivateWin()
{
gameEnded = true;
DeleteWord();
// Save the scores and show the scoreboard
SaveScores();
gameEndedPanel.GetComponent<GameEndedPanel>().GenerateContent(
startTime: startTime,
totalWords: spelledWords,
correctLetters: correctLetters,
incorrectLetters: incorrectLetters,
result: "GEWONNEN",
score: CalculateScore()
);
gameEndedPanel.SetActive(true);
}
/// <summary>
/// Update and save the scores
/// </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();
UserList.Save();
}
/// <summary>
/// Delete all letter objects
/// </summary>
@@ -414,7 +255,7 @@ public partial class SpellingBeeController : AbstractFeedback
/// <param name="successful">true if the letter was correctly signed, false otherwise</param>
public void NextLetter(bool successful)
{
if (gameEnded) { return; }
if (!gameIsActive) { return; }
// Change color of current letter (skip spaces)
if (successful)
@@ -469,7 +310,8 @@ public partial class SpellingBeeController : AbstractFeedback
}
else
{
ActivateWin();
//ActivateWin();
ActivateEnd(true);
}
}
@@ -514,7 +356,7 @@ public partial class SpellingBeeController : AbstractFeedback
Learnable letter = fingerspelling.learnables.Find(l => l.name == sign);
return letter.thresholdPercentage;
}
/*
/// <summary>
/// The updateFunction that is called when new probabilities become available
/// </summary>
@@ -525,7 +367,7 @@ public partial class SpellingBeeController : AbstractFeedback
string currentSign = GetSign();
// Get the predicted sign
if (signPredictor != null && signPredictor.learnableProbabilities != null &&
currentSign != null && signPredictor.learnableProbabilities.ContainsKey(currentSign))
currentSign != null && signPredictor.learnableProbabilities.ContainsKey(currentSign) && gameIsActive)
{
float accCurrentSign = signPredictor.learnableProbabilities[currentSign];
float thresholdCurrentSign = GetTresholdPercentage(currentSign);
@@ -613,6 +455,7 @@ public partial class SpellingBeeController : AbstractFeedback
}
yield return null;
}
*/
/// <summary>
/// Function to get the current letter that needs to be signed
/// </summary>
@@ -638,4 +481,157 @@ public partial class SpellingBeeController : AbstractFeedback
}
NextLetter(successful);
}
/// <summary>
/// The logic to process the signs sent by the signPredictor
/// </summary>
/// <param name="accuracy">The accuracy of the passed sign</param>
/// <param name="predictedSign">The name of the passed sign</param>
protected override void ProcessMostProbableSign(float accuracy, string predictedSign)
{
string currentSign = GetSign();
float accPredictSign = accuracy;
float accCurrentSign = signPredictor.learnableProbabilities[currentSign];
float thresholdCurrentSign = GetTresholdPercentage(currentSign);
float thresholdPredictedSign = GetTresholdPercentage(predictedSign);
// If there is a feedback-object, we wil change its appearance
if (feedbackText != null && feedbackProgressImage != null)
{
Color col;
if (accPredictSign > thresholdCurrentSign)
{
feedbackText.text = "Goed";
col = new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f);
}
else if (accCurrentSign > 0.9 * thresholdCurrentSign)
{
feedbackText.text = "Bijna...";
col = new Color(0xf2 / 255.0f, 0x7f / 255.0f, 0x0c / 255.0f);
}
else if (accPredictSign > thresholdPredictedSign)
{
feedbackText.text = $"Verkeerde gebaar: '{predictedSign}'";
col = new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f);
accCurrentSign = 0.0f;
}
else
{
feedbackText.text = "Detecteren...";
col = new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f);
}
feedbackText.color = col;
feedbackProgressImage.color = col;
float oldValue = feedbackProgress.value;
// use an exponential scale
float newValue = Mathf.Exp(4 * (Mathf.Clamp(accCurrentSign / thresholdCurrentSign, 0.0f, 1.0f) - 1.0f));
feedbackProgress.gameObject.Tween("FeedbackUpdate", oldValue, newValue, 0.2f, TweenScaleFunctions.CubicEaseInOut, (t) =>
{
if (feedbackProgress != null)
{
feedbackProgress.value = t.CurrentValue;
}
});
}
// The logic for the internal workings of the game
if (accPredictSign > thresholdPredictedSign)
{
// Correct sign, instantly pass it along
if (predictedSign == currentSign)
{
PredictSign(predictedSign);
timer = DateTime.Now;
predictedSign = null;
previousIncorrectSign = null;
}
// Incorrect sign, wait a bit before passing it along
else
{
if (previousIncorrectSign != predictedSign)
{
timer = DateTime.Now;
previousIncorrectSign = predictedSign;
}
else if (DateTime.Now - timer > TimeSpan.FromSeconds(2.0f))
{
PredictSign(predictedSign);
timer = DateTime.Now;
predictedSign = null;
previousIncorrectSign = null;
}
}
}
}
/// <summary>
/// The logic to set the scoreboard of spellingbee
/// </summary>
/// <param name="victory">SHows whether or not the player won</param>
protected override void SetScoreBoard(bool victory)
{
string resultTxt;
if (victory)
{
resultTxt = "GEWONNEN";
}
else
{
resultTxt = "VERLOREN";
}
// Save the scores and show the scoreboard
gameEndedPanel.GetComponent<SpellingBeeGameEndedPanel>().GenerateContent(
startTime: startTime,
totalWords: spelledWords,
correctLetters: correctLetters,
incorrectLetters: incorrectLetters,
result: resultTxt,
score: CalculateScore()
);
}
/// <summary>
/// The spellinbee-specific logic that needs to be called at the start of the game
/// </summary>
protected override void StartGameLogic()
{
correctLetters = 0;
incorrectLetters = 0;
words.Clear();
// We use -1 instead of 0 so SetNextWord can simply increment it each time
spelledWords = -1;
wordIndex = 0;
gameIsActive = true;
timerValue = 30.0f;
bonusActiveRemaining = 0.0f;
startTime = DateTime.Now;
gameEndedPanel.SetActive(false);
bonusTimeText.SetActive(false);
currentTheme = minigame.themeList.themes[minigame.themeList.currentThemeIndex];
//feedback.signPredictor.SetModel(currentTheme.modelIndex);
words.AddRange(currentTheme.learnables);
ShuffleWords();
NextWord();
scoreDisplay.text = $"Score: {CalculateScore()}";
scoreBonus.text = "";
}
/// <summary>
/// The spellingbee-specific logic that needs to be called at the end of a game
/// </summary>
/// <param name="victory"></param>
protected override void EndGameLogic(bool victory)
{
gameIsActive = false;
DeleteWord();
}
}