Exemplo n.º 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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds the cells from prefab to the level builder.
        /// </summary>
        /// <param name="builder">The level builder.</param>
        /// <param name="upperLeft">The upper left position of the prefab within the level.</param>
        /// <param name="flipX">if set to <c>true</c> the prefab will be flipped horizontally.</param>
        /// <param name="flipY">if set to <c>true</c> the prefab will be flipped vertically.</param>
        /// <param name="prefabGuid">The prefab instance's unique identifier.</param>
        /// <param name="prefab">The prefab.</param>
        /// <param name="prefabDimensions">The prefab dimensions.</param>
        private static void AddCellsFromPrefab(LevelBuilder builder,
                                               Pos2D upperLeft,
                                               bool flipX,
                                               bool flipY,
                                               Guid prefabGuid,
                                               PrefabData prefab,
                                               Pos2D prefabDimensions)
        {
            // Loop over the rows in the prefab
            int y = 0;

            foreach (var row in prefab.Data)
            {
                // Loop over the cells in the prefab
                int x = 0;
                foreach (var c in row)
                {
                    // Do nothing if there's nothing assignable for the cell
                    if (c != ' ')
                    {
                        Pos2D cellPos =
                            CalculatePrefabCellPosition(new Pos2D(x, y), upperLeft, prefabDimensions, flipY, flipX);

                        var cell = builder.BuildPrefabCell(c, cellPos, prefab);

                        // Respect the IsInvulnerable flag on prefabs
                        if (prefab.IsInvulnerable)
                        {
                            foreach (var wall in cell.Objects.Where(o => o.ObjectType == GameObjectType.Wall))
                            {
                                wall.SetInvulnerable();
                            }
                        }

                        builder.AddCell(cell, prefabGuid);
                    }

                    x++;
                }

                y++;
            }
        }