Create smooth stream of symbols
This commit is contained in:
@@ -1,9 +1,18 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
/// <summary>
|
||||
/// Contains all game logic for the JustSign game
|
||||
/// </summary>
|
||||
public class JustSignController : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// The canvas containing all components
|
||||
/// </summary>
|
||||
public Canvas canvas;
|
||||
|
||||
/// <summary>
|
||||
/// All of the words that can be used in this session
|
||||
/// </summary>
|
||||
@@ -19,23 +28,79 @@ public class JustSignController : MonoBehaviour
|
||||
/// </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>
|
||||
/// Width and height of the symbols
|
||||
/// </summary>
|
||||
private int symbolSize = 280;
|
||||
|
||||
/// <summary>
|
||||
/// Controls movement speed of symbols (higher -> faster)
|
||||
/// </summary>
|
||||
private int moveSpeed = 200;
|
||||
|
||||
/// <summary>
|
||||
/// Starting X-coordinate of a symbol = (-1920 - symbolsize) / 2
|
||||
/// </summary>
|
||||
private int trackX = -1100;
|
||||
|
||||
/// <summary>
|
||||
/// Starting Y-coordinate of a symbol
|
||||
/// </summary>
|
||||
private int trackY = -200;
|
||||
|
||||
/// <summary>
|
||||
/// Time at which the last symbol was spawned
|
||||
/// </summary>
|
||||
private float lastSpawn;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
themeList = ThemeLoader.LoadJson();
|
||||
currentTheme = FindThemeByName(PlayerPrefs.GetString("themeName"));
|
||||
words = currentTheme.words;
|
||||
/*
|
||||
RectTransform rectTransform = newImage.GetComponent<RectTransform>();
|
||||
rectTransform.localPosition = new Vector3(xPosition, yPosition, 0);
|
||||
rectTransform.sizeDelta = new Vector2(width, height);
|
||||
*/
|
||||
lastSpawn = Time.time;
|
||||
|
||||
SpawnNewSymbol();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
{
|
||||
float currentTime = Time.time;
|
||||
if (currentTime - lastSpawn > 1) {
|
||||
lastSpawn = currentTime;
|
||||
SpawnNewSymbol();
|
||||
}
|
||||
|
||||
if (activeSymbols[0].GetComponent<RectTransform>().localPosition.x > -trackX) {
|
||||
Debug.Log("LENGTH BEFORE:");
|
||||
Debug.Log(activeWords.Count);
|
||||
activeWords.RemoveAt(0);
|
||||
Debug.Log("LENGTH AFTER:");
|
||||
Debug.Log(activeWords.Count);
|
||||
GameObject symbol = activeSymbols[0];
|
||||
activeSymbols.RemoveAt(0);
|
||||
Destroy(symbol);
|
||||
}
|
||||
|
||||
//Debug.Log(activeWords.Count);
|
||||
Debug.Log(activeWords.Count);
|
||||
|
||||
foreach (GameObject symbol in activeSymbols) {
|
||||
RectTransform rectTransform = symbol.GetComponent<RectTransform>();
|
||||
rectTransform.localPosition = new Vector3(rectTransform.localPosition.x + Time.deltaTime * moveSpeed, trackY, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -60,4 +125,26 @@ public class JustSignController : MonoBehaviour
|
||||
Debug.Log("Requested theme not found");
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new symbol at the start of the track
|
||||
/// </summary>
|
||||
void SpawnNewSymbol() {
|
||||
string nextSymbol = words[UnityEngine.Random.Range(0, words.Length)];
|
||||
|
||||
GameObject newSymbolObject = new GameObject("Symbol");
|
||||
Image newImage = newSymbolObject.AddComponent<Image>();
|
||||
RectTransform rectTransform = newSymbolObject.GetComponent<RectTransform>();
|
||||
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<Sprite>("Common/Images/" + nextSymbol);
|
||||
|
||||
// Set the new sprite as the Image component's source image
|
||||
newImage.sprite = sprite;
|
||||
|
||||
activeWords.Add(nextSymbol);
|
||||
activeSymbols.Add(newSymbolObject);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user