521 lines
16 KiB
C#
521 lines
16 KiB
C#
using DigitalRuby.Tween;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Random = UnityEngine.Random;
|
|
|
|
/// <summary>
|
|
/// Contains all game logic for the JustSign game
|
|
/// </summary>
|
|
public class JustSignController : AbstractMinigameController
|
|
{
|
|
/// <summary>
|
|
/// All of the words that can be used in this session
|
|
/// </summary>
|
|
private List<Learnable> words = new List<Learnable>();
|
|
|
|
/// <summary>
|
|
/// The input field where the user can type his or her answer
|
|
/// </summary>
|
|
public TMP_InputField answerField;
|
|
|
|
/// <summary>
|
|
/// The feedback on the timing
|
|
/// </summary>
|
|
public TMP_Text timingFeedback;
|
|
|
|
/// <summary>
|
|
/// The current score
|
|
/// </summary>
|
|
public TMP_Text scoreDisplay;
|
|
|
|
/// </summary>
|
|
/// Reference to the list of available songs
|
|
/// </summary>
|
|
public SongList songList;
|
|
|
|
/// <summary>
|
|
/// Reference to the currently used song
|
|
/// </summary>
|
|
private Song currentSong;
|
|
|
|
/// <summary>
|
|
/// Reference to the perfect hitzone
|
|
/// </summary>
|
|
public RectTransform hitZonePerfect;
|
|
|
|
/// <summary>
|
|
/// Reference to the good hitzone
|
|
/// </summary>
|
|
public RectTransform hitZoneGood;
|
|
|
|
/// <summary>
|
|
/// Reference to the meh hitzone
|
|
/// </summary>
|
|
public RectTransform hitZoneMeh;
|
|
|
|
/// <summary>
|
|
/// Score obtained when getting a perfect hit
|
|
/// </summary>
|
|
private int perfectScore = 50;
|
|
|
|
/// <summary>
|
|
/// Score obtained when getting a good hit
|
|
/// </summary>
|
|
private int goodScore = 20;
|
|
|
|
/// <summary>
|
|
/// Score obtained when getting a meh hit
|
|
/// </summary>
|
|
private int mehScore = 10;
|
|
|
|
/// <summary>
|
|
/// Score obtained when getting a terrible hit
|
|
/// </summary>
|
|
private int terribleScore = -3;
|
|
|
|
/// <summary>
|
|
/// Score obtained when symbol goes offscreen
|
|
/// </summary>
|
|
private int offscreenScore = -5;
|
|
|
|
/// <summary>
|
|
/// Symbol prefab
|
|
/// </summary>
|
|
public GameObject symbolPrefab;
|
|
|
|
/// <summary>
|
|
/// Reference to symbol prefab
|
|
/// </summary>
|
|
public Transform symbolContainer;
|
|
|
|
/// <summary>
|
|
/// The theme we are currently using
|
|
/// </summary>
|
|
private Theme currentTheme;
|
|
|
|
/// <summary>
|
|
/// List of strings representing all words on the track
|
|
/// </summary>
|
|
private List<string> activeWords = new List<string>();
|
|
|
|
/// <summary>
|
|
/// List of objects representing all symbols on the track
|
|
/// </summary>
|
|
private List<GameObject> activeSymbols = new List<GameObject>();
|
|
|
|
/// <summary>
|
|
/// Controls movement speed of symbols (higher -> faster)
|
|
/// </summary>
|
|
private int moveSpeed = 100;
|
|
|
|
/// <summary>
|
|
/// Starting X-coordinate of a symbol = (-1920 - symbolsize) / 2
|
|
/// </summary>
|
|
private int trackX = 1920 / 2;
|
|
|
|
/// <summary>
|
|
/// Starting Y-coordinate of a symbol
|
|
/// </summary>
|
|
private int trackY = 0;
|
|
|
|
/// <summary>
|
|
/// Time at which the last symbol was spawned
|
|
/// </summary>
|
|
private float lastSpawn;
|
|
|
|
/// <summary>
|
|
/// Time at which the game started, needed to know when to stop
|
|
/// </summary>
|
|
private float beginTime;
|
|
|
|
/// <summary>
|
|
/// Time at which the last symbol should spawn
|
|
/// </summary>
|
|
private float lastSymbolTime;
|
|
|
|
/// <summary>
|
|
/// Counter that keeps track of how many signs get you score "perfect"
|
|
/// </summary>
|
|
private int perfectSigns;
|
|
|
|
/// <summary>
|
|
/// Counter that keeps track of how many signs get you score "good"
|
|
/// </summary>
|
|
private int goodSigns;
|
|
|
|
/// <summary>
|
|
/// Counter that keeps track of how many signs get you score "meh"
|
|
/// </summary>
|
|
private int mehSigns;
|
|
|
|
/// <summary>
|
|
/// Counter that keeps track of how many signs get you score "perfect"
|
|
/// </summary>
|
|
private int terribleSigns;
|
|
|
|
/// <summary>
|
|
/// Counter that keeps track of how many signs done incorrectly
|
|
/// </summary>
|
|
private int incorrectSigns;
|
|
|
|
/// <summary>
|
|
/// LPM
|
|
/// </summary>
|
|
public TMP_Text lpmText;
|
|
|
|
/// <summary>
|
|
/// Perfect Signs Score
|
|
/// </summary>
|
|
public TMP_Text perfectSignsText;
|
|
|
|
/// <summary>
|
|
/// Good Signs Score
|
|
/// </summary>
|
|
public TMP_Text goodSignsText;
|
|
|
|
/// <summary>
|
|
/// Meh Signs Score
|
|
/// </summary>
|
|
public TMP_Text mehSignsText;
|
|
|
|
/// <summary>
|
|
/// Perfect Signs Score
|
|
/// </summary>
|
|
public TMP_Text terribleSignsText;
|
|
|
|
/// <summary>
|
|
/// Signs that were not found
|
|
/// </summary>
|
|
public TMP_Text notFoundSignsText;
|
|
|
|
/// <summary>
|
|
/// Score
|
|
/// </summary>
|
|
public TMP_Text scoreText;
|
|
|
|
/// <summary>
|
|
/// Reference to the feedback field
|
|
/// </summary>
|
|
public TMP_Text feedbackText;
|
|
|
|
/// <summary>
|
|
/// Reference to the progress bar image, so we can add fancy colors
|
|
/// </summary>
|
|
public Image feedbackProgressImage;
|
|
|
|
/// <summary>
|
|
/// Sprite shown when perfect score
|
|
/// </summary>
|
|
public Sprite perfectSprite;
|
|
|
|
/// <summary>
|
|
/// Sprite shown when good score
|
|
/// </summary>
|
|
public Sprite goodSprite;
|
|
|
|
/// <summary>
|
|
/// Sprite shown when meh score
|
|
/// </summary>
|
|
public Sprite mehSprite;
|
|
|
|
/// <summary>
|
|
/// Sprite shown when terrible score (too soon)
|
|
/// </summary>
|
|
public Sprite terribleSprite;
|
|
|
|
/// <summary>
|
|
/// Sprite shown when sign leaves screen
|
|
/// </summary>
|
|
public Sprite tooLateSprite;
|
|
|
|
/// <summary>
|
|
/// Reference to display the feedback image
|
|
/// </summary>
|
|
public Image imageFeedback;
|
|
|
|
/// <summary>
|
|
/// Message to display when there is no model
|
|
/// </summary>
|
|
public GameObject previewMessage;
|
|
|
|
/// <summary>
|
|
/// Reference to the score, feedback and image
|
|
/// </summary>
|
|
public GameObject userFeedback;
|
|
|
|
protected override Theme signPredictorTheme
|
|
{
|
|
get { return currentTheme; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wait for a given amount of time (specified in song) before spawning symbols
|
|
/// </summary>
|
|
IEnumerator WaitThenStart(float nrOfSeconds)
|
|
{
|
|
//yield on a new YieldInstruction that waits for nrOfSeconds seconds
|
|
yield return new WaitForSeconds(nrOfSeconds);
|
|
gameIsActive = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update is called once per frame
|
|
/// </summary>
|
|
void Update()
|
|
{
|
|
if (gameIsActive)
|
|
{
|
|
// Destroy the oldest symbol if it leaves the screen
|
|
if (activeSymbols.Count > 0)
|
|
{
|
|
if (activeSymbols[0].GetComponent<RectTransform>().localPosition.x > trackX)
|
|
{
|
|
DestroySymbolAt(0);
|
|
incorrectSigns++;
|
|
timingFeedback.text = $"Te laat! \n {offscreenScore}";
|
|
imageFeedback.sprite = tooLateSprite;
|
|
}
|
|
}
|
|
|
|
// Spawn new symbol every spawn period
|
|
float currentTime = Time.time;
|
|
if (currentTime - lastSpawn > currentSong.spawnPeriod && lastSymbolTime > currentTime)
|
|
{
|
|
lastSpawn = currentTime;
|
|
SpawnNewSymbol();
|
|
}
|
|
|
|
// Check if the song has ended and activate scorescreen if it has
|
|
if (currentTime - beginTime > currentSong.duration)
|
|
{
|
|
// The boolean that is passed is irrelevant for this game
|
|
ActivateEnd(true);
|
|
}
|
|
|
|
// Move all active symbols to the right
|
|
foreach (GameObject symbol in activeSymbols)
|
|
{
|
|
RectTransform rectTransform = symbol.GetComponent<RectTransform>();
|
|
rectTransform.localPosition = new Vector3(rectTransform.localPosition.x + Time.deltaTime * moveSpeed, trackY, 0);
|
|
}
|
|
|
|
scoreDisplay.text = $"Score: {CalculateScore()}";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calculate the score
|
|
/// </summary>
|
|
/// <returns>The calculated score</returns>
|
|
public override int CalculateScore()
|
|
{
|
|
return goodSigns * goodScore + perfectSigns * perfectScore + mehScore * mehSigns + terribleScore * terribleSigns + incorrectSigns * offscreenScore;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Destroy the symbol at the given index
|
|
/// </summary>
|
|
/// <param name="index">The index of the symbol to destroy</param>
|
|
void DestroySymbolAt(int index)
|
|
{
|
|
activeWords.RemoveAt(index);
|
|
GameObject symbol = activeSymbols[index];
|
|
activeSymbols.RemoveAt(index);
|
|
Destroy(symbol);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new symbol at the start of the track
|
|
/// </summary>
|
|
void SpawnNewSymbol()
|
|
{
|
|
// Pick a word that isn't in use yet
|
|
List<int> unusedWordIndices = new List<int>();
|
|
|
|
for (int i = 0; i < words.Count; i++)
|
|
{
|
|
if (!activeWords.Contains(words[i].name))
|
|
{
|
|
unusedWordIndices.Add(i);
|
|
}
|
|
}
|
|
|
|
Learnable newLearnable = words[unusedWordIndices[Random.Range(0, unusedWordIndices.Count)]];
|
|
string nextSymbol = newLearnable.name;
|
|
|
|
GameObject newSymbolObject = GameObject.Instantiate(symbolPrefab, new Vector3(0, trackY, 0), Quaternion.identity, symbolContainer);
|
|
|
|
// Dynamically load appearance
|
|
Image image = newSymbolObject.transform.Find("Image").GetComponent<Image>();
|
|
image.sprite = newLearnable.image;
|
|
|
|
// Place the word that the symbol represents under the image
|
|
TMP_Text text = newSymbolObject.GetComponentInChildren<TMP_Text>();
|
|
text.text = nextSymbol;
|
|
|
|
activeWords.Add(nextSymbol.ToUpper());
|
|
activeSymbols.Add(newSymbolObject);
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
Learnable predSign = currentTheme.learnables.Find(l => l.name.ToUpper() == predictedSign);
|
|
|
|
// If there is a feedback-object, we wil change its appearance
|
|
if (feedbackText != null && feedbackProgressImage != null)
|
|
{
|
|
Color col;
|
|
if (accuracy > predSign.thresholdPercentage)
|
|
{
|
|
feedbackText.text = $"Herkent '{predictedSign}'";
|
|
col = new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f);
|
|
}
|
|
else if (accuracy > 0.9 * predSign.thresholdPercentage)
|
|
{
|
|
feedbackText.text = $"Lijkt op '{predictedSign}'";
|
|
col = new Color(0xf2 / 255.0f, 0x7f / 255.0f, 0x0c / 255.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(accuracy / predSign.thresholdPercentage, 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 (accuracy > predSign.thresholdPercentage)
|
|
{
|
|
int matchedSymbolIndex = activeWords.IndexOf(predictedSign.ToUpper());
|
|
|
|
// Destroy the oldest symbol if the current input matches it
|
|
if (0 <= matchedSymbolIndex)
|
|
{
|
|
float x = activeSymbols[matchedSymbolIndex].transform.localPosition.x;
|
|
|
|
// parameters to define the Perfect hit zone
|
|
float perfectRange = hitZonePerfect.sizeDelta.x;
|
|
float perfectCenter = hitZonePerfect.localPosition.x;
|
|
// parameters to define the Good hit zone
|
|
float goodRange = hitZoneGood.sizeDelta.x;
|
|
float goodCenter = hitZoneGood.localPosition.x;
|
|
// parameters to define the Meh hit zone
|
|
float mehRange = hitZoneMeh.sizeDelta.x;
|
|
float mehCenter = hitZoneMeh.localPosition.x;
|
|
|
|
if (perfectCenter - perfectRange / 2 <= x && x <= perfectCenter + perfectRange / 2)
|
|
{
|
|
timingFeedback.text = $"Perfect! \n +{perfectScore}";
|
|
imageFeedback.sprite = perfectSprite;
|
|
perfectSigns++;
|
|
timingFeedback.color = new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f);
|
|
}
|
|
else if (goodCenter - goodRange / 2 <= x && x <= goodCenter + goodRange / 2)
|
|
{
|
|
timingFeedback.text = $"Goed \n +{goodScore}";
|
|
imageFeedback.sprite = goodSprite;
|
|
goodSigns++;
|
|
timingFeedback.color = new Color(0xf7 / 255.0f, 0xad / 255.0f, 0x19 / 255.0f);
|
|
}
|
|
else if (mehCenter - mehRange / 2 <= x && x <= mehCenter + mehRange / 2)
|
|
{
|
|
timingFeedback.text = $"Bijna... \n +{mehScore}";
|
|
imageFeedback.sprite = mehSprite;
|
|
mehSigns++;
|
|
timingFeedback.color = new Color(0xf2 / 255.0f, 0x7f / 255.0f, 0x0c / 255.0f);
|
|
}
|
|
else
|
|
{
|
|
timingFeedback.text = $"Te vroeg! \n {terribleScore}";
|
|
imageFeedback.sprite = terribleSprite;
|
|
terribleSigns++;
|
|
timingFeedback.color = new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f);
|
|
}
|
|
|
|
DestroySymbolAt(matchedSymbolIndex);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The logic to set the scoreboard of justsign
|
|
/// </summary>
|
|
/// <param name="victory">Shows whether or not the player won, is not relevant for JustSIgn</param>
|
|
protected override void SetScoreBoard(bool victory)
|
|
{
|
|
gameEndedPanel.GetComponent<JustSignGameEndedPanel>().GenerateContent(
|
|
perfectSigns: perfectSigns,
|
|
goodSigns: goodSigns,
|
|
mehSigns: mehSigns,
|
|
terribleSigns: terribleSigns,
|
|
incorrectSigns: incorrectSigns,
|
|
duration: currentSong.duration,
|
|
score: CalculateScore()
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The justsign-specific logic that needs to be called at the start of the game
|
|
/// </summary>
|
|
protected override void StartGameLogic()
|
|
{
|
|
// Set the current theme so that it can be passed along
|
|
|
|
currentTheme = minigame.themeList.themes[minigame.themeList.currentThemeIndex];
|
|
|
|
userFeedback.SetActive(currentTheme.modelIndex != ModelIndex.NONE);
|
|
previewMessage.SetActive(currentTheme.modelIndex == ModelIndex.NONE);
|
|
perfectSigns = 0;
|
|
goodSigns = 0;
|
|
mehSigns = 0;
|
|
terribleSigns = 0;
|
|
incorrectSigns = 0;
|
|
timingFeedback.text = "";
|
|
imageFeedback.sprite = minigame.thumbnail;
|
|
gameEndedPanel.SetActive(false);
|
|
|
|
scoreDisplay.text = $"Score: {CalculateScore()}";
|
|
words.AddRange(currentTheme.learnables);
|
|
currentSong = songList.songs[songList.currentSongIndex];
|
|
AudioSource.PlayClipAtPoint(currentSong.song, Vector3.zero, 1.0f);
|
|
beginTime = Time.time;
|
|
lastSymbolTime = beginTime + currentSong.duration - 1920.0f / moveSpeed;
|
|
|
|
StartCoroutine(WaitThenStart(currentSong.firstSymbolTime));
|
|
}
|
|
|
|
/// <summary>
|
|
/// The justsign-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;
|
|
while (activeSymbols.Count > 0)
|
|
{
|
|
DestroySymbolAt(0);
|
|
}
|
|
}
|
|
}
|