Resolve WES-89 "Justsign music"

This commit is contained in:
Lukas Van Rossem
2023-03-25 11:34:08 +00:00
parent fee989006c
commit 668b769094
18 changed files with 307 additions and 57 deletions

View File

@@ -40,6 +40,16 @@ public class JustSignController : MonoBehaviour
/// </summary>
public Minigame minigame;
/// <summary>
/// Reference to the list of available songs
/// </summary>
public SongList songList;
/// <summary>
/// Reference to the currently used song
/// </summary>
private Song currentSong;
/// <summary>
/// The zone that the player should be hitting with his or her inputs
/// </summary>
@@ -80,6 +90,11 @@ public class JustSignController : MonoBehaviour
/// </summary>
private int score;
/// <summary>
/// Have the symbols started spawning or not
/// </summary>
private bool gameIsActive = false;
/// <summary>
/// Width and height of the symbols
/// </summary>
@@ -146,9 +161,14 @@ public class JustSignController : MonoBehaviour
private float lastSpawn;
/// <summary>
/// Determines every how many seconds a symbol should spawn (will become music-dependent later on)
/// Time at which the game started, needed to know when to stop
/// </summary>
private float spawnPeriod = 3.0f;
private float beginTime;
/// <summary>
/// Time at which the last symbol should spawn
/// </summary>
private float lastSymbolTime;
/// <summary>
/// Start is called before the first frame update
@@ -158,10 +178,22 @@ public class JustSignController : MonoBehaviour
scoreDisplay.text = "Score: " + score.ToString();
currentTheme = minigame.themeList.themes[minigame.themeList.currentThemeIndex];
words.AddRange(currentTheme.learnables);
//currentTheme = FindThemeByName(PlayerPrefs.GetString("themeName"));
//words = currentTheme.words;
lastSpawn = Time.time;
SpawnNewSymbol();
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>
/// 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>
@@ -169,56 +201,68 @@ public class JustSignController : MonoBehaviour
/// </summary>
void Update()
{
int matchedSymbolIndex = -1;
for (int i = 0; i < activeWords.Count; i++) {
if (activeWords[i].ToLower() == answerField.text.ToLower()) {
matchedSymbolIndex = i;
}
}
// Destroy the oldest symbol if the current input matches it
if (matchedSymbolIndex >= 0) {
int difference = Math.Abs((int) (activeSymbols[matchedSymbolIndex].transform.position.x - hitZone.transform.position.x));
if (difference < perfectBoundary) {
feedBack.text = "Perfect!";
score += perfectScore;
} else if (difference < goodBoundary) {
feedBack.text = "Good!";
score += goodScore;
} else if (difference < mehBoundary) {
feedBack.text = "Meh...";
score += mehScore;
} else {
feedBack.text = "Terrible!";
score += terribleScore;
if (gameIsActive) {
int matchedSymbolIndex = -1;
for (int i = 0; i < activeWords.Count; i++) {
if (activeWords[i].ToLower() == answerField.text.ToLower()) {
matchedSymbolIndex = i;
}
}
DestroySymbolAt(matchedSymbolIndex);
answerField.text = "";
}
// Destroy the oldest symbol if the current input matches it
if (matchedSymbolIndex >= 0) {
int difference = Math.Abs((int) (activeSymbols[matchedSymbolIndex].transform.position.x - hitZone.transform.position.x));
if (difference < perfectBoundary) {
feedBack.text = "Perfect!";
score += perfectScore;
} else if (difference < goodBoundary) {
feedBack.text = "Good!";
score += goodScore;
} else if (difference < mehBoundary) {
feedBack.text = "Meh...";
score += mehScore;
} else {
feedBack.text = "Terrible!";
score += terribleScore;
}
// Destroy the oldest symbol if it leaves the screen
if (activeSymbols.Count > 0) {
if (activeSymbols[0].GetComponent<RectTransform>().localPosition.x > -trackX) {
DestroySymbolAt(0);
score += offscreenScore;
DestroySymbolAt(matchedSymbolIndex);
answerField.text = "";
}
}
// Spawn new symbol every spawn period
float currentTime = Time.time;
if (currentTime - lastSpawn > spawnPeriod) {
lastSpawn = currentTime;
SpawnNewSymbol();
}
// Destroy the oldest symbol if it leaves the screen
if (activeSymbols.Count > 0) {
if (activeSymbols[0].GetComponent<RectTransform>().localPosition.x > -trackX) {
DestroySymbolAt(0);
feedBack.text = "Terrible!";
score += offscreenScore;
}
}
// 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);
}
// Spawn new symbol every spawn period
float currentTime = Time.time;
if (currentTime - lastSpawn > currentSong.spawnPeriod && lastSymbolTime > currentTime) {
lastSpawn = currentTime;
SpawnNewSymbol();
}
scoreDisplay.text = "Score: " + score.ToString();
// Check if the song has ended and activate scorescreen if it has
if (currentTime - beginTime > currentSong.duration) {
gameIsActive = false;
while (activeSymbols.Count > 0) {
DestroySymbolAt(0);
}
// TODO: Scoreboard
}
// 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: " + score.ToString();
}
}
/// <summary>