using System.Collections.Generic; using UnityEngine; namespace SuperTiled2Unity.Editor { public static class PolygonUtils { // Group convex polygons into a composite collider public static void AddCompositePolygonCollider(GameObject go, List convexPolygons, SuperImportContext context) { // If there is only one convex polygon then don't use a composite if (convexPolygons.Count == 1) { var polyCollider = go.AddComponent(); polyCollider.SetPath(0, convexPolygons[0]); polyCollider.gameObject.AddComponent(); } else { // Rigid body is needed for composite collider var rigid = go.AddComponent(); rigid.bodyType = RigidbodyType2D.Static; rigid.simulated = true; // Colliders will be grouped by the composite // This way we have convex polygon paths (in the children) if needed // And we can have complex polygons represented by one object var composite = go.AddComponent(); composite.geometryType = ST2USettings.instance.m_CollisionGeometryType; composite.generationType = CompositeCollider2D.GenerationType.Manual; // Add polygon colliders foreach (var path in convexPolygons) { var goPolygon = new GameObject("ConvexPolygon"); go.AddChildWithUniqueName(goPolygon); var polyCollider = goPolygon.AddComponent(); polyCollider.SetMergeWithComposite(true); polyCollider.SetPath(0, path); polyCollider.gameObject.AddComponent(); } composite.ST2UGeneratePolygonGeometry(); } } // Postive value == CW, negative == CCW public static float SumOverEdges(Vector2[] points) { float sum = 0; for (int i = 0; i < points.Length; i++) { int j = (i + 1) % points.Length; float dx = points[j].x - points[i].x; float dy = points[j].y + points[i].y; sum += dx * dy; } return sum; } } }