IEnumerator GenerateMap(int pMapHeight, int pMapWidth) { //creating a periln values map int[,] map = perlin.GanerateMap(pMapWidth, pMapHeight); //initializing tilemap to desired size tileMap = new Tile[pMapHeight, pMapWidth]; chunkedMap = new List <Tile> [pMapHeight / chunkSize, pMapWidth / chunkSize]; //we have to initialize the array before using //without this step there were no lists to add the tiles later for (int x = 0; x < pMapHeight / chunkSize; x++) { for (int y = 0; y < pMapWidth / chunkSize; y++) { chunkedMap[x, y] = new List <Tile>(); } } //we will use this list for generateing houses List <Vector2Int> buildableTiles = new List <Vector2Int>(); yield return(new WaitForEndOfFrame()); for (int i = 0; i < pMapWidth; i++) { for (int j = 0; j < pMapHeight; j++) { Tile newTile; //casting perlin values to tile types if (map[i, j] == 0) { newTile = new Tile() { type = "water", name = "Empty Tile", level = 0, position = new Vector2Int(i, j) }; } else if (map[i, j] == 1) { newTile = new Tile() { type = "sand", name = "Empty Tile", level = 0, position = new Vector2Int(i, j) }; } else if (map[i, j] == 2) { newTile = new Tile() { type = "grass", name = "Empty Tile", level = 0, position = new Vector2Int(i, j) }; buildableTiles.Add(new Vector2Int(i, j)); } else if (map[i, j] == 3) { //randomizing int tmp = UnityEngine.Random.Range(0, 2); if (tmp == 0) { newTile = new Tile() { type = "trees1", name = "Empty Tile", level = 0, position = new Vector2Int(i, j) }; } else { newTile = new Tile() { type = "trees2", name = "Empty Tile", level = 0, position = new Vector2Int(i, j) }; } buildableTiles.Add(new Vector2Int(i, j)); } else if (map[i, j] == 4) { newTile = new Tile() { type = "trees2", name = "Empty Tile", level = 0, position = new Vector2Int(i, j) }; } else { Debug.LogError("DIDNT FIND TILE TYPE FOR NUMBER:" + map[i, j]); newTile = new Tile(); } //adding to tile Map tileMap[i, j] = newTile; //adding to the chunked tile map int x = Mathf.FloorToInt((float)i / chunkSize); int y = Mathf.FloorToInt((float)j / chunkSize); chunkedMap[x, y].Add(newTile); } } Dictionary <Vector2Int, Tile> houses = new Dictionary <Vector2Int, Tile>(); for (int i = 0; i < mapData.number_of_houses; i++) { int randTile = UnityEngine.Random.Range(0, buildableTiles.Count); Vector2Int pos = buildableTiles[randTile]; int offset = 20; //Making sure not to add to the same tile multiple times or is to close to edge of the map while (houses.ContainsKey(pos) || (pos.x < offset || pos.x > mapData.map_width - offset) || (pos.y < offset || pos.y > mapData.map_height - offset)) { randTile = UnityEngine.Random.Range(0, buildableTiles.Count); pos = buildableTiles[randTile]; } Tile tile; //randomizing tyle type int tmp = UnityEngine.Random.Range(0, 2); if (tmp == 0) { tile = new Tile() { type = "house2", name = "Storage", level = 1, position = pos }; } else { tile = new Tile() { type = "house1", name = "Barrack", level = 2, position = pos }; } houses.Add(pos, tile); //changing the tile on the tilemap tileMap[pos.x, pos.y] = tile; //adding to the chunked tile map int x = Mathf.FloorToInt((float)pos.x / chunkSize); int y = Mathf.FloorToInt((float)pos.y / chunkSize); foreach (var item in chunkedMap[x, y]) { if (item.position == new Vector2Int(pos.x, pos.y)) { //removing old tile from the list of tiles in this chunk chunkedMap[x, y].Remove(item); break; } } //adding new tile to the chunked tile map chunkedMap[x, y].Add(tile); //saving the position a house so we can set the camera on map scene to look at a house on starting the scene if (i == 0) { firstHousePos = pos; } } //signaling the map is done done = true; }