private static TiledMapObject ReadTiledMapObject(ContentReader reader, TiledMap map)
        {
            var         objectType = (TiledMapObjectType)reader.ReadByte();
            var         identifier = reader.ReadInt32();
            var         name       = reader.ReadString();
            var         type       = reader.ReadString();
            var         position   = new Vector2(reader.ReadSingle(), reader.ReadSingle());
            var         width      = reader.ReadSingle();
            var         height     = reader.ReadSingle();
            var         size       = new SizeF(width, height);
            var         rotation   = reader.ReadSingle();
            var         isVisible  = reader.ReadBoolean();
            var         properties = new TiledMapProperties();
            const float opacity    = 1.0f;

            ReadProperties(reader, properties);

            TiledMapObject mapObject;

            switch (objectType)
            {
            case TiledMapObjectType.Rectangle:
                mapObject = new TiledMapRectangleObject(identifier, name, size, position, rotation, opacity, isVisible, type);
                break;

            case TiledMapObjectType.Tile:
                var globalTileIdentifierWithFlags = reader.ReadUInt32();
                var tile                = new TiledMapTile(globalTileIdentifierWithFlags, (ushort)position.X, (ushort)position.Y);
                var tileset             = map.GetTilesetByTileGlobalIdentifier(tile.GlobalIdentifier);
                var localTileIdentifier = tile.GlobalIdentifier - map.GetTilesetFirstGlobalIdentifier(tileset);
                var tilesetTile         = tileset.Tiles.FirstOrDefault(x => x.LocalTileIdentifier == localTileIdentifier);
                mapObject = new TiledMapTileObject(identifier, name, tileset, tilesetTile, size, position, rotation, opacity, isVisible, type);
                break;

            case TiledMapObjectType.Ellipse:
                mapObject = new TiledMapEllipseObject(identifier, name, size, position, rotation, opacity, isVisible);
                break;

            case TiledMapObjectType.Polygon:
                mapObject = new TiledMapPolygonObject(identifier, name, ReadPoints(reader), size, position, rotation, opacity, isVisible, type);
                break;

            case TiledMapObjectType.Polyline:
                mapObject = new TiledMapPolylineObject(identifier, name, ReadPoints(reader), size, position, rotation, opacity, isVisible, type);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            foreach (var property in properties)
            {
                mapObject.Properties.Add(property.Key, property.Value);
            }

            return(mapObject);
        }
        private static TiledMapObject ReadTiledMapObject(ContentReader reader, TiledMapTileset tileset)
        {
            var         objectType = (TiledMapObjectType)reader.ReadByte();
            var         identifier = reader.ReadInt32();
            var         name       = reader.ReadString();
            var         type       = reader.ReadString();
            var         position   = new Vector2(reader.ReadSingle(), reader.ReadSingle());
            var         width      = reader.ReadSingle();
            var         height     = reader.ReadSingle();
            var         size       = new Size2(width, height);
            var         rotation   = reader.ReadSingle();
            var         isVisible  = reader.ReadBoolean();
            var         properties = new TiledMapProperties();
            const float opacity    = 1.0f;

            ReadProperties(reader, properties);

            TiledMapObject mapObject;

            switch (objectType)
            {
            case TiledMapObjectType.Rectangle:
                mapObject = new TiledMapRectangleObject(identifier, name, size, position, rotation, opacity, isVisible, type);
                break;

            case TiledMapObjectType.Tile:
                reader.ReadUInt32();                         // Tile objects within TiledMapTilesetTiles currently ignore the gid and behave like rectangle objects.
                mapObject = new TiledMapRectangleObject(identifier, name, size, position, rotation, opacity, isVisible, type);
                break;

            case TiledMapObjectType.Ellipse:
                mapObject = new TiledMapEllipseObject(identifier, name, size, position, rotation, opacity, isVisible);
                break;

            case TiledMapObjectType.Polygon:
                mapObject = new TiledMapPolygonObject(identifier, name, ReadPoints(reader), size, position, rotation, opacity, isVisible, type);
                break;

            case TiledMapObjectType.Polyline:
                mapObject = new TiledMapPolylineObject(identifier, name, ReadPoints(reader), size, position, rotation, opacity, isVisible, type);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            foreach (var property in properties)
            {
                mapObject.Properties.Add(property.Key, property.Value);
            }

            return(mapObject);
        }
Пример #3
0
        internal TiledMapObjectLayer(ContentReader input, TiledMap map)
            : base(input)
        {
            Color     = input.ReadColor();
            DrawOrder = (TiledMapObjectDrawOrder)input.ReadByte();

            var objectCount = input.ReadInt32();

            Objects = new TiledMapObject[objectCount];

            for (var i = 0; i < objectCount; i++)
            {
                TiledMapObject @object;

                var objectType = (TiledMapObjectType)input.ReadByte();
                switch (objectType)
                {
                case TiledMapObjectType.Rectangle:
                    @object = new TiledMapRectangleObject(input);
                    break;

                case TiledMapObjectType.Tile:
                    @object = new TiledMapTileObject(input, map);
                    break;

                case TiledMapObjectType.Ellipse:
                    @object = new TiledMapEllipseObject(input);
                    break;

                case TiledMapObjectType.Polygon:
                    @object = new TiledMapPolygonObject(input);
                    break;

                case TiledMapObjectType.Polyline:
                    @object = new TiledMapPolylineObject(input);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                Objects[i] = @object;
            }
        }
        internal TiledMapTilesetTile(int localTileIdentifier, ContentReader input)
        {
            var objectCount = input.ReadInt32();

            var objects = new TiledMapObject[objectCount];

            for (var i = 0; i < objectCount; i++)
            {
                TiledMapObject @object;

                var objectType = (TiledMapObjectType)input.ReadByte();
                switch (objectType)
                {
                case TiledMapObjectType.Rectangle:
                    @object = new TiledMapRectangleObject(input);
                    break;

                case TiledMapObjectType.Ellipse:
                    @object = new TiledMapEllipseObject(input);
                    break;

                case TiledMapObjectType.Polygon:
                    @object = new TiledMapPolygonObject(input);
                    break;

                case TiledMapObjectType.Polyline:
                    @object = new TiledMapPolylineObject(input);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                objects[i] = @object;
            }

            Objects             = objects;
            LocalTileIdentifier = localTileIdentifier;
            Properties          = new TiledMapProperties();
        }