示例#1
0
    void Survey2D(DungeonSquare sq)
    {
        int adjNum = 4;

        sq.adjacents = new DungeonSquare[adjNum];
        int[] adjCoords = new int[adjNum];

        adjCoords[0] = sq.id + 1;
        adjCoords[1] = sq.id + DungeonSquare.ID;
        adjCoords[2] = sq.id - 1;
        adjCoords[3] = sq.id - DungeonSquare.ID;

        for (int i = 0; i < adjNum; i++)
        {
            if (coordinatesToSquare.ContainsKey(adjCoords[i]))            //if the adjacent square is in the list of occupied coordinates
            {
                sq.adjacents[i] = coordinatesToSquare[adjCoords[i]];      //reference the square from the list of occupied coordinates as adjacent
            }
            else if (emptyCoordinatesToSquare.ContainsKey(adjCoords[i]))  //if the adjacent square is in the list of empties
            {
                sq.adjacents[i] = emptyCoordinatesToSquare[adjCoords[i]]; //reference the square from the list of empties as adjacent
            }
            else //if the adjacent square is in neither list
            {
                sq.adjacents[i] = new DungeonSquare(sq.xCoord + directions[i][0], sq.yCoord + directions[i][1]); //make a new DungeonSquare at these coordinates
                DungeonSquare adj = sq.adjacents[i];                                                             //set the square to this adjacency

                emptyCoordinatesToSquare.Add(adj.id, adj);                                                       //add the square to the list of empties
            }
        }
    }
示例#2
0
    public static void SurveyOutlet(DungeonSquare rootSquare, int adjacencyIndex, DungeonSquare[,,] grid, out DungeonSquare outletSquare)
    {
        rootSquare.adjacents[adjacencyIndex] = new DungeonSquare(
            rootSquare.xCoord + directions[adjacencyIndex][0],
            rootSquare.yCoord + directions[adjacencyIndex][1],
            rootSquare.zCoord + directions[adjacencyIndex][2]
            );

        outletSquare          = rootSquare.adjacents[adjacencyIndex];
        outletSquare.isOutlet = true;

        if (outletSquare.xCoord >= 0 && outletSquare.xCoord < grid.GetLength(0) &&
            outletSquare.yCoord >= 0 && outletSquare.yCoord < grid.GetLength(1) &&
            outletSquare.zCoord >= 0 && outletSquare.zCoord < grid.GetLength(2))
        {
            grid[outletSquare.xCoord, outletSquare.yCoord, outletSquare.zCoord] = outletSquare;
            Survey(outletSquare.xCoord, outletSquare.yCoord, outletSquare.zCoord, grid);

            //Fill open slot of any square adjacent to this new outlet
            for (int i = 0; i < outletSquare.adjacents.Length; i++)
            {
                if (outletSquare.adjacents[i] != null)
                {
                    outletSquare.adjacents[i].adjacents[adjDirectionFlips[i]] = outletSquare;
                }
            }
        }
    }
示例#3
0
    public void AttachOutlet(DungeonSquare square, GameObject outlet, DungeonSquare[,] grid, out DungeonSquare outletSquare)
    {
        //Identify and store the root square's open adjacent squares
        List <int>           indices       = new List <int>();
        List <DungeonSquare> openAdjacents = new List <DungeonSquare>();

        for (int i = 0; i < square.adjacents.Length; i++)
        {
            if (square.adjacents[i] == null)
            {
                openAdjacents.Add(square.adjacents[i]);
                indices.Add(i);
            }
        }

        int rand = Random.Range(0, openAdjacents.Count);

        SurveyOutlet(square, indices[rand], grid, out outletSquare);

        //Replace wall with outlet
        Destroy(square.layout.transform.GetChild(indices[rand]).GetChild(0).gameObject);
        outlet = Instantiate(outlet, square.layout.transform.GetChild(indices[rand]));
        ZeroOut(outlet);
        outletSquare.layout = outlet;

        //Add columns
        if (useColumns)
        {
            Transform  t   = square.layout.transform;
            GameObject col = Instantiate(levelPrefabs.column, t.GetChild(indices[rand]));
            ZeroOut(col);
            col = (indices[rand] < 3) ? Instantiate(levelPrefabs.column, t.GetChild(indices[rand] + 1)) : Instantiate(levelPrefabs.column, t.GetChild(0));
            ZeroOut(col);
        }
    }
示例#4
0
    void AddSquare(int x, int y)
    {
        DungeonSquare square = new DungeonSquare(x, y);

        coordinatesToSquare.Add(square.id, square);
        emptyCoordinatesToSquare.Remove(square.id);

        Survey2D(square);
    }
示例#5
0
    void SetOutlet(List <DungeonSquare> linkables, GameObject outlet, out DungeonSquare sq)
    {
        int outletIndex = Random.Range(0, linkables.Count);

        connectLogic.AttachOutlet(linkables[outletIndex], outlet, squares, out sq);
        if (linkables.Count > 1)
        {
            linkables.RemoveAt(outletIndex);
        }
    }
