using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridManager : MonoBehaviour
{
public int gridwidth = 16;
public int gridheight = 8;
public int minPathLength = 30;
private EnemyWaveManager waveManager;
public GridCellObject[] gridCells;
public GridCellObject[] sceneryCells;
private PathGenerator pathGenerator;
// Start is called before the first frame update
void Start()
{
pathGenerator = new PathGenerator(gridwidth, gridheight);
waveManager = GetComponent<EnemyWaveManager>();
List<Vector2Int> pathCells = pathGenerator.GeneratePath();
int pathSize = pathCells.Count;
while (pathSize < minPathLength)
{
pathCells = pathGenerator.GeneratePath();
while (pathGenerator.GenerateCrossroads());
pathSize = pathCells.Count;
}
StartCoroutine(CreateGrid(pathCells));
}
IEnumerator CreateGrid(List<Vector2Int> pathCells)
{
yield return LayPathCells(pathCells);
yield return LaySceneryCells();
waveManager.SetPathCells(pathGenerator.GenerateRoute());
}
private IEnumerator LayPathCells(List<Vector2Int> pathCells)
{
foreach (Vector2Int pathCell in pathCells)
{
int neighbourValue = pathGenerator.getCellNeighbourValue(pathCell.x, pathCell.y);
// Debug.Log("Tile " + pathCell.x + " , " + pathCell.y + "neighbour value = " + neighbourValue);
GameObject pathTile = gridCells[neighbourValue].cellPrefab;
GameObject pathTileCell = Instantiate(pathTile, new Vector3(pathCell.x, 0f, pathCell.y), Quaternion.identity);
pathTileCell.transform.Rotate(0f, gridCells[neighbourValue].yRotation, 0f, Space.Self);
yield return new WaitForSeconds(0.1f);
}
yield return null;
}
IEnumerator LaySceneryCells()
{
Debug.Log("Lay Scenery Cells started");
for (int x = 0; x <gridwidth; x++)
{
for (int y = 0; y <gridheight; y++)
{
if (pathGenerator.CellIsEmpty(x, y))
{
int randomSceneryCellIndex = Random.Range(0, sceneryCells.Length);
Instantiate(sceneryCells[randomSceneryCellIndex].cellPrefab, new Vector3(x, 0f, y), Quaternion.identity);
yield return new WaitForSeconds(0.030f);
}
}
}
yield return null;
}
}