126 lines
3.3 KiB
C#
126 lines
3.3 KiB
C#
using Assets.Scripts;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using static GameHandler;
|
|
|
|
public class TileSpawner : SingletonMB<TileSpawner>
|
|
{
|
|
#region PROPERTIES
|
|
#endregion
|
|
#region VARIABLES
|
|
|
|
private GameHandler _gameHandler;
|
|
|
|
[SerializeField] private int maxTile = 6;
|
|
[SerializeField] private GameObject tilePrefab;
|
|
[SerializeField] private float tileLength = 15f;
|
|
[SerializeField] private float tileMinX = 15f;
|
|
|
|
private List<Transform> tileList = new List<Transform>();
|
|
|
|
private GameObject tilesParent;
|
|
private bool attemptStarted;
|
|
private float startPositionX;
|
|
|
|
#endregion
|
|
#region EVENTS
|
|
private void StartAttempt_OnAttemptStarted(object sender, AttemptEventArgs args)
|
|
{
|
|
attemptStarted = true;
|
|
}
|
|
private void EndAttempt_OnAttemptEnded(object sender, AttemptEventArgs args)
|
|
{
|
|
attemptStarted = false;
|
|
}
|
|
|
|
#endregion
|
|
#region METHODS
|
|
|
|
private void SpawnAllTiles()
|
|
{
|
|
for (int i = 1; i <= maxTile; i++)
|
|
{
|
|
SpawnTile(i);
|
|
}
|
|
}
|
|
|
|
private void SpawnTile(int tileNumber)
|
|
{
|
|
GameObject newTile = Instantiate(tilePrefab, transform.position-new Vector3((tileNumber*tileLength),0,0), Quaternion.identity, transform);
|
|
//transform.position += new Vector3(tileLength, 0, 0);
|
|
tileList.Add(newTile.transform);
|
|
}
|
|
|
|
private void ResetBackgroundPosition()
|
|
{
|
|
transform.position += new Vector3(tileLength, transform.position.y, transform.position.z);
|
|
}
|
|
private void MoveTilesHorizontally()
|
|
{
|
|
transform.position += new Vector3(-GameHandler.Instance.FrameDistance, 0, 0);
|
|
if(startPositionX- transform.position.x > tileLength)
|
|
{
|
|
ResetBackgroundPosition();
|
|
}
|
|
|
|
//List<Transform> tilesToRemove = new List<Transform>();
|
|
|
|
//foreach (Transform tileTransform in tileList)
|
|
//{
|
|
// tileTransform.position += new Vector3(GameHandler.Instance.TotalDistance,0,0);;
|
|
// if (tileTransform.position.x < tileMinX)
|
|
// {
|
|
// tilesToRemove.Add(tileTransform);
|
|
// }
|
|
//}
|
|
|
|
//// Remove tiles that have gone off screen
|
|
//foreach (Transform tileToRemove in tilesToRemove)
|
|
//{
|
|
// tileList.Remove(tileToRemove);
|
|
// Destroy(tileToRemove.gameObject);
|
|
// SpawnTile();
|
|
//}
|
|
}
|
|
|
|
private float GetStartPosition()
|
|
{
|
|
Camera mainCamera = Camera.main;
|
|
float screenRight = mainCamera.ScreenToWorldPoint(new Vector3(Screen.width, 0, mainCamera.nearClipPlane)).x;
|
|
return screenRight+2*tileLength;
|
|
}
|
|
|
|
#endregion
|
|
#region LIFECYCLE
|
|
|
|
void Start()
|
|
{
|
|
attemptStarted = false;
|
|
|
|
_gameHandler = GameHandler.Instance;
|
|
_gameHandler.OnAttemptStarted += StartAttempt_OnAttemptStarted;
|
|
_gameHandler.OnAttemptEnding += EndAttempt_OnAttemptEnded;
|
|
|
|
startPositionX = GetStartPosition();
|
|
transform.position = new Vector3(startPositionX, transform.position.y, transform.position.z);
|
|
|
|
|
|
SpawnAllTiles();
|
|
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if(attemptStarted)
|
|
{
|
|
MoveTilesHorizontally();
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|