示例#6
0
    public GameObject BuildLayout(DungeonSquare[] adjacents)
    {
        int num2D = 4;

        GameObject output = Instantiate(dungeonSquare);

        //2D
        if (adjacents.Length == num2D)
        {
            Instantiate(levelPrefabs.floor, output.transform);
            Instantiate(levelPrefabs.ceiling, output.transform.GetChild(4));
        }

        //Store the four corners
        Transform[] corners = new Transform[num2D];
        for (int i = 0; i < corners.Length; i++)
        {
            corners[i] = output.transform.GetChild(i);
        }

        for (int i = 0; i < adjacents.Length; i++)
        {
            if (i < num2D)
            {
                DungeonSquare next       = adjacents[i];
                DungeonSquare previous   = (i > 0) ? adjacents[i - 1] : adjacents[num2D - 1];
                GameObject    edgePrefab = levelPrefabs.wall;

                if (next != null && previous != null && useColumns)
                {
                    edgePrefab = levelPrefabs.column;
                }
                else if (next != null)
                {
                    continue;
                }

                Instantiate(edgePrefab, corners[i]);
            }
            else
            {
                //4 is up and 5 is down
                if (i == 4 && adjacents[i] == null)
                {
                    Instantiate(levelPrefabs.ceiling, output.transform);
                }
                else if (i == 5 && adjacents[i] == null)
                {
                    Instantiate(levelPrefabs.floor, output.transform);
                }
            }
        }

        return(output);
    }
示例#7
0
    DungeonSquare[] SetCluster(int size, int col, int row, DungeonSquare[,] grid)
    {
        DungeonSquare[] cluster = new DungeonSquare[size];

        for (int i = 0; i < size; i++)
        {
            cluster[i] = grid[col, row - (size - i)];
        }

        return(cluster);
    }
示例#8
0
    public static void Survey(int rowIndex, int colIndex, DungeonSquare[,] grid)
    {
        DungeonSquare square = grid[rowIndex, colIndex];

        #region Cardinal Directions
        DungeonSquare north = (rowIndex < grid.GetLength(0) - 1) ? grid[rowIndex + 1, colIndex] : null;
        DungeonSquare east  = (colIndex < grid.GetLength(1) - 1) ? grid[rowIndex, colIndex + 1] : null;
        DungeonSquare south = (rowIndex != 0) ? grid[rowIndex - 1, colIndex] : null;
        DungeonSquare west  = (colIndex != 0) ? grid[rowIndex, colIndex - 1] : null;
        #endregion

        DungeonSquare[] adjacentSquares = new DungeonSquare[] { north, east, south, west };
        square.adjacents = adjacentSquares;
    }
示例#9
0
    public void GenerateGrid()
    {
        ClearGrid();
        squares = new DungeonSquare[length, width];
        PrefabDimensions dimensions = connectLogic.dungeonSquare.GetComponent <PrefabDimensions>();

        GenerateCoordinates(dimensions.width, dimensions.length, squares);

        //[Column][Cluster][Square]
        List <List <DungeonSquare[]> > gridClusters = new List <List <DungeonSquare[]> >();

        for (int i = 0; i < length; i++)
        {
            List <DungeonSquare[]> rowClusters = new List <DungeonSquare[]>();
            int clusterSize = 0;

            for (int j = 0; j < width; j++)
            {
                #region Clustering
                DungeonSquare[] cluster;

                //Only 1 cluster for the origin
                if (i == 0 && j == 0)
                {
                    squares[i, j] = new DungeonSquare(i, j);
                    clusterSize++;
                    continue;
                }

                //Store clusters
                float rand = Random.Range(0, 1f);
                if (rand < gapFrequency)
                {
                    if (clusterSize > 0)
                    {
                        //Cluster excluding current (null) square
                        cluster = SetCluster(clusterSize, i, j, squares);
                        rowClusters.Add(cluster);
                        clusterSize = 0;
                    }

                    if (i == 0)
                    {
                        break;
                    }
                    continue;
                }

                squares[i, j] = new DungeonSquare(i, j);

                clusterSize++;
                if (j == width - 1)
                {
                    //Cluster including current (!null) square
                    cluster = SetCluster(clusterSize, i, j + 1, squares);
                    rowClusters.Add(cluster);
                }
                #endregion
            }

            gridClusters.Add(rowClusters);
        }

        #region Linking
        for (int i = 1; i < length; i++)
        {
            //Iterate through clusters
            for (int j = 0; j < gridClusters[i].Count; j++)
            {
                int connections = 0;

                for (int k = 0; k < gridClusters[i][j].Length; k++)
                {
                    DungeonSquare current = gridClusters[i][j][k];

                    if (squares[i - 1, current.yCoord] != null)
                    {
                        connections++;
                    }
                }

                if (connections == 0)
                {
                    //Scout out to the first row to find where this cluster can connect
                    bool hit = false;

                    for (int k = 0; k < gridClusters[i][j].Length; k++)
                    {
                        DungeonSquare current = gridClusters[i][j][k];

                        for (int l = i - 1; l >= 0; l--)
                        {
                            if (squares[l, current.yCoord] != null)
                            {
                                hit = true;
                                for (int m = i - 1; m > l; m--)
                                {
                                    squares[m, current.yCoord] = new DungeonSquare(m, current.yCoord);
                                }
                                break;
                            }
                        }

                        if (hit == true)
                        {
                            break;
                        }
                    }

                    //If there's nowhere it can connect, draw a vertical line, then horizontal to the origin
                    if (hit == false)
                    {
                        DungeonSquare sq = gridClusters[i][j][0];

                        for (int k = i - 1; k >= 0; k--)
                        {
                            squares[k, sq.yCoord] = new DungeonSquare(k, sq.yCoord);
                        }

                        for (int k = sq.yCoord; k >= gridClusters[0][0].Length; k--)
                        {
                            if (squares[0, k] == null)
                            {
                                squares[0, k] = new DungeonSquare(0, k);
                            }
                        }
                    }
                }
            }
        }
        #endregion

        GenerateSquares(squares);
        GenerateOutlets(squares);
    }