Exemple #1
0
    public override List <DungeonGridCell> CreateDungeon(DungeonManager p_gen, DungeonTheme p_dungeonTheme, DungeonNavigation p_dungeonNav)
    {
        p_gen.m_allCells = new DungeonGridCell[m_cellsInDungeon.x, m_cellsInDungeon.y];

        ///Generates a random amount of room cells
        #region Room Cell Generation
        int numOfRooms = Random.Range(m_minRooms, m_maxRooms);

        List <int> roomCells  = new List <int>();
        List <int> emptyCells = new List <int>();


        List <DungeonGridCell> returningRoomCells = new List <DungeonGridCell>();
        int cellCount = m_cellsInDungeon.x * m_cellsInDungeon.y;
        for (int z = 0; z < cellCount; z++)
        {
            roomCells.Add(z);
        }


        //Create the number of rooms
        for (int i = 0; i < cellCount - numOfRooms; i++)
        {
            int currentCellIndex = Random.Range(0, roomCells.Count);
            emptyCells.Add(roomCells[currentCellIndex]);
            roomCells.RemoveAt(currentCellIndex);
        }
        #endregion


        #region Hallway Cell Generation
        //The hallways
        int currentCorridors = Random.Range((int)((float)emptyCells.Count * m_minEmptyPercent), (int)((float)emptyCells.Count * m_maxEmptyPercent));

        for (int c = 0; c < cellCount - numOfRooms; c++)
        {
            if (c >= currentCorridors)
            {
                int currentCellIndex = Random.Range(0, emptyCells.Count);
                emptyCells.RemoveAt(currentCellIndex);
            }
        }

        #endregion


        #region Creates the connections between cells
        int currentIndex = 0;
        for (int x = 0; x < m_cellsInDungeon.x; x++)
        {
            for (int y = 0; y < m_cellsInDungeon.y; y++)
            {
                p_gen.m_allCells[x, y] = new DungeonGridCell();
                p_gen.m_allCells[x, y].m_connectionPoints = new List <ConnectionPoint>();
                p_gen.m_allCells[x, y].m_connectedTo      = new List <Vector2Int>();


                //Create the rooms
                if (roomCells.Contains(currentIndex))
                {
                    p_gen.m_allCells[x, y].ChangeCellType(DungeonGridCell.CellType.Room);
                }
                else if (emptyCells.Contains(currentIndex))
                {
                    p_gen.m_allCells[x, y].ChangeCellType(DungeonGridCell.CellType.None);
                }

                //The connection points
                else
                {
                    p_gen.m_allCells[x, y].ChangeCellType(DungeonGridCell.CellType.Hallway);
                    Vector2Int bounds = new Vector2Int(m_cellSize.x - m_cellBoarder, m_cellSize.y - m_cellBoarder);
                    Vector3Int newPos = new Vector3Int(Random.Range(2, bounds.x) + (x * m_cellSize.x), Random.Range(2, bounds.y) + (y * m_cellSize.y), 0);

                    p_gen.m_allCells[x, y].AddConnectionPoint(newPos, ConnectionPoint.ConnectionType.Node);
                }


                currentIndex++;
            }
        }

        #endregion

        int[,] dungeonGrid = new int[m_cellsInDungeon.x * m_cellSize.x, m_cellsInDungeon.y *m_cellSize.y];
        //Draw the boarder

        /*for (int x = -m_dungeonMapBounds.x; x < m_cellsInDungeon.x * m_cellSize.x + m_dungeonMapBounds.x; x++)
         * {
         *  for (int y = -m_dungeonMapBounds.y; y < m_cellsInDungeon.y * m_cellSize.y + m_dungeonMapBounds.y; y++)
         *  {
         *      if(x > dungeonGrid.GetLength(0))
         *      {
         *          Debug.LogError("X Too big: " + x);
         *      }
         *      else if (y > dungeonGrid.GetLength(1))
         *      {
         *          Debug.LogError("Y Too big: " + y);
         *      }
         *      dungeonGrid[x, y] = 0;
         *
         *  }
         * }*/


        //Get the items structs
        p_dungeonTheme.FixRates();
        List <ItemStruct> itemsInDungeon = p_dungeonTheme.ItemsInDungeon();

        //Assign the tiles to the 2d array grid
        for (int x = 0; x < m_cellsInDungeon.x; x++)
        {
            for (int y = 0; y < m_cellsInDungeon.y; y++)
            {
                p_gen.m_allCells[x, y].SetGridPosition(new Vector2Int(x, y));
                switch (p_gen.m_allCells[x, y].m_currentCellType)
                {
                case DungeonGridCell.CellType.Room:

                    DungeonGridCell roomCell = CreateRoom(p_gen, p_dungeonTheme, new Vector3Int(x * m_cellSize.x, y * m_cellSize.y, 0), p_gen.m_allCells[x, y], p_gen.m_allCells, ref dungeonGrid);
                    PopulateRoomWithItems(p_gen, p_dungeonTheme, roomCell.m_floorTiles, itemsInDungeon);
                    roomCell.m_roomIndex = returningRoomCells.Count;
                    returningRoomCells.Add(roomCell);
                    break;

                case DungeonGridCell.CellType.HallwayOneWay:
                    break;

                case DungeonGridCell.CellType.Hallway:
                    CreateCorridor(p_gen, p_dungeonTheme, Vector3Int.zero, p_gen.m_allCells[x, y].m_connectionPoints[0].m_connectionPos, p_gen.m_allCells[x, y], ref dungeonGrid);
                    break;

                case DungeonGridCell.CellType.None:
                    break;
                }
            }
        }


        //Create the room connections
        for (int x = 0; x < m_cellsInDungeon.x; x++)
        {
            for (int y = 0; y < m_cellsInDungeon.y; y++)
            {
                p_gen.m_allRooms.Add(p_gen.m_allCells[x, y]);
                foreach (ConnectionPoint connectedPoint in p_gen.m_allCells[x, y].m_connectionPoints)
                {
                    RoomConnections(p_gen, p_dungeonTheme, p_gen.m_allCells, p_gen.m_allCells[x, y], connectedPoint, ref dungeonGrid);
                }
            }
        }

        Vector2Int gridLength = new Vector2Int(dungeonGrid.GetLength(0), dungeonGrid.GetLength(1));

        //Draw the tiles on the tilemaps
        for (int x = -m_dungeonMapBounds.x; x < dungeonGrid.GetLength(0) + m_dungeonMapBounds.x; x++)
        {
            for (int y = -m_dungeonMapBounds.y; y < dungeonGrid.GetLength(1) + m_dungeonMapBounds.y; y++)
            {
                if (x < 0 || x > gridLength.x - 1 || y < 0 || y > gridLength.y - 1)
                {
                    p_gen.m_wallTiles.SetTile(new Vector3Int(x, y, 0), p_dungeonTheme.m_wallTile);
                    continue;
                }
                ///Wall


                if (dungeonGrid[x, y] == 0)
                {
                    p_gen.m_wallTiles.SetTile(new Vector3Int(x, y, 0), p_dungeonTheme.m_wallTile);
                }

                ///Floor
                else if (dungeonGrid[x, y] == 1)
                {
                    p_gen.m_floorTiles.SetTile(new Vector3Int(x, y, 0), p_dungeonTheme.m_floorTile);
                    p_gen.m_miniMapTiles.SetTile(new Vector3Int(x, y, 0), p_gen.m_miniMapTile);
                }
            }
        }

        p_dungeonNav.m_gridWorldSize = new Vector2(m_cellSize.x * m_cellsInDungeon.x, m_cellSize.y * m_cellsInDungeon.y);
        p_dungeonNav.m_gridOrigin    = p_dungeonNav.m_gridWorldSize / 2;
        p_dungeonNav.CreateGrid();
        return(returningRoomCells);
    }
