Exemplo n.º 1
0
    private void GenerateFloorLayout()
    {
        var coordinates = new RoomCoordinates(Random.Range(1, 4), Random.Range(1, 4));
        var firstRoom   = CreateFirstRoom(coordinates);

        //No rooms were created yet
        int numberOfRoomsCreated = CreateBranch(firstRoom, coordinates, 0);

        /*
         * if (Random.Range(0.0f, 1.0f) <= _branchingProbability) {
         *      numberOfRoomsCreated = CreateBranch(firstRoom, coordinates, numberOfRoomsCreated);
         * }
         */
        var previousRoom = firstRoom;

        if (level == maxLevels)
        {
            numberOfRooms += 1;
        }

        while (numberOfRoomsCreated < numberOfRooms - 1)
        {
            RoomDirection direction;
            try {
                direction = DetermineNextRoomLocation(coordinates);
            } catch (Exception ex) {
                previousRoom = _floorGrid.Rooms.FirstOrDefault(r => _floorGrid.GetValidDirectionsFromRoom(r).Any());

                if (previousRoom == null)
                {
                    throw new Exception("Everything is ruined", ex);
                }
                direction   = DetermineNextRoomLocation(_floorGrid.GetCoordinatesForRoom(previousRoom));
                coordinates = _floorGrid.GetCoordinatesForRoom(previousRoom);
            }

            coordinates = DetermineNewCoordinates(direction, coordinates);

            if (!_floorGrid.IsDeadEnd(coordinates.X, coordinates.Y))
            {
                previousRoom = AddNewRoom(previousRoom, direction, coordinates);
                numberOfRoomsCreated++;
            }

            if (numberOfRoomsCreated < numberOfRooms - 1 && Random.Range(0.0f, 1.0f) <= _branchingProbability)
            {
                numberOfRoomsCreated = CreateBranch(previousRoom, coordinates, numberOfRoomsCreated);
            }
        }

        if (!_floorGrid.GetValidDirectionsFromRoom(previousRoom).Any())
        {
            var validRooms = _floorGrid.Rooms.Where(
                r => r != firstRoom && _floorGrid.GetValidDirectionsFromRoom(r).Any()).ToList();
            previousRoom = validRooms.ElementAt(Random.Range(0, validRooms.Count));

            coordinates = _floorGrid.GetCoordinatesForRoom(previousRoom);
        }

        // Boss room should not appear at the last level
        if (level != maxLevels)
        {
            CreateBossRoom(previousRoom, coordinates, RoomType.BossRoom);
        }
        else
        {
            CreateBossRoom(previousRoom, coordinates, RoomType.AngelRoom);
            CreateBossRoom(previousRoom, coordinates, RoomType.DevilRoom);
        }

        if (!_floorGrid.GetValidDirectionsFromRoom(previousRoom).Any())
        {
            var validRooms = _floorGrid.Rooms.Where(
                r => r._roomType == RoomType.NormalRoom && _floorGrid.GetValidDirectionsFromRoom(r).Any()).ToList();
            previousRoom = validRooms.ElementAt(Random.Range(0, validRooms.Count));

            coordinates = _floorGrid.GetCoordinatesForRoom(previousRoom);
        }
    }
Exemplo n.º 2
0
    private void GenerateFloorLayout()
    {
        var coordinates = new RoomCoordinates(Random.Range(1, 4), Random.Range(1, 4));
        var firstRoom   = CreateFirstRoom(coordinates);

        //Create branch for the first room
        int numberOfRoomsCreated = CreateBranch(firstRoom, coordinates, 0);

        if (Random.Range(0.0f, 1.0f) <= BranchingProbability)
        {
            Debug.Log("Branching!");
            numberOfRoomsCreated = CreateBranch(firstRoom, coordinates, numberOfRoomsCreated);
        }

        var previousRoom = firstRoom;

        while (numberOfRoomsCreated < _numberOfRooms - 1)
        {
            RoomDirection direction;
            try
            {
                direction = DetermineNextRoomLocation(coordinates);
            }
            catch (Exception ex)
            {
                previousRoom =
                    _floorGrid.Rooms.FirstOrDefault(r => _floorGrid.GetValidDirectionsFromRoom(r).Any());
                if (previousRoom == null)
                {
                    throw new Exception("Everything is ruined", ex);
                }
                direction   = DetermineNextRoomLocation(_floorGrid.GetCoordinatesForRoom(previousRoom));
                coordinates = _floorGrid.GetCoordinatesForRoom(previousRoom);
            }

            coordinates = DetermineNewCoordinates(direction, coordinates);

            if (!_floorGrid.IsDeadEnd(coordinates.X, coordinates.Y))
            {
                previousRoom = AddNewRoom(previousRoom, direction, coordinates);
                numberOfRoomsCreated++;
            }
            if (numberOfRoomsCreated < _numberOfRooms - 1 && Random.Range(0.0f, 1.0f) <= BranchingProbability)
            {
                Debug.Log("Branching!");
                numberOfRoomsCreated = CreateBranch(previousRoom, coordinates, numberOfRoomsCreated);
            }
            //if (Random.Range(0.0f, 1.0f) >= BranchingProbability*BranchingProbability)
            //{
            //    Debug.Log("Branching again!");
            //    numberOfRoomsCreated = CreateBranch(previousRoom, coordinates, numberOfRoomsCreated);
            //}
        }
        if (!_floorGrid.GetValidDirectionsFromRoom(previousRoom).Any())
        {
            var validRooms = _floorGrid.Rooms.Where(
                r => r != firstRoom && _floorGrid.GetValidDirectionsFromRoom(r).Any()).ToList();
            previousRoom = validRooms.ElementAt(Random.Range(0, validRooms.Count));

            coordinates = _floorGrid.GetCoordinatesForRoom(previousRoom);
        }

        CreateBossRoom(previousRoom, coordinates);

        if (!_floorGrid.GetValidDirectionsFromRoom(previousRoom).Any())
        {
            var validRooms = _floorGrid.Rooms.Where(
                r => r.RoomType == RoomType.NormalRoom && _floorGrid.GetValidDirectionsFromRoom(r).Any()).ToList();
            previousRoom = validRooms.ElementAt(Random.Range(0, validRooms.Count));

            coordinates = _floorGrid.GetCoordinatesForRoom(previousRoom);
        }
        var dir = _floorGrid.GetValidDirectionsFromRoom(coordinates).First();

        AddNewRoom(previousRoom, dir, DetermineNewCoordinates(dir, coordinates), RoomType.TreasureRoom);
    }