miepzerino
2025-04-02 d43c291c837d1aa6a653ba0d455fe3ff2708579e
Assets/Scripts/GenerateTileMap.cs
@@ -131,7 +131,13 @@
                            Vector3Int tileSpawnCoord = new Vector3Int(x, y);
                            if (!destroyedTiles.Contains(tileSpawnCoord) && tilemap.HasTile(tileSpawnCoord))
                            {
                                tilemap.SetTile(tileSpawnCoord, generateable.tile);
                                // 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);
                            }
                        }
@@ -167,4 +173,43 @@
        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;
    }
}