private void CollectTilePolygons()
        {
            if (!m_Tile.m_CollisionObjects.IsEmpty())
            {
                foreach (var collision in m_Tile.m_CollisionObjects)
                {
                    var tilePoly = new TilePolygon();
                    tilePoly.IsClosed          = collision.IsClosed;
                    tilePoly.ColliderLayerName = collision.m_PhysicsLayer;
                    tilePoly.ColliderLayerId   = LayerMask.NameToLayer(collision.m_PhysicsLayer);
                    tilePoly.IsTrigger         = collision.m_IsTrigger;

                    var points = m_ImportContext.MakePointsPPU(collision.Points);
                    points = points.Select(pt => (Vector2)m_Transform.MultiplyPoint(pt)).ToArray();

                    // Make sure the polygon points order is still CCW. Otherwise clipper may subtract polygons from each other.
                    if (PolygonUtils.SumOverEdges(points) < 0)
                    {
                        points = points.Reverse().ToArray();
                    }

                    tilePoly.Points = points;

                    m_Polygons.Add(tilePoly);
                }
            }
        }
        private static void AddPolygonCollider(GameObject go, CollisionObject collision, SuperTile tile, SuperImportContext importContext)
        {
            // Note that polygons may need to be decomposed into convex parts
            var points = importContext.MakePointsPPU(collision.Points);

            // Triangulate the polygon points
            var triangulator = new Triangulator();
            var triangles    = triangulator.TriangulatePolygon(points);

            // Gather triangles into a collection of convex polygons
            var composition    = new ComposeConvexPolygons();
            var convexPolygons = composition.Compose(triangles);

            PolygonUtils.AddCompositePolygonCollider(go, convexPolygons);
        }
Ejemplo n.º 3
0
        private void ProcessPolygonElement(GameObject goObject, XElement xPolygon)
        {
            // Get the points of the polygon so we can decompose into a collection of convex polygons
            var points = xPolygon.GetAttributeAsVector2Array("points");

            points = points.Select(p => ColliderFactory.TransformPoint(p)).ToArray();
            points = Importer.SuperImportContext.MakePoints(points);

            // Triangulate the polygon points
            var triangulator = new Triangulator();
            var triangles    = triangulator.TriangulatePolygon(points);

            // Gather triangles into a collection of convex polygons
            var composition    = new ComposeConvexPolygons();
            var convexPolygons = composition.Compose(triangles);

            PolygonUtils.AddCompositePolygonCollider(goObject, convexPolygons);
        }
        private static void AddPolygonCollider(GameObject go, CollisionObject collision, SuperTile tile, SuperImportContext importContext)
        {
            // Note that polygons may need to be decomposed into convex parts
            var points = importContext.MakePoints(collision.Points);

            // Triangulate the polygon points
            var triangulator = new Triangulator();
            var triangles    = triangulator.TriangulatePolygon(points);

            // Gather triangles into a collection of convex polygons
            var composition    = new ComposeConvexPolygons();
            var convexPolygons = composition.Compose(triangles);

            PolygonUtils.AddCompositePolygonCollider(go, convexPolygons);

            // Position is from top-left corner
            float height = importContext.MakeScalar(tile.m_Height);

            go.transform.localPosition = new Vector3(0, height, 0);
        }
Ejemplo n.º 5
0
        private void ProcessObjectGroupElement(SuperTile tile, XElement xObjectGroup)
        {
            // Object groups on tiles come from the Tiled Collision Editor
            if (xObjectGroup.Elements().Any())
            {
                // We'll be adding collision objects to our tile
                tile.m_CollisionObjects = new List <CollisionObject>();

                foreach (var xObject in xObjectGroup.Elements("object"))
                {
                    var collision = new CollisionObject();

                    // Template may fill in a bunch of properties
                    m_Importer.ApplyTemplateToObject(xObject);

                    collision.m_ObjectId   = xObject.GetAttributeAs("id", 0);
                    collision.m_ObjectName = xObject.GetAttributeAs("name", string.Format("Object_{0}", collision.m_ObjectId));
                    collision.m_ObjectType = xObject.GetAttributeAs("type", "");
                    collision.m_Position.x = xObject.GetAttributeAs("x", 0.0f);
                    collision.m_Position.y = xObject.GetAttributeAs("y", 0.0f);
                    collision.m_Size.x     = xObject.GetAttributeAs("width", 0.0f);
                    collision.m_Size.y     = xObject.GetAttributeAs("height", 0.0f);
                    collision.m_Rotation   = xObject.GetAttributeAs("rotation", 0.0f);

                    // Are there any properties on the collision object?
                    collision.m_CustomProperties = CustomPropertyLoader.LoadCustomPropertyList(xObject.Element("properties"));
                    collision.m_CustomProperties.AddPropertiesFromType(collision.m_ObjectType, m_Importer.SuperImportContext);

                    var xPolygon  = xObject.Element("polygon");
                    var xPolyline = xObject.Element("polyline");

                    if (xPolygon != null)
                    {
                        // Get points and make sure they are CCW
                        var points = xPolygon.GetAttributeAsVector2Array("points");

                        if (PolygonUtils.SumOverEdges(points) < 0)
                        {
                            points = points.Reverse().ToArray();
                        }

                        collision.MakePointsFromPolygon(points);
                    }
                    else if (xPolyline != null)
                    {
                        // Get points and make sure they are CCW
                        var points = xPolyline.GetAttributeAsVector2Array("points");

                        if (PolygonUtils.SumOverEdges(points) < 0)
                        {
                            points = points.Reverse().ToArray();
                        }

                        collision.MakePointsFromPolyline(points);
                    }
                    else if (xObject.Element("ellipse") != null)
                    {
                        if (collision.m_Size.x == 0)
                        {
                            m_Importer.ReportError("Invalid ellipse (Tile ID ='{0}') in tileset '{1}' has zero width", tile.m_TileId, m_TilesetScript.name);
                            m_TilesetScript.m_HasErrors = true;
                        }
                        else if (collision.m_Size.y == 0)
                        {
                            m_Importer.ReportError("Invalid ellipse (Tile ID ='{0}') in tileset '{1}' has zero height", tile.m_TileId, m_TilesetScript.name);
                            m_TilesetScript.m_HasErrors = true;
                        }
                        else
                        {
                            collision.MakePointsFromEllipse(m_Importer.SuperImportContext.Settings.EdgesPerEllipse);
                        }
                    }
                    else
                    {
                        // By default, objects are rectangles
                        if (collision.m_Size.x == 0)
                        {
                            m_Importer.ReportError("Invalid rectangle (Tile ID ='{0}') in tileset '{1}' has zero width", tile.m_TileId, m_TilesetScript.name);
                            m_TilesetScript.m_HasErrors = true;
                        }
                        else if (collision.m_Size.y == 0)
                        {
                            m_Importer.ReportError("Invalid rectangle (Tile ID ='{0}') in tileset '{1}' has zero height", tile.m_TileId, m_TilesetScript.name);
                            m_TilesetScript.m_HasErrors = true;
                        }
                        else
                        {
                            collision.MakePointsFromRectangle();
                        }
                    }

                    // Do not add if there are no points
                    if (!collision.Points.IsEmpty())
                    {
                        AssignCollisionObjectProperties(collision, tile);

                        collision.RenderPoints(tile, m_TilesetScript.m_GridOrientation, m_TilesetScript.m_GridSize);
                        tile.m_CollisionObjects.Add(collision);
                    }
                }
            }
        }