Hey! What would be the best solution to generate a random tower defense game level? My game consists of square shaped pictures, like the different road elements etc. I was thinking of filling the scene with empty gameobjects, so it will look like a matrix, determine the start and end position, and fill up the empty gameobjects with the road elements so it will somehow form a path from the start point to the end point. Then it is easy to determie the waypoints the enemies follow, because whenever the algorithm puts down a road element, it will put down a waypoint as well. Is this a good idea? Or is there any better? Thanks in advance.
This is very simple code, and you need it edit
public class Generator : Monobehaviour {
public GameObject[,] allTiles = new GameObject[8, 6];
public GameObject tileRoad;
public GameObject otherTile;
private void Generate() {
// Fill map with other tiles
for(int i = 0; i < allTiles.Length; i++) {
for(int j = 0; j < allTiles*.Length; j++) {*
GameObject other = Instantiate(otherTile)
other.transform.position = new Vector2(i,j);
}
}
}
var dir = 0; // direction create til road 0 - up, 1 - down, 2 - left, 3 - right
dir = Random.Range(0, 4);
var pos = Vector2.zero;
// For example create only 5 tiles
for(int i = 0; i < 5; i++) {
switch(dir) {
case 0:
pos.y+= 1;
break;
case 1:
pos.y-= 1;
break;
case 2:
pos.x= 1;
break;
case 3:
pos.x+= 1;
break;
}
GameObject other = Instantiate(tileRoad)
pos.y+= 1;
other.transform.position = new Vector2(pos.x, pos.y);
}
}