using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileManager : MonoBehaviour {
public GameObject[] tilePrefabs;
public GameObject currentTile;
private static TileManager instance;
private Stack<GameObject> leftTiles = new Stack<GameObject>();
private Stack<GameObject> topTiles = new Stack<GameObject>();
private Stack<GameObject> rightTiles = new Stack<GameObject>();
public static TileManager Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<TileManager> ();
}
return instance;
}
}
public Stack<GameObject> LeftTiles
{
get
{
return leftTiles;
}
set
{
leftTiles = value;
}
}
public Stack<GameObject> TopTiles
{
get
{
return topTiles;
}
set
{
topTiles = value;
}
}
public Stack<GameObject> RightTiles
{
get
{
return rightTiles;
}
set
{
rightTiles = value;
}
}
// Use this for initialization
void Start ()
{
CreateTiles (30);
for (int i = 0; i < 15; i++)
{
SpawnTiles ();
}
}
// Update is called once per frame
void Update () {
}
public void CreateTiles (int amount)
{
for (int i = 0; i < amount; i++)
{
LeftTiles.Push (Instantiate (tilePrefabs [0]));
TopTiles.Push (Instantiate (tilePrefabs [1]));
RightTiles.Push (Instantiate (tilePrefabs[2]));
TopTiles.Peek ().name = "TopTile";
TopTiles.Peek ().SetActive (false);
LeftTiles.Peek ().name = "LeftTile";
LeftTiles.Peek ().SetActive (false);
RightTiles.Peek ().name = "RightTile";
RightTiles.Peek ().SetActive (false);
}
}
public void SpawnTiles()
{
if (LeftTiles.Count == 0 || TopTiles.Count == 0 || RightTiles.Count == 0)
{
CreateTiles (15);
}
//Generating a random number between 0 and 1
int randomIndex = Random.Range (0, 3);
if (randomIndex == 0)
{
GameObject tmp = LeftTiles.Pop ();
tmp.SetActive (true);
tmp.transform.position = currentTile.transform.GetChild (0).transform.GetChild (randomIndex).position;
currentTile = tmp;
}
else if(randomIndex == 1)
{
GameObject tmp = TopTiles.Pop ();
tmp.SetActive (true);
tmp.transform.position = currentTile.transform.GetChild (0).transform.GetChild (randomIndex).position;
currentTile = tmp;
}
else if (randomIndex == 2)
{
GameObject tmp = RightTiles.Pop();
tmp.SetActive(true);
tmp.transform.position = currentTile.transform.GetChild(0).transform.GetChild(randomIndex).position;
currentTile = tmp;
}
currentTile = (GameObject)Instantiate (tilePrefabs[randomIndex], currentTile.transform.GetChild (0).transform.GetChild (randomIndex).transform.GetChild(randomIndex).position, Quaternion.identity);
}
}