using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
///
/// Contains all game logic for the JustSign game
///
public class JustSignController : MonoBehaviour
{
///
/// The canvas containing all components
///
public Canvas canvas;
///
/// The input field where the user can type his or her answer
///
public TMP_InputField answerField;
///
/// All of the words that can be used in this session
///
private string[] words;
///
/// All of the available themes
///
private ThemeList themeList;
///
/// The theme we are currently using
///
private Theme currentTheme;
///
/// List of strings representing all words on the track
///
private List activeWords = new List();
///
/// List of objects representing all symbols on the track
///
private List activeSymbols = new List();
///
/// Width and height of the symbols
///
private int symbolSize = 280;
///
/// Controls movement speed of symbols (higher -> faster)
///
private int moveSpeed = 200;
///
/// Starting X-coordinate of a symbol = (-1920 - symbolsize) / 2
///
private int trackX = -1100;
///
/// Starting Y-coordinate of a symbol
///
private int trackY = -200;
///
/// Time at which the last symbol was spawned
///
private float lastSpawn;
///
/// Start is called before the first frame update
///
void Start()
{
themeList = ThemeLoader.LoadJson();
currentTheme = FindThemeByName(PlayerPrefs.GetString("themeName"));
words = currentTheme.words;
lastSpawn = Time.time;
SpawnNewSymbol();
}
///
/// Update is called once per frame
///
void Update()
{
if (answerField.text.ToLower() == activeWords[0]) {
DestroyRightmostSymbol();
answerField.text = "";
}
if (activeSymbols[0].GetComponent().localPosition.x > -trackX) {
DestroyRightmostSymbol();
}
float currentTime = Time.time;
if (currentTime - lastSpawn > 1) {
lastSpawn = currentTime;
SpawnNewSymbol();
}
foreach (GameObject symbol in activeSymbols) {
RectTransform rectTransform = symbol.GetComponent();
rectTransform.localPosition = new Vector3(rectTransform.localPosition.x + Time.deltaTime * moveSpeed, trackY, 0);
}
}
///
/// Destroy the oldest symbol on the track
///
void DestroyRightmostSymbol() {
activeWords.RemoveAt(0);
GameObject symbol = activeSymbols[0];
activeSymbols.RemoveAt(0);
Destroy(symbol);
}
///
/// Find the chosen theme by its name
///
/// The name of the theme to find
/// The requested theme
private Theme FindThemeByName(string themeName)
{
int themeIndex = 0;
while (themeIndex < themeList.themes.Length)
{
Theme theme = themeList.themes[themeIndex];
if (theme.name == themeName)
{
return theme;
}
themeIndex++;
}
Debug.Log("Requested theme not found");
return null;
}
///
/// Create a new symbol at the start of the track
///
void SpawnNewSymbol() {
string nextSymbol = words[UnityEngine.Random.Range(0, words.Length)];
GameObject newSymbolObject = new GameObject("Symbol");
Image newImage = newSymbolObject.AddComponent();
RectTransform rectTransform = newSymbolObject.GetComponent();
rectTransform.SetParent(canvas.transform, false); // Set the parent to the Canvas
rectTransform.localPosition = new Vector3(trackX, trackY, 0);
rectTransform.sizeDelta = new Vector2(symbolSize, symbolSize);
Sprite sprite = Resources.Load("Common/Images/" + nextSymbol);
// Set the new sprite as the Image component's source image
newImage.sprite = sprite;
activeWords.Add(nextSymbol);
activeSymbols.Add(newSymbolObject);
}
}