Example #1
0
        public TilePolygonObject(int id, float x, float y, TilePolygon polygon) : base(id, x, y)
        {
            X       = x;
            Y       = y;
            Polygon = polygon;

            var bodyDef = new BodyDef
            {
                Position = new Vec2(x.ToMeter(), y.ToMeter()),
            };

            Body = Map.World.CreateBody(bodyDef);

            var dividedPolygon = DividePolygon(polygon.Points);

            _polygonDefs = dividedPolygon.Select(points =>
            {
                points         = MakePolygonCw(points);
                var polygonDef = new PolygonDef
                {
                    VertexCount = points.Count,
                    Vertices    = points.Select(point => point.ToVec2()).ToArray()
                };
                polygonDef.Filter.CategoryBits = (ushort)CategoryBits.Ground;
                polygonDef.Filter.MaskBits     = (ushort)MaskBits.Ground;
                return(polygonDef);
            }).ToList();

            _polygonDefs.ForEach(def => { Body.CreateShape(def); });

            Body.SetUserData(this);
        }
Example #2
0
        public static TileObject Parse(XmlElement xmlElement)
        {
            var x  = float.Parse(xmlElement.GetAttribute("x"));
            var y  = float.Parse(xmlElement.GetAttribute("y"));
            var id = int.Parse(xmlElement.GetAttribute("id"));

            if (xmlElement.HasAttribute("width"))
            {
                var width       = float.Parse(xmlElement.GetAttribute("width"));
                var height      = float.Parse(xmlElement.GetAttribute("height"));
                var hasRotation = xmlElement.HasAttribute("rotation");
                var rotation    = hasRotation ? float.Parse(xmlElement.GetAttribute("rotation")) : 0;
                if (xmlElement.HasAttribute("gid"))
                {
                    var gid = int.Parse(xmlElement.GetAttribute("gid"));
                    return(new TileImageObject(id, x, y, width, height, gid));
                }
                return(new TileRectangleObject(id, x, y, width, height, rotation));
            }
            TilePolygon    polygon        = null;
            TileObjectType tileObjectType = TileObjectType.Unknown;

            foreach (XmlElement item in xmlElement.ChildNodes)
            {
                switch (item.Name)
                {
                case "polygon":
                    polygon = new TilePolygon();
                    polygon.Parse(item);
                    tileObjectType = TileObjectType.Polygon;
                    break;

                case "point":
                    tileObjectType = TileObjectType.Point;
                    break;
                }
            }

            switch (tileObjectType)
            {
            case TileObjectType.Polygon:
                return(new TilePolygonObject(id, x, y, polygon));

            case TileObjectType.Point:
                return(new TilePointObject(id, x, y));

            default:
                return(new TileObject(id, x, y));
            }
        }