Twin-Hook/Assets/Scripts/Level/LevelGeneration.cs
Gerard Gascón e0a842f222 init
2025-04-24 17:26:56 +02:00

63 lines
2.1 KiB
C#

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<MeshRenderer>();
mesh.material = (i + j) % 2 == 0 ? black : white;
yield return new WaitForSecondsRealtime(0.015625f);
}
}*/
List<Vector3> positions = new List<Vector3>();
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<MeshRenderer>();
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<MeshRenderer>();
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<MeshRenderer>();
mesh.material = (pos.x + pos.z) % 2 == 0 ? black : white;
positions.Remove(pos);
}
yield return new WaitForSecondsRealtime(0.01f);
}
}
}