示例#1
0
    public void SetCost(RDGGridNode node, RDGRoom roomFrom, RDGRoom roomTo, RDGGrid grid)
    {
        hueristic = Mathf.Abs(x - Mathf.CeilToInt(roomTo.center.x)) + Mathf.Abs(y - Mathf.CeilToInt(roomTo.center.y));

        if ((roomFrom != null && grid[x, y] == roomFrom) || (roomTo != null && grid[x, y] == roomTo))
        {
            cost = 0;
        }
        else if (grid[x, y] != null)
        {
            if (grid[x, y].type == RoomType.CORRIDOR)
            {
                cost = 0;
            }
            else
            {
                cost = int.MaxValue - hueristic;                 //Subtract the huersitic so that we don't overflow when we call fullCost
            }
        }
        else
        {
            cost = parent.cost + 1;
            if (dir != node.dir)
            {
                cost += 2;
            }
        }
    }
    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;
    }
    void BuildGrid()
    {
        int minX = int.MaxValue;
        int minY = int.MaxValue;
        int maxX = int.MinValue;
        int maxY = int.MinValue;

        foreach (var item in rooms)
        {
            minX = Mathf.Min(minX, item.x);
            maxX = Mathf.Max(maxX, item.x + item.width);
            minY = Mathf.Min(minY, item.y);
            maxY = Mathf.Max(maxY, item.y + item.height);
        }

        theGrid = new RDGGrid(minX, minY, maxX, maxY);

        foreach (var item in rooms)
        {
            theGrid.AddRoom(item);
        }
    }