| | |
| | | Vector3Int tileSpawnCoord = new Vector3Int(x, y); |
| | | if (!destroyedTiles.Contains(tileSpawnCoord) && tilemap.HasTile(tileSpawnCoord)) |
| | | { |
| | | // Check potential cluster size before placing |
| | | int clusterSize = CountPotentialClusterSize(x, y, generateable.weight, generateable.clusterWeight); |
| | | if (clusterSize >= generateable.minClusterSize) |
| | | { |
| | | tilemap.SetTile(tileSpawnCoord, generateable.tile); |
| | | } |
| | | //tilemap.SetTile(tileSpawnCoord, generateable.tile); |
| | | } |
| | | } |
| | | |
| | |
| | | yield return null; |
| | | finishedCallback(); |
| | | } |
| | | private int CountPotentialClusterSize(int startX, int startY, int weight, int clusterWeight) |
| | | { |
| | | int size = 0; |
| | | Queue<Vector2Int> toCheck = new Queue<Vector2Int>(); |
| | | HashSet<Vector2Int> checked_positions = new HashSet<Vector2Int>(); |
| | | |
| | | toCheck.Enqueue(new Vector2Int(startX, startY)); |
| | | checked_positions.Add(new Vector2Int(startX, startY)); |
| | | |
| | | while (toCheck.Count > 0) |
| | | { |
| | | Vector2Int current = toCheck.Dequeue(); |
| | | size++; |
| | | |
| | | // Check all 8 neighboring tiles |
| | | for (int dx = -1; dx <= 1; dx++) |
| | | { |
| | | for (int dy = -1; dy <= 1; dy++) |
| | | { |
| | | if (dx == 0 && dy == 0) continue; |
| | | |
| | | Vector2Int neighbor = new Vector2Int(current.x + dx, current.y + dy); |
| | | if (checked_positions.Contains(neighbor)) continue; |
| | | |
| | | float xPerlin = ((float)neighbor.x / maxWidth) * clusterWeight + offsetX; |
| | | float yPerlin = ((float)neighbor.y / maxHeight) * clusterWeight + offsetY; |
| | | float perlinNoise = Mathf.PerlinNoise(xPerlin, yPerlin); |
| | | |
| | | if (perlinNoise <= (1f / (float)weight)) |
| | | { |
| | | toCheck.Enqueue(neighbor); |
| | | checked_positions.Add(neighbor); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | return size; |
| | | } |
| | | } |