Beispiel #1
0
        public static MiniMapHullData ConstructLinkedHulls(Hull mainHull, HashSet <Hull> linkedHulls, GUIComponent parent, Rectangle worldBorders)
        {
            Rectangle parentRect = parent.Rect;

            Dictionary <Hull, Rectangle> rects = new Dictionary <Hull, Rectangle>();
            Rectangle worldRect = mainHull.WorldRect;

            worldRect.Y = -worldRect.Y;

            rects.Add(mainHull, worldRect);

            foreach (Hull hull in linkedHulls)
            {
                Rectangle rect = hull.WorldRect;
                rect.Y = -rect.Y;

                worldRect = Rectangle.Union(worldRect, rect);
                rects.Add(hull, rect);
            }

            worldRect.Y = -worldRect.Y;

            List <RectangleF> normalizedRects = new List <RectangleF>();
            List <Hull>       hullRefs        = new List <Hull>();

            foreach (var(hull, rect) in rects)
            {
                Rectangle wRect = rect;
                wRect.Y = -wRect.Y;

                var(posX, posY) = new Vector2(
                    (wRect.X - worldBorders.X) / (float)worldBorders.Width,
                    (worldBorders.Y - wRect.Y) / (float)worldBorders.Height);

                var(scaleX, scaleY) = new Vector2(wRect.Width / (float)worldBorders.Width, wRect.Height / (float)worldBorders.Height);

                RectangleF newRect = new RectangleF(posX * parentRect.Width, posY * parentRect.Height, scaleX * parentRect.Width, scaleY * parentRect.Height);

                normalizedRects.Add(newRect);
                hullRefs.Add(hull);
            }

            ImmutableArray <RectangleF> snappedRectangles = ToolBox.SnapRectangles(normalizedRects, treshold: 1);

            List <List <Vector2> > polygon = ToolBox.CombineRectanglesIntoShape(snappedRectangles);

            List <List <Vector2> > scaledPolygon = new List <List <Vector2> >();

            foreach (List <Vector2> list in polygon)
            {
                var(polySizeX, polySizeY) = ToolBox.GetPolygonBoundingBoxSize(list);
                float sizeX = polySizeX - 1f,
                      sizeY = polySizeY - 1f;

                scaledPolygon.Add(ToolBox.ScalePolygon(list, new Vector2(sizeX / polySizeX, sizeY / polySizeY)));
            }

            return(new MiniMapHullData(scaledPolygon, worldRect, parentRect.Size, snappedRectangles, hullRefs.ToImmutableArray()));
        }