Example #1
0
        private void ProcessPolylineElement(GameObject goObject, XElement xPolyline)
        {
            var points = xPolyline.GetAttributeAsVector2Array("points");

            ColliderFactory.MakePolyline(goObject, points);
            goObject.AddComponent <SuperColliderComponent>();
        }
Example #2
0
        public void CreateObjects()
        {
            Assert.IsNotNull(m_Xml);
            Assert.IsNotNull(m_ObjectLayer);
            Assert.IsNotNull(Importer);
            Assert.IsNotNull(ColliderFactory);

            var xObjects  = m_Xml.Elements("object");
            var drawOrder = m_Xml.GetAttributeAs <DrawOrder>("draworder", DrawOrder.TopDown);

            if (drawOrder == DrawOrder.TopDown)
            {
                // xObjects need to be ordered by y-value
                xObjects = xObjects.OrderBy((x) => {
                    float m_X         = x.GetAttributeAs <float>("x", 0.0f);
                    float m_Y         = x.GetAttributeAs <float>("y", 0.0f);
                    var localPosition = ColliderFactory.TransformPoint(new Vector2(m_X, m_Y));
                    return(localPosition.y);
                });
            }

            foreach (var xObj in xObjects)
            {
                // Ignore invisible objects
                if (!xObj.GetAttributeAs <bool>("visible", true))
                {
                    continue;
                }

                CreateObject(xObj);
            }
        }
Example #3
0
        private void ProcessObjectRectangle(GameObject goObject, XElement xObject)
        {
            var width  = xObject.GetAttributeAs("width", 0f);
            var height = xObject.GetAttributeAs("height", 0f);

            ColliderFactory.MakeBox(goObject, width, height);
            goObject.AddComponent <SuperColliderComponent>();
        }
Example #4
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);
        }
Example #5
0
        private SuperObject CreateSuperObject(XElement xObject)
        {
            // Create the object
            GameObject goObject = new GameObject();
            var        comp     = goObject.AddComponent <SuperObject>();

            // Fill out the attributes
            comp.m_Id        = xObject.GetAttributeAs("id", 0);
            comp.m_TiledName = xObject.GetAttributeAs("name", string.Format("Object_{0}", comp.m_Id));
            comp.m_Type      = xObject.GetAttributeAs("type", "");
            comp.m_X         = xObject.GetAttributeAs("x", 0.0f);
            comp.m_Y         = xObject.GetAttributeAs("y", 0.0f);
            comp.m_Rotation  = xObject.GetAttributeAs("rotation", 0.0f);
            comp.m_Width     = xObject.GetAttributeAs("width", 0.0f);
            comp.m_Height    = xObject.GetAttributeAs("height", 0.0f);
            comp.m_TileId    = xObject.GetAttributeAs <uint>("gid", 0);
            comp.m_Visible   = xObject.GetAttributeAs("visible", true);
            comp.m_Template  = xObject.GetAttributeAs("template", "");

            // Assign the name of our game object
            if (comp.m_TileId != 0)
            {
                // The tile object name is decorated. A descendent will have the "real" object name.
                goObject.name = string.Format("{0} (TRS)", comp.m_TiledName);
            }
            else
            {
                goObject.name = comp.m_TiledName;
            }

            // Position the game object
            var localPosition = new Vector2(comp.m_X, comp.m_Y);

            localPosition = ColliderFactory.TransformPoint(localPosition);
            localPosition = Importer.SuperImportContext.MakePoint(localPosition);

            goObject.transform.localPosition = localPosition;
            goObject.transform.localRotation = Quaternion.Euler(0, 0, Importer.SuperImportContext.MakeRotation(comp.m_Rotation));

            // Add our object to the parent layer
            m_ObjectLayer.gameObject.AddChildWithUniqueName(goObject);

            return(comp);
        }