コード例 #1
0
        /// <summary>
        /// Creates a level out of the current state and returns that <see cref="LevelData"/> instance.
        /// </summary>
        /// <returns>LevelData.</returns>
        /// <exception cref="InvalidOperationException">Cannot finalize a level if no cells have been added</exception>
        public LevelData CreateLevel()
        {
            if (!Cells.Any())
            {
                throw new InvalidOperationException("Cannot finalize a level if no cells have been added");
            }

            // Build out the level object
            var level = new LevelData
            {
                Id          = LevelId,
                Name        = LevelName,
                PlayerStart = PlayerStart
            };

            // Copy over the cells into the level
            foreach (var cell in Cells)
            {
                level.AddCell(cell);
            }

            // Set the coordinates of the map based on the cells present
            level.UpperLeft  = new Pos2D(level.Cells.Min(c => c.Pos.X), level.Cells.Min(c => c.Pos.Y));
            level.LowerRight = new Pos2D(level.Cells.Max(c => c.Pos.X), level.Cells.Max(c => c.Pos.Y));

            // Mark command requires that the mark pos default to the start location
            level.MarkedPos = PlayerStart;

            // Ensure all walls are marked external that should be
            FinalizePlacedWalls(level);

            // Loop over all door cells and ensure that they have floors without walls on the other side of them.
            FinalizeDoors(level);

            FinalizeFloors(level);

            GenerateActors(level);

            return(level);
        }