void SpawnMap() { mappings = mappingsHolder.mappings; for (int x = 0; x < fullMap.width; x++) { for (int y = 0; y < fullMap.height; y++) { Color pixelColor = fullMap.GetPixel(x, y); if (pixelColor.a == 0) { continue; } TileMapping tile = Array.Find(mappings, TileMapping => TileMapping.color == pixelColor); Vector3 thisOffset = new Vector3(tileSize * (float)x, tileSize * (float)y, 0); Vector3 spawnPos = transform.position + thisOffset; if (tile != null && tile.prefab != null) //avoid errors during testing { Instantiate(tile.prefab, spawnPos, Quaternion.identity).transform.parent = this.transform; } else { print("no prefab for this mapping"); } } } SpawnableMapping[] spawnablesMappings = mappingsHolder.spawnableMappings; for (int x = 0; x < spawnablesMap.width; x++) { for (int y = 0; y < spawnablesMap.height; y++) { Color pixelColor = spawnablesMap.GetPixel(x, y); if (pixelColor.a == 0) { continue; } SpawnableMapping tile = Array.Find(spawnablesMappings, SpawnableMapping => SpawnableMapping.color == pixelColor); Vector3 thisOffset = new Vector3(tileSize * (float)x, tileSize * (float)y, 0); Vector3 spawnPos = transform.position + thisOffset; if (tile != null && tile.prefab != null) //avoid errors during testing { Instantiate(tile.prefab, spawnPos, Quaternion.identity).transform.parent = this.transform; } else { print("Could not instanciate spawnable with pixel color: " + pixelColor); } } } }
bool IsValidPosition(Vector2Int pos, List <Vector2Int> takenPositions, SpawnableMapping mapping) { bool ret = true; if (takenPositions.Contains(pos)) { ret = false; //dont pick used position } if (takenPositions.Contains(pos + Vector2Int.down) || takenPositions.Contains(pos + Vector2Int.up) || takenPositions.Contains(pos + Vector2Int.left) || takenPositions.Contains(pos + Vector2Int.right)) { ret = false; //dont pick adjacent to used position } if (mapping.type == SpawnableMapping.SpawnableType.treasure) { float weight = treasureWeights.Find(treasureWieght => treasureWieght.position == pos).weight; if (weight < UnityEngine.Random.value) { ret = false; // weight treasure spawning } } return(ret); }