Esempio n. 1
0
    private void Generate()
    {
        // create initial room point
        // always will be room 1
        Room startRoom = new Room("Rooms/room1");

        startRoom.MinimapRoomCoordinates = new Vector2((float)Math.Round((double)(maxWidth / 2)), (float)Math.Round((double)(maxHeight / 2)));    // start in middle
        startRoom.Visited = true;
        RoomList.Add(startRoom);
        _currentAmtOfRooms++;

        // set some properties
        // tile size is 32x32.  We need to adjust room width and height so we don't get the extra space around the room that holds
        // the collisions for the passages.
        RoomWidth  = startRoom.GetRoomWidth() - (2 * tileSize);
        RoomHeight = startRoom.GetRoomHeight() - (2 * tileSize);

        while (true)
        {
            // out of rooms
            if (_maxRooms - _currentAmtOfRooms <= 0)
            {
                break;
            }
            if (_currentRoomIndex >= RoomList.Count)
            {
                break;
            }

            CurrentRoom = RoomList[_currentRoomIndex];
            // randomly choose amount of directions to branch depending on which
            // is less: max rooms left to place or connections possible for room
            //int maxPossibleConnections = Math.Min(_maxRooms - _currentAmtOfRooms, CurrentRoom.GetPossibleConnectionCount(RoomList));
            int maxPossibleConnections = Math.Min(_maxRooms - _currentAmtOfRooms, GetPossibleConnectionsCount());

            // if we can't connect, go to next room
            if (maxPossibleConnections <= 0)
            {
                _currentRoomIndex++;
                continue;
            }

            // generated max amount of rooms
            if (maxPossibleConnections == 0)
            {
                break;
            }

            int todoConnections = XeldaGame.rand.Next(1, maxPossibleConnections + 1);
            // for each needed room, generate and connect the rooms
            for (int i = 0; i < todoConnections; i++)
            {
                Direction dir        = GetRandomDirectionForConnection(CurrentRoom);
                int       roomNumber = XeldaGame.rand.Next(1, _maxRoomTemplates + 1);
                Room      room       = new Room("Rooms/room" + roomNumber, dir, RoomList.IndexOf(CurrentRoom));
                room.Visited = false;
                SetDebugRoomPosition(CurrentRoom, room, dir);
                RoomList.Add(room);
                _currentAmtOfRooms++;
                CurrentRoom.ConnectThisToNewRoom(dir, RoomList.IndexOf(room));
            }

            // treat list like queue and go to next node and build off of that.
            _currentRoomIndex++;
        }

        // set CurrentRoom back to 0 so player starts at first node.
        CurrentRoom = RoomList[0];

        // first room of dungeon should have no mobs
        CurrentRoom.ClearMobsFromRoom();

        // tell each room to add walls to block passages that have no adjacent room to connect
        foreach (Room room in RoomList)
        {
            room.AddPassageWalls();
        }
    }