Esempio n. 1
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++;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Adds a prefab room to a level under construction.
        /// </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="prefab">The prefab to add</param>
        /// <param name="flipX">if set to <c>true</c> the prefab will be flipped horizontally before being applied.</param>
        /// <param name="flipY">if set to <c>true</c> the prefab will be flipped vertically before being applied.</param>
        /// <returns>A unique identifier for the prefab, used for encounter generation.</returns>
        public Guid AddPrefab(LevelBuilder builder,
                              Pos2D upperLeft,
                              PrefabData prefab,
                              bool flipX = false,
                              bool flipY = false)
        {
            // Validate that the prefab was identified.
            if (prefab == null)
            {
                throw new ArgumentNullException(nameof(prefab));
            }

            // Come up with a distinct prefab ID for this particular prefab instance
            var prefabGuid = Guid.NewGuid();

            // Declare loop formulae here so they don't have to be recomputed for every cell
            Pos2D prefabDimensions = new Pos2D(prefab.Data.Max(c => c.Length), prefab.Data.Count);

            AddCellsFromPrefab(builder, upperLeft, flipX, flipY, prefabGuid, prefab, prefabDimensions);

            return(prefabGuid);
        }