Пример #1
0
    /// <summary>
    /// Creates a minimum spanning tree using the connections that this graph already has.
    /// </summary>
    /// <returns>The minimum spanning tree.</returns>
    public IEnumerator GenerateMinSpanTree(RDGGraph mst)
    {
        List <RDGRoom> undiscovered = new List <RDGRoom>();

        foreach (var item in graph.Keys)
        {
            undiscovered.Add(item);
        }

        RDGRoom currFrom = undiscovered[0];

        undiscovered.Remove(currFrom);
        mst.AddRoom(currFrom);

        RDGRoom currTo;
        int     currMinDist;

        while (undiscovered.Count > 0)
        {
            //Reset vars
            currTo      = currFrom = null;
            currMinDist = int.MaxValue;

            //Find the shortest connection to a new room

            //Check through every room we've discovered
            foreach (var discoveredRoom in mst.graph.Keys)
            {
                //Get its adjency list from the creator object
                foreach (var room in graph[discoveredRoom])
                {
                    //Check if undiscovered
                    if (undiscovered.Contains(room))
                    {
                        int distance = RDGMath.DistBetweenRooms(discoveredRoom, room);
                        if (currTo == null || distance < currMinDist)
                        {
                            currFrom    = discoveredRoom;
                            currTo      = room;
                            currMinDist = distance;
                        }
                    }
                }
            }

            //Remove that room from undiscovered and add it to the graph
            undiscovered.Remove(currTo);
            mst.AddRoom(currTo);

            //Connect the room to the graph
            mst.AddConnection(currFrom, currTo);

            yield return(null);
        }
    }
    IEnumerator Generate()
    {
        graph    = null;
        minGraph = null;
        theGrid  = null;

        acting = true;
        rooms.Clear();
        RDGRoom.ResetIDs();

        for (int i = 0; i < numRooms; i++)
        {
            rooms.Add(MakeRoom());
            yield return(null);
        }

        while (PushRoomsOut())
        {
            yield return(null);
        }

        tris = Triangulate();

        graph = new RDGGraph(rooms, tris);

        minGraph = new RDGGraph();
        yield return(StartCoroutine(graph.GenerateMinSpanTree(minGraph)));

        yield return(StartCoroutine(AddExtraConnections()));

        BuildGrid();

        yield return(StartCoroutine(BuildCorridors()));

        foreach (var item in rooms)
        {
            MakeRoomFloor(item);
            yield return(null);

            yield return(StartCoroutine(GenerateWalls(item)));
        }

        acting = false;
    }