示例#1
0
        /// <summary>
        /// Builds a rectangular room of the specified dimensions.
        /// </summary>
        /// <param name="builder">The level builder.</param>
        /// <param name="upperLeft">The absolute position of the upper left point of the room.</param>
        /// <param name="size">The width and height of the room.</param>
        /// <returns>The unique identifier of the room.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// size - The X size of a constructed room must be greater than zero.
        /// or
        /// size - The Y size of a constructed room must be greater than zero.
        /// </exception>
        /// <exception cref="System.ArgumentOutOfRangeException">size - The X and Y sizes of a constructed room must each be greater than zero.</exception>
        public Guid AddRectangularRoom(LevelBuilder builder, Pos2D upperLeft, Pos2D size)
        {
            // Validate
            if (size.X <= 0 || size.Y <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(size),
                                                      "The X and Y sizes of a constructed room must be greater than zero.");
            }

            var roomGuid = Guid.NewGuid();

            // Loop through the bounds and add the requested room
            for (var x = upperLeft.X; x <= upperLeft.X + size.X - 1; x++)
            {
                for (var y = upperLeft.Y; y <= upperLeft.Y + size.Y - 1; y++)
                {
                    var isOnEdge = x == upperLeft.X ||
                                   x == upperLeft.X + size.X - 1 ||
                                   y == upperLeft.Y ||
                                   y == upperLeft.Y + size.Y - 1;

                    char terrain = isOnEdge ? '#' : '.';

                    var cell = builder.BuildCell(terrain, new Pos2D(x, y));
                    builder.AddCell(cell, roomGuid);
                }
            }

            return(roomGuid);
        }