Exemplo n.º 1
0
        static Corner GetOrAddCorner(ProximityMap <Corner> corners, Vector2f cornerPoint)
        {
            if (!corners.Find(cornerPoint, out KeyValuePair <Vector2f, Corner> corner, false))
            {
                corner                = corners.Add(cornerPoint, new Corner());
                corner.Value.Point    = cornerPoint;
                corner.Value.IsBorder =
                    corner.Value.Point.x <= 0.0001f ||
                    corner.Value.Point.x >= 0.9999f ||
                    corner.Value.Point.y <= 0.0001f ||
                    corner.Value.Point.y >= 0.9999f;
            }

            return(corner.Value);
        }
Exemplo n.º 2
0
        static IEnumerator BuildRegionGraph(Graph graph, WorldGeneratorData data, WorldGeneratorOptions options)
        {
            var regions = new ProximityMap <Region>(10, 1);
            var corners = new ProximityMap <Corner>(10, 1);
            var edges   = new List <Edge>();

            var coords = data.Voronoi.SiteCoords();

            for (int c = 0; c < coords.Count; c++)
            {
                var coord = coords[c];
                var site  = data.Voronoi.SitesIndexedByLocation[coord];

                var region = new Region
                {
                    Center = coord
                };

                var points = site.Region(data.VoronoiBounds);
                for (int p = 0; p < points.Count; p++)
                {
                    var point  = points[p];
                    var corner = GetOrAddCorner(corners, point);
                    corner.Regions.Add(region);
                    region.Corners.Add(corner);
                }

                regions.Add(region.Center, region);

                yield return(null);
            }

            csDelaunay.Edge voronoiEdge;
            for (var e = 0; e < data.Voronoi.Edges.Count; e++)
            {
                voronoiEdge = data.Voronoi.Edges[e];
                if (!voronoiEdge.Visible() || voronoiEdge.ClippedEnds == null)
                {
                    continue;
                }
                var leftCorner  = GetOrAddCorner(corners, voronoiEdge.ClippedEnds[LR.LEFT]);
                var rightCorner = GetOrAddCorner(corners, voronoiEdge.ClippedEnds[LR.RIGHT]);

                var edge = new Edge();
                edge.LeftCorner = leftCorner;
                edge.LeftCorner.Edges.Add(edge);
                edge.RightCorner = rightCorner;
                edge.RightCorner.Edges.Add(edge);

                if (voronoiEdge.LeftSite != null && regions.Find(voronoiEdge.LeftSite.Coord, out var leftRegion, false))
                {
                    edge.LeftRegion = leftRegion.Value;
                    edge.LeftRegion.Edges.Add(edge);
                }
                if (voronoiEdge.RightSite != null && regions.Find(voronoiEdge.RightSite.Coord, out var rightRegion, false))
                {
                    edge.RightRegion = rightRegion.Value;
                    edge.RightRegion.Edges.Add(edge);
                }

                if (edge.LeftRegion != null && edge.RightRegion != null)
                {
                    edge.LeftRegion.Neighbors.Add(edge.RightRegion);
                    edge.RightRegion.Neighbors.Add(edge.LeftRegion);
                }

                edge.LeftCorner.Neighbors.Add(edge.RightCorner);
                edge.RightCorner.Neighbors.Add(edge.LeftCorner);

                edges.Add(edge);

                yield return(null);
            }

            graph.Regions = new List <Region>();
            for (int r = 0; r < regions.Elements.Count; r++)
            {
                graph.Regions.Add(regions.Elements[r].Value);
            }

            graph.Corners = new List <Corner>();
            for (int c = 0; c < corners.Elements.Count; c++)
            {
                graph.Corners.Add(corners.Elements[c].Value);
            }

            graph.Edges = edges;
        }