Exemple #2
0
    private IEnumerator CheckDungeonConnection()
    {
        bool mapSuccess = false;

        while (!mapSuccess)
        {
            foreach (GameObject item in m_itemsOnFloor)
            {
                m_pooler.ReturnToPool(item);
            }
            m_itemsOnFloor.Clear();

            m_allRooms.Clear();
            m_hallwayPoints.Clear();
            m_wallTiles.ClearAllTiles();
            m_floorTiles.ClearAllTiles();
            m_miniMapTiles.ClearAllTiles();



            m_allRooms = m_dungeonTheme.CreateNewFloor(this, m_dungeonNav);
            m_currentEnemyTypesInDungeon = m_dungeonTheme.AiInDungeon();

            yield return(new WaitForEndOfFrame());

            m_dungeonNav.m_gridWorldSize = new Vector2(m_dungeonTheme.m_generationTypes[m_dungeonGenTypeIndex].m_cellSize.x * m_dungeonTheme.m_generationTypes[m_dungeonGenTypeIndex].m_cellsInDungeon.x, m_dungeonTheme.m_generationTypes[m_dungeonGenTypeIndex].m_cellSize.y * m_dungeonTheme.m_generationTypes[m_dungeonGenTypeIndex].m_cellsInDungeon.y);
            m_dungeonNav.m_gridOrigin    = m_dungeonNav.m_gridWorldSize / 2;
            m_dungeonNav.CreateGrid();
            Vector3 startPoint = m_allRooms[0].m_worldPos;
            bool    roomFailed = false;
            for (int i = 1; i < m_allRooms.Count; i++)
            {
                List <Node> path = m_gridTestAgent.CreatePath(startPoint, m_allRooms[i].m_worldPos);
                if (path == null)
                {
                    roomFailed = true;
                }
            }
            mapSuccess = !roomFailed;

            /*TODO
             * /// Currently, the path is drawn first, to create all the physical colliders, generate the navigation grid, and uses a* to determine if the path is possible.<br/>
             * /// But maybe its more efficient to utilize cell types isntead, so there are no physics and art parts to that.
             */


            /*if (roomFailed)
             * {
             *  m_floorTiles.color = Color.red;
             *  yield return new WaitForSeconds(.25f);
             * }
             * yield return new WaitForSeconds(.1f);
             * m_floorTiles.color = Color.white;*/
            //mapSuccess = false;
        }
        List <int> removeIndex = new List <int>();

        for (int i = 0; i < m_hallwayPoints.Count; i++)
        {
            if (m_hallwayPoints[i].m_connectedTo.Count == 0)
            {
                Debug.LogWarning("Hallway point: " + m_hallwayPoints[i].m_worldPos + " is not connected to anything. Removing");
                removeIndex.Add(i);
            }
        }
        removeIndex.Reverse();
        foreach (int i in removeIndex)
        {
            m_hallwayPoints.RemoveAt(i);
        }
        m_occupiedSpaces.Clear();

        Vector2 randomPos = RandomSpawnPosition(m_allRooms);

        m_playerObject.transform.position = randomPos;
        m_occupiedSpaces.Add(randomPos);

        randomPos = RandomSpawnPosition(m_allRooms);
        m_staircase.transform.position = randomPos;
        m_occupiedSpaces.Add(randomPos);

        m_playerInput.m_canPerform = true;
    }