示例#1
0
        // Generate a new map that places rooms randomly
        public Maps.Map CreateMap()
        {
            // Set the properties of all cells to false
            _map.Initialize(_width, _height);

            // Try to place as many rooms as the specified maxRooms
            // Note: Only using decrementing loop because of WordPress formatting
            for (int r = _maxRooms; r > 0; r--)
            {
                // Determine the size and position of the room randomly
                int roomWidth     = Program.Random.Next(_roomMinSize, _roomMaxSize);
                int roomHeight    = Program.Random.Next(_roomMinSize, _roomMaxSize);
                int roomXPosition = Program.Random.Next(0, _width - roomWidth - 1);
                int roomYPosition = Program.Random.Next(0, _height - roomHeight - 1);

                // All of our rooms can be represented as Rectangles
                var newRoomBox = new Rectangle(roomXPosition, roomYPosition, roomWidth, roomHeight);
                var newRoom    = new Room(newRoomBox);

                // Check to see if the room rectangle intersects with any other rooms
                bool newRoomIntersects = _map.Rooms.Any(room => newRoom.Box.Intersects(room.Box));

                // As long as it doesn't intersect add it to the list of rooms
                if (!newRoomIntersects)
                {
                    _map.Rooms.Add(newRoom);
                }
            }
            // Iterate through each room that we wanted placed
            // call CreateRoom to make it
            foreach (Room room in _map.Rooms)
            {
                CreateRoom(room);
            }

            // Iterate through each room that was generated
            // Don't do anything with the first room, so start at r = 1 instead of r = 0
            for (int r = 1; r < _map.Rooms.Count; r++)
            {
                // For all remaing rooms get the center of the room and the previous room
                int previousRoomCenterX = _map.Rooms[r - 1].Box.Center.X;
                int previousRoomCenterY = _map.Rooms[r - 1].Box.Center.Y;
                int currentRoomCenterX  = _map.Rooms[r].Box.Center.X;
                int currentRoomCenterY  = _map.Rooms[r].Box.Center.Y;

                // Give a 50/50 chance of which 'L' shaped connecting hallway to tunnel out
                if (Program.Random.Next(1, 2) == 1)
                {
                    CreateHorizontalTunnel(previousRoomCenterX, currentRoomCenterX, previousRoomCenterY);
                    CreateVerticalTunnel(previousRoomCenterY, currentRoomCenterY, currentRoomCenterX);
                }
                else
                {
                    CreateVerticalTunnel(previousRoomCenterY, currentRoomCenterY, previousRoomCenterX);
                    CreateHorizontalTunnel(previousRoomCenterX, currentRoomCenterX, currentRoomCenterY);
                }
            }

            return(_map);
        }