예제 #1
0
        /// <summary>
        /// Creates a new <see cref="GameLevel"/> instance from an enumeration of vertices
        /// generated by a <see cref="Generation.LevelGenerator"/> instance.
        /// </summary>
        /// <param name="generatedVertices">An enumeration of vertices generated by the game level
        /// generator.</param>
        /// <returns>The created game level instance.</returns>
        /// <remarks>
        /// <para>The generated vertices are automatically arranged in a circle in random order
        /// during the game level's initialization.</para>
        /// </remarks>
        public static GameLevel Create(IEnumerable<Generation.Vertex> generatedVertices)
        {
            var vertexMappings = new Dictionary<Generation.Vertex, Vertex>();
            var lineSegments = new List<LineSegment>();
            foreach (Generation.Vertex generatedVertex in generatedVertices)
            {
                var vertex = new Vertex();
                vertexMappings[generatedVertex] = vertex;
                foreach (Generation.Vertex connectedGeneratedVertex in generatedVertex.ConnectedVertices)
                {
                    if (!vertexMappings.ContainsKey(connectedGeneratedVertex))
                    {
                        // 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[connectedGeneratedVertex];
                    LineSegment lineSegment = vertex.ConnectToVertex(otherVertex);
                    lineSegments.Add(lineSegment);
                }
            }

            return new GameLevel(vertexMappings.Values, lineSegments, true);
        }
예제 #2
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);
        }