static TiledObjectGroup readObjectGroup(ContentReader reader, TiledMap tiledMap)
        {
            var objectGroup = tiledMap.createObjectGroup(
                reader.ReadString(), reader.ReadColor(), reader.ReadBoolean(), reader.ReadSingle());

            readCustomProperties(reader, objectGroup.properties);

            var objectCount = reader.ReadInt32();

            objectGroup.objects = new TiledObject[objectCount];
            for (var i = 0; i < objectCount; i++)
            {
                var obj = new TiledObject()
                {
                    gid      = reader.ReadInt32(),
                    name     = reader.ReadString(),
                    type     = reader.ReadString(),
                    x        = reader.ReadInt32(),
                    y        = reader.ReadInt32(),
                    width    = reader.ReadInt32(),
                    height   = reader.ReadInt32(),
                    rotation = reader.ReadInt32(),
                    visible  = reader.ReadBoolean()
                };

                var tiledObjectType = reader.ReadString();
                if (tiledObjectType == "ellipse")
                {
                    // ellipse has no extra props
                    obj.tiledObjectType = TiledObject.TiledObjectType.Ellipse;
                }
                else if (tiledObjectType == "image")
                {
                    obj.tiledObjectType = TiledObject.TiledObjectType.Image;
                    throw new NotImplementedException("Image objects are not yet implemented");
                }
                else if (tiledObjectType == "polygon")
                {
                    obj.tiledObjectType = TiledObject.TiledObjectType.Polygon;
                    obj.polyPoints      = readVector2Array(reader);
                }
                else if (tiledObjectType == "polyline")
                {
                    obj.tiledObjectType = TiledObject.TiledObjectType.Polyline;
                    obj.polyPoints      = readVector2Array(reader);
                }
                else
                {
                    obj.tiledObjectType = TiledObject.TiledObjectType.None;
                }

                obj.objectType = reader.ReadString();

                readCustomProperties(reader, obj.properties);

                objectGroup.objects[i] = obj;
            }

            return(objectGroup);
        }
