using System.Collections; using System.Collections.Generic; using UnityEngine; using MyTypes; public class LevelGeneration : MonoBehaviour{ [SerializeField] GameObject levelPrefab; [SerializeField] bool spawnGrid = true; [Space] [SerializeField] Material black; [SerializeField] Material white; // Start is called before the first frame update void Start(){ if (GameManager.Instance.autoStart) StartGeneration(); } public void StartGeneration(){ if(spawnGrid) StartCoroutine(SpawnGrid()); } IEnumerator SpawnGrid(){ /*for (int i = 0; i < MyVars.gridWidth; i++){ for (int j = 0; j < MyVars.gridHeight; j++){ MeshRenderer mesh = Instantiate(levelPrefab, new Vector3(i, -1.5f, j), Quaternion.identity).GetComponentInChildren(); mesh.material = (i + j) % 2 == 0 ? black : white; yield return new WaitForSecondsRealtime(0.015625f); } }*/ List positions = new List(); for (int i = 0; i < MyVars.gridWidth; i++){ for (int j = 0; j < MyVars.gridHeight; j++){ positions.Add(new Vector3(i, -1.5f, j)); } } for (int i = 0; i < MyVars.gridWidth * MyVars.gridHeight; i += 3){ if (i < MyVars.gridWidth * MyVars.gridHeight){ Vector3 pos = positions[Random.Range(0, positions.Count)]; MeshRenderer mesh = Instantiate(levelPrefab, pos, Quaternion.identity).GetComponentInChildren(); mesh.material = (pos.x + pos.z) % 2 == 0 ? black : white; positions.Remove(pos); } if (i + 1 < MyVars.gridWidth * MyVars.gridHeight){ Vector3 pos = positions[Random.Range(0, positions.Count)]; MeshRenderer mesh = Instantiate(levelPrefab, pos, Quaternion.identity).GetComponentInChildren(); mesh.material = (pos.x + pos.z) % 2 == 0 ? black : white; positions.Remove(pos); } if (i + 2 < MyVars.gridWidth * MyVars.gridHeight){ Vector3 pos = positions[Random.Range(0, positions.Count)]; MeshRenderer mesh = Instantiate(levelPrefab, pos, Quaternion.identity).GetComponentInChildren(); mesh.material = (pos.x + pos.z) % 2 == 0 ? black : white; positions.Remove(pos); } yield return new WaitForSecondsRealtime(0.01f); } } }