Beispiel #1
0
    private void CreateSideRooms(RoomNode node, int roomSupply)
    {
        int availableDoors = Mathf.Max(0, maxDoors - node.DoorCount);

        //There's nothing to do if no doors can be created
        //This will prevent endless loops since the roomSupply eventually drains
        //Prevent, that more doors are created than roomsCounts defines
        if (availableDoors == 0 || roomSupply <= 0 || nodesCreated >= roomsCount)
        {
            return;
        }
        //At least one door should be placed, else return
        int min = 1;
        //Only create as much doors as there are available
        //Don't create more doors than the supply offers
        int max = Mathf.Min(roomSupply, availableDoors);
        //The amount of rooms to be created
        //Since Range's max is exclusive I have to add 1 in order to make it inclusive
        int roomsCreated    = Random.Range(min, max + 1);
        int remainingSupply = roomSupply - roomsCreated;
        //Prevent possible division by zero.
        int supplyPerNode = (remainingSupply > 0) ? (int)Mathf.Ceil(remainingSupply / (float)roomsCreated) : 0;

        //Create new graph nodes, recursively call this function again with the remainingSupply
        for (int i = 0; i < roomsCreated; i++)
        {
            RoomNode newNode = new RoomNode(false, NodeType.SIDE);
            node.AddConnection(newNode);
            int newNodeSupply = (supplyPerNode > remainingSupply) ? Mathf.Max(0, remainingSupply) : supplyPerNode;
            CreateSideRooms(newNode, newNodeSupply);
            nodesCreated++;
        }
    }
Beispiel #2
0
    public void ApplyNewCenter(RoomNode center)
    {
        Queue <RoomNode> nodeQueue = new Queue <RoomNode> ();

        if (center.Parent != null)
        {
            center.AddConnection(center.Parent, false);
            center.Parent = null;
            nodeQueue.Enqueue(center);
        }

        while (nodeQueue.Count > 0)
        {
            RoomNode parent = nodeQueue.Dequeue();
            foreach (RoomNode child in parent.Connections)
            {
                if (child.Parent != parent)
                {
                    if (child.Parent != null)
                    {
                        child.AddConnection(child.Parent, false);
                        nodeQueue.Enqueue(child);
                    }
                    child.RemoveConnection(parent, false);
                    child.Parent = parent;
                }
            }
        }
    }
Beispiel #3
0
    private void CreateCriticalPath()
    {
        RoomNode prevNode = new RoomNode(true, NodeType.START);

        rootnode = prevNode;
        rootnodes.Add(rootnode);
        nodesCreated++;

        for (int i = 1; i < critPathLength; i++)
        {
            NodeType nodeType = i == critPathLength - 1 ? NodeType.END : NodeType.MIDDLE;
            RoomNode newNode  = new RoomNode(true, nodeType);
            prevNode.AddConnection(newNode);
            rootnodes.Add(newNode);
            prevNode = newNode;
            nodesCreated++;
        }
    }