Ejemplo n.º 1
0
    void RadialCheck()
    {
        List <int> toBeRemoved = new List <int>();
        List <int> toBeAdded   = new List <int>();

        foreach (int roomNumber in activeRooms)
        {
            int noOfRoomsGenerated = 0;
            for (int i = 0; i < 9; i++)
            {
                //If we have reached desired number of rooms, end
                if (totalNoOfRoomsGenerated >= totalNoOfRooms)
                {
                    break;
                }
                RoomGenerator roomGen = gridPoints[roomNumber].GetComponent <RoomGenerator>();

                bool canGenerateRoom = true;

                //The address of the other room in the gridPoints array
                int otherRoom = -1;
                //The position of this room relative to the newly generated room
                int j = -1;

                //If the room generator script says this position isn't valid, end
                if (roomGen.validRGPositions[i])
                {
                    //Setting otherRoom and j variables depending on room position
                    #region
                    switch (i)
                    {
                    case 0:
                        //Making sure a room doesn't get created diagonally if 2 already exist either side of that position
                        if (roomBool[roomNumber - 1] && roomBool[roomNumber - levelGridSize.x])
                        {
                            canGenerateRoom = false;
                        }

                        if (roomNumber - levelGridSize.x - 1 > 0 && roomNumber % levelGridSize.x != 0)
                        {
                            otherRoom = roomNumber - levelGridSize.x - 1;

                            j = 8;
                        }
                        break;

                    case 1:
                        if (roomNumber - levelGridSize.x > 0)
                        {
                            otherRoom = roomNumber - levelGridSize.x;

                            j = 7;
                        }
                        break;

                    case 2:
                        //Making sure a room doesn't get created diagonally if 2 already exist either side of that position
                        if (roomBool[roomNumber + 1] && roomBool[roomNumber - levelGridSize.x])
                        {
                            canGenerateRoom = false;
                        }

                        if (roomNumber - levelGridSize.x + 1 > 0 && roomNumber % levelGridSize.x != levelGridSize.x - 1)
                        {
                            otherRoom = roomNumber - levelGridSize.x + 1;

                            j = 6;
                        }
                        break;

                    case 3:
                        if (roomNumber - 1 > 0 && roomNumber % levelGridSize.x != 0)
                        {
                            otherRoom = roomNumber - 1;

                            j = 5;
                        }
                        break;

                    case 5:
                        if (roomNumber + 1 < gridPoints.Length && roomNumber % levelGridSize.x != levelGridSize.x - 1)
                        {
                            otherRoom = roomNumber + 1;

                            j = 3;
                        }
                        break;

                    case 6:
                        //Making sure a room doesn't get created diagonally if 2 already exist either side of that position
                        if (roomBool[roomNumber - 1] && roomBool[roomNumber + levelGridSize.x])
                        {
                            canGenerateRoom = false;
                        }

                        if (roomNumber + levelGridSize.x - 1 < gridPoints.Length && roomNumber % levelGridSize.x != 0)
                        {
                            otherRoom = roomNumber + levelGridSize.x - 1;

                            j = 2;
                        }
                        break;

                    case 7:
                        if (roomNumber + levelGridSize.x < gridPoints.Length)
                        {
                            otherRoom = roomNumber + levelGridSize.x;

                            j = 1;
                        }
                        break;

                    case 8:
                        //Making sure a room doesn't get created diagonally if 2 already exist either side of that position
                        if (roomBool[roomNumber + 1] && roomBool[roomNumber + levelGridSize.x])
                        {
                            canGenerateRoom = false;
                        }

                        if (roomNumber + levelGridSize.x + 1 < gridPoints.Length && roomNumber % levelGridSize.x != levelGridSize.x - 1)
                        {
                            otherRoom = roomNumber + levelGridSize.x + 1;

                            j = 0;
                        }
                        break;

                    default:

                        break;
                    }
                    #endregion

                    //If valid room was selected
                    if (otherRoom >= 0 && j >= 0 && otherRoom != 4)
                    {
                        RoomGenerator otherRoomGen = gridPoints[otherRoom].GetComponent <RoomGenerator>();

                        if (roomBool[otherRoom])
                        {
                            //Telling this room that there is a room in the i position
                            roomGen.SetAdjacentRoomBool(i, true);
                            //Telling other room that there is a room in the j position
                            otherRoomGen.SetAdjacentRoomBool(j, true);
                        }

                        if (Roll(chanceToCreateRoom) && canGenerateRoom)
                        {
                            bool createdCorridor = false;

                            //If there is no room or corridor occupying this space
                            if (!roomBool[otherRoom] && !corridorBool[otherRoom])
                            {
                                //Generating room in position i
                                GenerateRoom(otherRoom, j);
                                roomBool[otherRoom] = true;

                                //Telling this room that there is a room in the i position
                                roomGen.SetAdjacentRoomBool(i, true);
                                //Telling other room that there is a room in the j position
                                otherRoomGen.SetAdjacentRoomBool(j, true);

                                //Choosing the shape of the room
                                otherRoomGen.ChooseSmallRoomLayout(j);
                                //Setting distance from start
                                otherRoomGen.SetDistanceFromStart(roomGen.GetDistanceFromStart() + 1);

                                //Create Corridor to new room
                                StartCoroutine(CreateCorridor(roomNumber, i));
                                createdCorridor = true;

                                //Storing this room number to be added later to active rooms
                                toBeAdded.Add(otherRoom);

                                totalNoOfRoomsGenerated++;

                                //Note room has been generated
                                noOfRoomsGenerated++;
                            }

                            //Generate corridor if room already exists
                            if (!roomGen.CheckForCorridor(i) && roomBool[otherRoom] && !createdCorridor)
                            {
                                if (Roll(chanceToCreateCorridor) && otherRoomGen.validRGPositions[j])
                                {
                                    StartCoroutine(CreateCorridor(roomNumber, i));

                                    //Set the new distances from start
                                    if (otherRoomGen.GetDistanceFromStart() < roomGen.GetDistanceFromStart())
                                    {
                                        roomGen.SetDistanceFromStart(otherRoomGen.GetDistanceFromStart() + 1);
                                    }
                                    else
                                    {
                                        otherRoomGen.SetDistanceFromStart(roomGen.GetDistanceFromStart() + 1);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //If this room has generated another room, replace it on the active list with the new room(s)
            if (noOfRoomsGenerated > 0)
            {
                toBeRemoved.Add(roomNumber);
            }
        }

        //Removing rooms from active list and adding to passive
        foreach (int room in toBeRemoved.ToArray())
        {
            activeRooms.Remove(room);
            existingRooms.Add(room);
        }
        toBeRemoved.Clear();


        //Adding newly generated rooms to active list
        foreach (int room in toBeAdded.ToArray())
        {
            activeRooms.Add(room);
        }
        toBeAdded.Clear();
    }