Example #1
0
    public void CreatePathBetweenNodes(SettlementPathNode firstNode, int secondNodeDirection, int width, int length = -1)
    {
        SettlementPathNode second = firstNode.Connected[secondNodeDirection];

        if (second == null)
        {
            Debug.Error("Paths are not connected");
            return;
        }

        Vec2i pathDiff         = second.Position - firstNode.Position; //Find the vector that describes first->second
        int   pathDiffLen      = pathDiff.x + pathDiff.z;              //Vector should have only 1 component, so total length can be written as sum
        int   startPointOffset = pathDiffLen / 4;
        int   startPoint       = MiscMaths.RandomRange(startPointOffset, pathDiffLen - startPointOffset);

        //Generate node between two existing nodes
        SettlementPathNode interPathNode = new SettlementPathNode(firstNode.Position + SettlementPathNode.GetDirection(secondNodeDirection) * startPoint);
        int oppositeDir = SettlementPathNode.OppositeDirection(secondNodeDirection);

        //TestNodes.Add(interPathNode);
        //Update connections
        firstNode.AddConnection(secondNodeDirection, interPathNode);
        interPathNode.AddConnection(oppositeDir, firstNode);
        second.AddConnection(oppositeDir, interPathNode);
        interPathNode.AddConnection(secondNodeDirection, second);
        CreatePathFromNode(interPathNode, width, length: length);
    }
Example #2
0
    public SettlementPathNode CreatePathFromNode(SettlementPathNode node, int width, bool extraLength = false, int chosenDirection = -1, int length = -1)
    {
        //If no direction is given, choose a null one
        if (chosenDirection == -1)
        {
            List <int> nullDirection = new List <int>();
            for (int i = 0; i < 4; i++)
            {
                if (node.Connected[i] == null)
                {
                    nullDirection.Add(i);
                }
            }
            Debug.Log(nullDirection.Count);
            //Choose a valid direction and find the vector step
            chosenDirection = GenerationRandom.RandomFromList(nullDirection);
        }

        Vec2i step = SettlementPathNode.GetDirection(chosenDirection);

        //If no length is given or given length is invalid, choose a path length
        if (length == -1 || !InBounds(node.Position + step * length))
        {
            int attemptLength = length == -1 ? GenerationRandom.RandomInt(40, TileSize) : length;
            while (!InBounds(node.Position + step * attemptLength))
            {
                attemptLength -= 1;
            }

            length = attemptLength;
        }
        int   halfWidth     = width / 2;
        Vec2i perpDirection = SettlementPathNode.GetPerpendicular(chosenDirection);

        if (extraLength)
        {
            length += halfWidth;
        }
        for (int l = 0; l < length; l++)
        {
            for (int w = -halfWidth; w <= halfWidth; w++)
            {
                Vec2i pos = node.Position + step * l + perpDirection * w;
                SetTile(pos.x, pos.z, Tile.TEST_BLUE);
            }
        }
        SettlementPathNode endNode = new SettlementPathNode(node.Position + step * length);

        node.AddConnection(chosenDirection, endNode);
        endNode.AddConnection(SettlementPathNode.OppositeDirection(chosenDirection), node);
        return(endNode);
    }
Example #3
0
    public void ConnectPathNodes(SettlementPathNode first, SettlementPathNode second, int width)
    {
        Vec2i diff = second.Position - first.Position; //Find the vector between the two nodes
        //First generate the path in the x direction
        Vec2i xDiff                = new Vec2i(diff.x, 0);
        int   xDiffDirection       = SettlementPathNode.GetDirection(xDiff);
        SettlementPathNode midNode = CreatePathFromNode(first, width, chosenDirection: xDiffDirection, length: Mathf.Abs(diff.x));

        //TestNodes.Add(midNode);
        //Add connections
        first.AddConnection(xDiffDirection, midNode);
        midNode.AddConnection(SettlementPathNode.OppositeDirection(xDiffDirection), first);

        Vec2i zDiff = new Vec2i(0, diff.z);

        ConnectNodes(midNode, second, width);
    }
Example #4
0
    private void DestroyNode(int x, int z)
    {
        SettlementPathNode node = TestNodes2[x, z];

        if (node == null)
        {
            return;
        }
        for (int i = 0; i < 4; i++)
        {
            //SettlementPathNode coni = node.Connected[i];
            if (node.Connected[i] != null)
            {
                GetNode(node.Connected[i])?.AddConnection(SettlementPathNode.OppositeDirection(i), null);
                node.Connected[i].AddConnection(SettlementPathNode.OppositeDirection(i), null);
                node.AddConnection(i, null);
            }
        }
        node             = null;
        TestNodes2[x, z] = null;
    }