示例#1
0
        public void TriangulateLevel()
        {
            vg = Undo.AddComponent <VertexGraph>(gameObject);

            // //For the purposes of inserting a level manually through a list of preset Vector2 vars.
            // Vector2 [] vertices = new Vector2[] { <Insert Vector2s> };
            // vg.AddPolygon (vertices);
            // // Subsequent adding of obstacles is done through listing it as a separate list of vertices

            // Gets the polygon objects out of the level component, from GRTK. If using a different method of supplying the polygon, this needs to be changed.
            GRTK.Polygon [] polyArray = level.GetComponents <GRTK.Polygon>();
            // adds all the polygons from the GRTK to the vertex graph. Starts at 1 rather than 0 because the outermost polygon in the GRTK method of level building is not part of the level.
            for (int i = 1; i < polyArray.Length; i++)
            {
                vg.AddPolygon(polyArray[i].GetRaw2());
            }

            // First, need to draw the lines between vertices, granted they don't intersect any other lines (boundaries or drawn)
            foreach (Vertex start in vg.GetVertices())
            {
                foreach (Vertex end in vg.GetVertices())
                {
                    Line possibleEdge = new Line(start, end);
                    if (vg.IntersectsNothing(possibleEdge) && !end.Equals(start))
                    {
                        vg.DrawEdge(possibleEdge);
                    }
                }
            }

            // Now, to delete all lines which are either in obstacles or exterior to the level.
            // If a point on a drawn line which has an infinite line drawn off in some direction has an even number of intersections with
            // the boundaries of the polygon, it is either outside of it or in an obstacle. This completes triangulation.

            foreach (Line line in vg.GetDrawnEdges())
            {
                if (NumIntersects(line, vg) % 2 == 0)
                {
                    vg.EraseEdge(line);
                }
            }

            // Next, to calculate angles for and sort the edges stemming from each vertex, now that they're "finalized" for the purposes of triangulation.
            foreach (Vertex v in vg.GetVertices())
            {
                v.SortEdges();
            }

            vg.createRegions();
        }
示例#2
0
 public void DecomposeLevel()
 {
     // So in order to combine two regions and add the combined one, we need to take care of a few bookeeping things.
     // Remove boundary line.
     // Remove both original regions.
     // Add the new one.
     // This needs to be done for each drawn line in the vg.
     foreach (Line l in vg.GetDrawnEdges())
     {
         Region r = new Region(l.cw, l.ccw, l);
         if (r.IsConvex())
         {
             vg.RemoveRegion(l.cw); vg.RemoveRegion(l.ccw); vg.EraseEdge(l);
             vg.AddRegion(r);
         }
     }
 }