Esempio n. 1
0
    public override void Apply(Vector2Int userNode, GridTerrainData terrainData, NodeAction action)
    {
        var userNodeValue = terrainData[userNode.x, userNode.y];

        for (int x = 0; x < terrainData.Width; x++)
        {
            for (int y = 0; y < terrainData.Height; y++)
            {
                action(userNode, userNodeValue, new Vector2Int(x, y), terrainData);
            }
        }
    }
    public override void Apply(Vector2Int userNode, GridTerrainData terrainData, NodeAction action)
    {
        var userNodeValue = terrainData[userNode.x, userNode.y];

        for (int x = Mathf.Max(userNode.x - _paintRadius, 0); x < Mathf.Min(userNode.x + _paintRadius + 1, terrainData.Width); x++)
        {
            for (int y = Mathf.Max(userNode.y - _paintRadius, 0); y < Mathf.Min(userNode.y + _paintRadius + 1, terrainData.Height); y++)
            {
                action(userNode, userNodeValue, new Vector2Int(x, y), terrainData);
            }
        }
    }
 private static void LevelHeightAction(Vector2Int userNode, int userNodeValue, Vector2Int currentNode, GridTerrainData editCopy)
 {
     editCopy[currentNode.x, currentNode.y] = userNodeValue;
 }
 private void SetHeightAction(Vector2Int userNode, int userNodeValue, Vector2Int currentNode, GridTerrainData editCopy)
 {
     editCopy[currentNode.x, currentNode.y] = _setHeight;
 }
    private void RandomHeightAction(Vector2Int userNode, int userNodeValue, Vector2Int currentNode, GridTerrainData editCopy)
    {
        var height = Mathf.PerlinNoise(currentNode.x * _randScale.x, currentNode.y * _randScale.y) * _randStrength;

        height = (height) * (_randMaxHeight - _randMinHeight) + _randMinHeight;
        var intHeight = Mathf.Clamp(Mathf.RoundToInt(height), _randMinHeight, _randMaxHeight);

        editCopy[currentNode.x, currentNode.y] = intHeight;
    }
 private static void DecreaseHeightAction(Vector2Int userNode, int userNodeValue, Vector2Int currentNode, GridTerrainData editCopy)
 {
     editCopy[currentNode.x, currentNode.y]--;
 }
Esempio n. 7
0
 /// <summary>
 /// Applies the brush to the given nodes.
 /// </summary>
 /// <param name="userNode">The node the user is clicking on</param>
 /// <param name="nodes">The serialized node array</param>
 /// <param name="terrain">The terrain object being edited, modify directly!</param>
 /// <param name="invert">When true the brush should function inverted, so lower terrain for example</param>
 public abstract void Apply(Vector2Int userNode, GridTerrainData terrainData, NodeAction action);
    public override void Apply(Vector2Int userNode, GridTerrainData terrainData, NodeAction action)
    {
        var nodesToCheck = new Queue <Vector2Int>();
        var checkedNodes = new HashSet <Vector2Int>();

        nodesToCheck.Enqueue(userNode);
        int guard         = 0;
        var userNodeValue = terrainData[userNode.x, userNode.y];

        while (nodesToCheck.Count > 0 && guard < terrainData.Width * terrainData.Height)
        {
            var currentNode = nodesToCheck.Dequeue();
            if (checkedNodes.Contains(currentNode))
            {
                // It is possible for a node to be added multiple times to the queue
                continue;
            }
            // Apply the action to this node
            action(userNode, userNodeValue, currentNode, terrainData);
            // Find neighbours that we need to add
            checkedNodes.Add(currentNode);
            Vector2Int relLeft, relTopLeft, relBottomLeft, relRight, relTopRight, relBottomRight, relTop, relBottom;
            bool       left = false, right = false, top = false, bottom = false;
            if (currentNode.x >= 1)
            {
                left = true;
            }
            if (currentNode.x < terrainData.Width - 1)
            {
                right = true;
            }
            if (currentNode.y >= 1)
            {
                bottom = true;
            }
            if (currentNode.y < terrainData.Height - 1)
            {
                top = true;
            }
            relLeft        = currentNode + Left;
            relTopLeft     = currentNode + TopLeft;
            relBottomLeft  = currentNode + BottomLeft;
            relRight       = currentNode + Right;
            relTopRight    = currentNode + TopRight;
            relBottomRight = currentNode + BottomRight;
            relTop         = currentNode + Top;
            relBottom      = currentNode + Bottom;

            if (left &&
                !checkedNodes.Contains(relLeft) &&
                terrainData[relLeft.x, relLeft.y] == userNodeValue)
            {
                nodesToCheck.Enqueue(relLeft);
            }
            if (left &&
                top &&
                !checkedNodes.Contains(relTopLeft) &&
                terrainData[relTopLeft.x, relTopLeft.y] == userNodeValue)
            {
                nodesToCheck.Enqueue(relTopLeft);
            }
            if (left &&
                bottom &&
                !checkedNodes.Contains(relBottomLeft) &&
                terrainData[relBottomLeft.x, relBottomLeft.y] == userNodeValue)
            {
                nodesToCheck.Enqueue(relBottomLeft);
            }
            if (right &&
                !checkedNodes.Contains(relRight) &&
                terrainData[relRight.x, relRight.y] == userNodeValue)
            {
                nodesToCheck.Enqueue(relRight);
            }
            if (right &&
                top &&
                !checkedNodes.Contains(relTopRight) &&
                terrainData[relTopRight.x, relTopRight.y] == userNodeValue)
            {
                nodesToCheck.Enqueue(relTopRight);
            }
            if (right &&
                bottom &&
                !checkedNodes.Contains(relBottomRight) &&
                terrainData[relBottomRight.x, relBottomRight.y] == userNodeValue)
            {
                nodesToCheck.Enqueue(relBottomRight);
            }
            if (top &&
                !checkedNodes.Contains(relTop) &&
                terrainData[relTop.x, relTop.y] == userNodeValue)
            {
                nodesToCheck.Enqueue(relTop);
            }
            if (bottom &&
                !checkedNodes.Contains(relBottom) &&
                terrainData[relBottom.x, relBottom.y] == userNodeValue)
            {
                nodesToCheck.Enqueue(relBottom);
            }
            guard++;
        }
    }