Beispiel #1
0
    // ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
    public IEnumerator GenerateNewHexGrid(uint a_xSize, uint a_ySize, Texture2D a_sampleTexture)
    {
        GameObject gridCell;

        int txWidth  = a_sampleTexture.width;
        int txHeight = a_sampleTexture.height;

        int   pixX, pixY;
        float hueSample, satSample, valSample;

        int     hexOffset;
        Vector3 pos;
        Vector3 scale;

        bool isWall;

        System.Func <int, uint, bool> IsEdge = delegate(int index, uint max)
        {
            return(index == 0 || index == max - 1);
        };



        ClearBoard();
        cols      = a_xSize;
        rows      = a_ySize;
        gridNodes = new TraversableNode[cols, rows];

        for (int i = 0; i < cols; i++)
        {
            for (int j = 0; j < rows; j++)
            {
                hexOffset = (i % 2);
                pixX      = (int)(((float)i / cols) * txWidth);
                pixY      = (int)(((float)j / rows) * txHeight);


                // biome, elevation, temperature
                Color.RGBToHSV(a_sampleTexture.GetPixel(pixX, pixY), out hueSample, out satSample, out valSample);

                isWall = edgesAreWalls && (IsEdge(i, cols) || IsEdge(j, rows));

                float initBias = float.MaxValue;
                Biome biome    = worldProfile.GetSubBiome(hueSample, satSample, valSample, ref initBias);

                gridCell = isWall ? worldProfile.biomeTile : biome.biomeTile;
                gridCell = gridCell == null ? worldProfile.subBiomes[0].biomeTile : gridCell;
                gridCell = Instantiate(gridCell);

                pos = new Vector3(
                    ((-cols / 2) + i) * xOffset,
                    0,
                    (((-rows / 2) + j) + (hexOffset * 0.5f)) * zOffset);

                scale = isWall ? new Vector3(1f, (maxHeight + 1f), 1f) :
                        Vector3.one + Vector3.up * ((int)(satSample * maxHeight));


                gridCell.name             = $"{biome.name}[{i}, {j}]";
                gridCell.transform.parent = transform;

                gridCell.transform.localPosition = pos * 2f;
                gridCell.transform.Rotate(Vector3.up, 30);
                gridCell.transform.localScale = scale;


                gridNodes[i, j] = (gridCell.GetComponent <TraversableNode>());
                gridNodes[i, j] = gridNodes[i, j] == null?gridCell.AddComponent <TraversableNode>() : gridNodes[i, j];

                gridNodes[i, j].coordinates[0]   = i;
                gridNodes[i, j].coordinates[1]   = j;
                gridNodes[i, j].pathingValues[1] = satSample;
                gridNodes[i, j].isTraversable    = !isWall;


                {
                    // Set the biome material
                    Renderer cellRenderer;
                    if (gridCell.TryGetComponent <Renderer>(out cellRenderer))
                    {
                        cellRenderer.material = biome.biomeMaterial;
                    }

                    // Spawn the biome's decoration
                    if (biome.biomeDeco != null)
                    {
                        Vector3 highpoint = gridCell.transform.position + Vector3.up * gridCell.transform.localScale.y / 8f;

                        GameObject deco = Instantiate(biome.biomeDeco);
                        deco.transform.position = highpoint;
                        deco.transform.parent   = gridCell.transform;
                    }
                }



                // Set node neighbors -----
                {
                    if (j > 0)
                    {
                        gridNodes[i, j].nodeData.AddNeighbor(gridNodes[i, j - 1].nodeData);
                    }

                    if (i > 0)
                    {
                        int nextJ = j + (hexOffset * 2 - 1);


                        gridNodes[i, j].nodeData.AddNeighbor(gridNodes[i - 1, j].nodeData);

                        if (nextJ >= 0 && nextJ < rows)
                        {
                            gridNodes[i, j].nodeData.AddNeighbor(gridNodes[i - 1, nextJ].nodeData);
                        }
                    }
                }
            }
            yield return(null);
        }
    }