Add feedback to keyboard input

This commit is contained in:
lvrossem
2023-03-16 19:03:39 +01:00
parent 39618ab75c
commit 4fdf52b7b1
2 changed files with 200 additions and 5 deletions

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
@@ -19,6 +20,16 @@ public class JustSignController : MonoBehaviour
/// </summary>
public TMP_InputField answerField;
/// <summary>
/// The feedback on the timing
/// </summary>
public TMP_Text feedBack;
/// <summary>
/// The zone that the player should be hitting with his or her inputs
/// </summary>
public GameObject hitZone;
/// <summary>
/// All of the words that can be used in this session
/// </summary>
@@ -44,6 +55,11 @@ public class JustSignController : MonoBehaviour
/// </summary>
private List<GameObject> activeSymbols = new List<GameObject>();
/// <summary>
/// The current score
/// </summary>
private int score;
/// <summary>
/// Width and height of the symbols
/// </summary>
@@ -64,11 +80,31 @@ public class JustSignController : MonoBehaviour
/// </summary>
private int trackY = -200;
/// <summary>
/// Max distance from hit zone to get perfect score
/// </summary>
private int perfectBoundary = 10;
/// <summary>
/// Max distance from hit zone to get good score
/// </summary>
private int goodBoundary = 120;
/// <summary>
/// Max distance from hit zone to get meh score
/// </summary>
private int mehBoundary = 200;
/// <summary>
/// Time at which the last symbol was spawned
/// </summary>
private float lastSpawn;
/// <summary>
/// Determines every how many seconds a symbol should spawn (will become music-dependent later on)
/// </summary>
private float spawnPeriod = 3.0f;
/// <summary>
/// Start is called before the first frame update
/// </summary>
@@ -78,7 +114,8 @@ public class JustSignController : MonoBehaviour
currentTheme = FindThemeByName(PlayerPrefs.GetString("themeName"));
words = currentTheme.words;
lastSpawn = Time.time;
Debug.Log(hitZone.transform.position.x);
Debug.Log(hitZone.transform.position.y);
SpawnNewSymbol();
}
@@ -86,22 +123,42 @@ public class JustSignController : MonoBehaviour
/// Update is called once per frame
/// </summary>
void Update()
{
{ /*
Debug.Log("X");
Debug.Log(activeSymbols[0].transform.position.x);
Debug.Log("Y");
Debug.Log(activeSymbols[0].transform.position.y);
*/
// Destroy the oldest symbol if the current input matches it
if (answerField.text.ToLower() == activeWords[0]) {
int difference = Math.Abs((int) (activeSymbols[0].transform.position.x - hitZone.transform.position.x));
if (difference < perfectBoundary) {
feedBack.text = "Perfect!";
} else if (difference < goodBoundary) {
feedBack.text = "Good!";
} else if (difference < mehBoundary) {
feedBack.text = "Meh...";
} else {
feedBack.text = "Terrible!";
}
DestroyRightmostSymbol();
answerField.text = "";
}
// Destroy the oldest symbol if it leaves the screen
if (activeSymbols[0].GetComponent<RectTransform>().localPosition.x > -trackX) {
DestroyRightmostSymbol();
}
// Spawn new symbol every spawn period
float currentTime = Time.time;
if (currentTime - lastSpawn > 1) {
if (currentTime - lastSpawn > spawnPeriod) {
lastSpawn = currentTime;
SpawnNewSymbol();
}
// 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);