Exemplo n.º 2
0
        protected override TiledMap Read(ContentReader reader, TiledMap existingInstance)
        {
            var backgroundColor = reader.ReadColor();
            var renderOrder     = (TiledRenderOrder)Enum.Parse(typeof(TiledRenderOrder), reader.ReadString(), true);
            var tiledMap        = new TiledMap(firstGid: reader.ReadInt32(),
                                               width: reader.ReadInt32(),
                                               height: reader.ReadInt32(),
                                               tileWidth: reader.ReadInt32(),
                                               tileHeight: reader.ReadInt32(),
                                               orientation: (TiledMapOrientation)reader.ReadInt32())
            {
                backgroundColor = backgroundColor,
                renderOrder     = renderOrder
            };

            tiledMap.largestTileWidth  = reader.ReadInt32();
            tiledMap.largestTileHeight = reader.ReadInt32();

            // determine if we have some tiles that are larger than our standard tile size and if so mark this map for requiring culling
            if (tiledMap.largestTileWidth > tiledMap.tileWidth || tiledMap.largestTileHeight > tiledMap.tileHeight)
            {
                tiledMap.requiresLargeTileCulling = true;
            }

            readCustomProperties(reader, tiledMap.properties);

            var tilesetCount = reader.ReadInt32();

            for (var i = 0; i < tilesetCount; i++)
            {
                var isStandardTileset = reader.ReadBoolean();

                // image collections will have not texture associated with them
                var       textureName = reader.ReadString();
                Texture2D texture     = null;
                if (textureName != string.Empty)
                {
                    var textureAssetName = reader.getRelativeAssetPath(textureName);
                    texture = reader.ContentManager.Load <Texture2D>(textureAssetName);
                }

                var tileset = tiledMap.createTileset(
                    texture: texture,
                    firstId: reader.ReadInt32(),
                    tileWidth: reader.ReadInt32(),
                    tileHeight: reader.ReadInt32(),
                    isStandardTileset: isStandardTileset,
                    spacing: reader.ReadInt32(),
                    margin: reader.ReadInt32());
                readCustomProperties(reader, tileset.properties);

                // tiledset tile array
                var tileCount = reader.ReadInt32();
                for (var j = 0; j < tileCount; j++)
                {
                    var tile = new TiledTilesetTile(reader.ReadInt32(), tiledMap);

                    var tileAnimationFrameCount = reader.ReadInt32();
                    if (tileAnimationFrameCount > 0)
                    {
                        tile.animationFrames = new List <TiledTileAnimationFrame>(tileAnimationFrameCount);
                    }

                    for (var k = 0; k < tileAnimationFrameCount; k++)
                    {
                        tile.animationFrames.Add(new TiledTileAnimationFrame(reader.ReadInt32(), reader.ReadSingle()));
                    }

                    // image source is optional
                    var isFromImageCollection = reader.ReadBoolean();
                    if (isFromImageCollection)
                    {
                        var rect = new Rectangle(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
                        ((TiledImageCollectionTileset)tileset).setTileTextureRegion(tileset.firstId + tile.id, rect);
                    }

                    int objectGroupCount_ = reader.ReadInt32();
                    for (int k = 0; k < objectGroupCount_; k++)
                    {
                        var objectGroup = new TiledObjectGroup(reader.ReadString(), reader.ReadColor(), reader.ReadBoolean(), reader.ReadSingle());

                        readCustomProperties(reader, objectGroup.properties);

                        var objectCount = reader.ReadInt32();
                        objectGroup.objects = new TiledObject[objectCount];
                        for (var l = 0; l < objectCount; l++)
                        {
                            var obj = new TiledObject()
                            {
                                gid      = reader.ReadInt32(),
                                name     = reader.ReadString(),
                                type     = reader.ReadString(),
                                x        = reader.ReadInt32(),
                                y        = reader.ReadInt32(),
                                width    = reader.ReadInt32(),
                                height   = reader.ReadInt32(),
                                rotation = reader.ReadInt32(),
                                visible  = reader.ReadBoolean()
                            };

                            var tiledObjectType = reader.ReadString();
                            if (tiledObjectType == "ellipse")
                            {
                                // ellipse has no extra props
                                obj.tiledObjectType = TiledObject.TiledObjectType.Ellipse;
                            }
                            else if (tiledObjectType == "image")
                            {
                                obj.tiledObjectType = TiledObject.TiledObjectType.Image;
                                throw new NotImplementedException("Image objects are not yet implemented");
                            }
                            else if (tiledObjectType == "polygon")
                            {
                                obj.tiledObjectType = TiledObject.TiledObjectType.Polygon;
                                obj.polyPoints      = readVector2Array(reader);
                            }
                            else if (tiledObjectType == "polyline")
                            {
                                obj.tiledObjectType = TiledObject.TiledObjectType.Polyline;
                                obj.polyPoints      = readVector2Array(reader);
                            }
                            else
                            {
                                obj.tiledObjectType = TiledObject.TiledObjectType.None;
                            }

                            obj.objectType = reader.ReadString();

                            readCustomProperties(reader, obj.properties);

                            objectGroup.objects[l] = obj;
                            if (tile.objectGroups == null)
                            {
                                tile.objectGroups = new List <TiledObjectGroup>();
                            }
                            tile.objectGroups.Add(objectGroup);
                        }
                    }

                    readCustomProperties(reader, tile.properties);

                    // give the TiledTilesetTile a chance to process and cache any data required
                    tile.processProperties();

                    // if this tile is from an image collection and it has no properties there is no need to keep it around since we
                    // already grabbed the image source above
                    if (!(isFromImageCollection && tile.properties.Count == 0))
                    {
                        tileset.tiles.Add(tile);
                    }
                }
            }

            var layerCount = reader.ReadInt32();

            for (var i = 0; i < layerCount; i++)
            {
                var layer = readLayer(reader, tiledMap);
                readCustomProperties(reader, layer.properties);
            }


            var objectGroupCount = reader.ReadInt32();

            for (var i = 0; i < objectGroupCount; i++)
            {
                readObjectGroup(reader, tiledMap);
            }

            return(tiledMap);
        }