Exemplo n.º 1
0
    // Converts the top cell to sand if certain criteria are met
    // This modifies terrain based on neighbor stacks
    // Thus it should only be called after all cell stacks are generated
    void AddSand(CellStack cellStack)
    {
        if (cellStack.Count() == terrain.waterLevel)
        {
            HexCoordinates[] neighbors = cellStack.coordinates.GetNeighbors();
            for (int i = 0; i < 6; i++)
            {
                CellStack neighbor = GetCellStackFromWorldCoords(neighbors[i]);

                // The neighbor cell stack might not be in this chunk
                if (neighbor == null)
                {
                    TerrainChunk neighborChunk = terrain.GetChunkFromWorldCoords(neighbors[i]);
                    if (neighborChunk != null)
                    {
                        neighbor = neighborChunk.GetCellStackFromWorldCoords(neighbors[i]);
                    }
                }

                if (neighbor != null)
                {
                    if (neighbor.Count() < terrain.waterLevel)
                    {
                        cellStack.Pop();
                        cellStack.Push(CellType.Sand);
                        break;
                    }
                }
            }
        }
    }
Exemplo n.º 2
0
    CellStack CreateCellStack(int x, int z, int height)
    {
        CellStack      cellStack        = ScriptableObject.CreateInstance <CellStack>();
        HexCoordinates coords           = HexCoordinates.FromOffsetCoordinates(x + (int)offsetOrigin.x, z + (int)offsetOrigin.y);
        Vector2Int     indexWithinChunk = new Vector2Int(x % size, z % size);

        cellStack.Init(coords, indexWithinChunk);
        cellStack.Push(CellType.Bedrock);

        int numStoneTiles = (int)(height * terrain.stoneHeightPercentage);

        // Generate terrian based on stack height
        for (int i = 0; i < height; i++)
        {
            CellType newCell;

            if (height >= terrain.waterLevel - 1 && i == height - 1)
            {
                newCell = CellType.Grass;
            }
            else if (i < numStoneTiles)
            {
                newCell = CellType.Stone;
            }
            else
            {
                newCell = CellType.Dirt;
            }

            cellStack.Push(newCell);
        }

        if (showCoordinates && gridCanvas != null)
        {
            Vector3 position = cellStack.coordinates.ToChunkPosition();
            position += HexMetrics.heightVector * (cellStack.Count() + 1);

            Text label = Instantiate <Text>(terrain.cellLabelPrefab);
            label.rectTransform.SetParent(gridCanvas.transform, false);
            label.rectTransform.anchoredPosition3D = new Vector3(position.x, position.z, -position.y);
            label.text = cellStack.coordinates.ToStringOnSeparateLines();
        }

        return(cellStack);
    }
Exemplo n.º 3
0
    public void AddCell(CellType cell, HexCoordinates coords)
    {
        CellStack stack = GetCellStackFromWorldCoords(coords);

        if (stack)
        {
            // Only trees can be placed on top of grass
            // So if something else is placed on top of grass, turn the grass into dirt
            if (stack.Peek() == CellType.Grass)
            {
                stack.Pop();
                stack.Push(CellType.Dirt);
            }

            stack.Push(cell);
            GenerateMeshes();
        }
    }