예제 #1
0
        /// <summary>
        /// Creates a new <see cref="GameLevel"/> instance from an enumeration of vertices loaded
        /// from a saved game file.
        /// </summary>
        /// <param name="savedVertices">An enumeration of vertices loaded from the saved game file.
        /// </param>
        /// <returns>The created game level instance.</returns>
        /// <remarks>
        /// <para>The loaded vertices' positions are preserved during the game level's
        /// initialization.</para>
        /// </remarks>
        public static GameLevel Create(IEnumerable<Saves.Vertex> savedVertices)
        {
            var vertexMappings = new Dictionary<int, Vertex>();
            var lineSegments = new List<LineSegment>();
            foreach (Saves.Vertex savedVertex in savedVertices)
            {
                var vertex = new Vertex();
                vertex.SetPosition(new Point(savedVertex.X, savedVertex.Y));
                vertexMappings[savedVertex.Id] = vertex;
                foreach (int connectedVertexId in savedVertex.ConnectedVertexIds)
                {
                    if (!vertexMappings.ContainsKey(connectedVertexId))
                    {
                        // The connected vertex has not been mapped to a vertex view model yet;
                        // skip this line segment for now, it will be added when the other vertex
                        // is enumerated
                        continue;
                    }

                    Vertex otherVertex = vertexMappings[connectedVertexId];
                    LineSegment lineSegment = vertex.ConnectToVertex(otherVertex);
                    lineSegments.Add(lineSegment);
                }
            }

            return new GameLevel(vertexMappings.Values, lineSegments, false);
        }