Beispiel #1
0
        protected override void Write(ContentWriter output, MapContent value)
        {
            // write the map information
            output.Write(value.Version);
            output.Write((byte)value.Orientation);
            output.Write(value.Width);
            output.Write(value.Height);
            output.Write(value.TileWidth);
            output.Write(value.TileHeight);
            WritePropertyList(output, value.Properties);
            output.Write(value.MakeTilesUnique);

            // write out our tile sets. we don't use a runtime TileSet, we read in as
            // a list of tile objects. so we don't write out a lot of the TileSet
            // information as most of it isn't useful.
            output.Write(value.TileSets.Count);
            foreach (var tileSet in value.TileSets)
            {
                // write out the first global ID of these tiles
                output.Write(tileSet.FirstId);

                // write out the texture used by the tiles
                output.WriteExternalReference(tileSet.Texture);

                // write out all the tiles in the tile set
                output.Write(tileSet.Tiles.Count);
                foreach (var tile in tileSet.Tiles)
                {
                    output.WriteObject(tile.Source);
                    WritePropertyList(output, tile.Properties);
                }
            }

            // write each layer
            output.Write(value.Layers.Count);
            foreach (var layer in value.Layers)
            {
                // basic information
                output.Write(layer.Type);
                output.Write(layer.Name);
                output.Write(layer.Width);
                output.Write(layer.Height);
                output.Write(layer.Visible);
                output.Write(layer.Opacity);
                WritePropertyList(output, layer.Properties);

                // figure out specific type of layer
                TileLayerContent      tileLayer = layer as TileLayerContent;
                MapObjectLayerContent objLayer  = layer as MapObjectLayerContent;

                // tile layers just write out index data
                if (tileLayer != null)
                {
                    output.WriteObject(tileLayer.Data);
                }

                // object layers write out all the objects
                else if (objLayer != null)
                {
                    output.Write(objLayer.Objects.Count);
                    foreach (var mapObj in objLayer.Objects)
                    {
                        output.Write(mapObj.Name);
                        output.Write(mapObj.Type);
                        output.WriteObject(mapObj.Location);
                        WritePropertyList(output, mapObj.Properties);
                    }

                    // and the color
                    output.Write(objLayer.Color);
                }
            }
        }
Beispiel #2
0
        public MapContent(XmlDocument document, ContentImporterContext context)
        {
            XmlNode mapNode = document["map"];

            Version = mapNode.Attributes["version"].Value;
            Orientation = (Orientation)Enum.Parse(typeof(Orientation), mapNode.Attributes["orientation"].Value, true);
            Width = int.Parse(mapNode.Attributes["width"].Value, CultureInfo.InvariantCulture);
            Height = int.Parse(mapNode.Attributes["height"].Value, CultureInfo.InvariantCulture);
            TileWidth = int.Parse(mapNode.Attributes["tilewidth"].Value, CultureInfo.InvariantCulture);
            TileHeight = int.Parse(mapNode.Attributes["tileheight"].Value, CultureInfo.InvariantCulture);

            XmlNode propertiesNode = document.SelectSingleNode("map/properties");
            if (propertiesNode != null)
            {
                Properties = Property.ReadProperties(propertiesNode, context);
            }

            foreach (XmlNode tileSet in document.SelectNodes("map/tileset"))
            {
                if (tileSet.Attributes["source"] != null)
                {
                    TileSets.Add(new ExternalTileSetContent(tileSet, context));
                }
                else
                {
                    TileSets.Add(new TileSetContent(tileSet, context));
                }
            }

            foreach (XmlNode layerNode in document.SelectNodes("map/layer|map/objectgroup"))
            {
                LayerContent layerContent;

                if (layerNode.Name == "layer")
                {
                    layerContent = new TileLayerContent(layerNode, context);
                }
                else if (layerNode.Name == "objectgroup")
                {
                    layerContent = new MapObjectLayerContent(layerNode, context);
                }
                else
                {
                    throw new Exception("Unknown layer name: " + layerNode.Name);
                }

                // Layer names need to be unique for our lookup system, but Tiled
                // doesn't require unique names.
                string layerName = layerContent.Name;
                int duplicateCount = 2;

                // if a layer already has the same name...
                if (Layers.Find(l => l.Name == layerName) != null)
                {
                    // figure out a layer name that does work
                    do
                    {
                        layerName = string.Format("{0}{1}", layerContent.Name, duplicateCount);
                        duplicateCount++;
                    } while (Layers.Find(l => l.Name == layerName) != null);

                    // log a warning for the user to see
                    context.Logger.LogWarning(string.Empty, new ContentIdentity(), "Renaming layer \"{1}\" to \"{2}\" to make a unique name.", layerContent.Type, layerContent.Name, layerName);

                    // save that name
                    layerContent.Name = layerName;
                }

                Layers.Add(layerContent);
            }
        }
Beispiel #3
0
        public MapContent(XmlDocument document, ContentImporterContext context)
        {
            XmlNode mapNode = document["map"];

            Version     = mapNode.Attributes["version"].Value;
            Orientation = (Orientation)Enum.Parse(typeof(Orientation), mapNode.Attributes["orientation"].Value, true);
            Width       = int.Parse(mapNode.Attributes["width"].Value, CultureInfo.InvariantCulture);
            Height      = int.Parse(mapNode.Attributes["height"].Value, CultureInfo.InvariantCulture);
            TileWidth   = int.Parse(mapNode.Attributes["tilewidth"].Value, CultureInfo.InvariantCulture);
            TileHeight  = int.Parse(mapNode.Attributes["tileheight"].Value, CultureInfo.InvariantCulture);

            XmlNode propertiesNode = document.SelectSingleNode("map/properties");

            if (propertiesNode != null)
            {
                Properties = Property.ReadProperties(propertiesNode, context);
            }

            foreach (XmlNode tileSet in document.SelectNodes("map/tileset"))
            {
                if (tileSet.Attributes["source"] != null)
                {
                    TileSets.Add(new ExternalTileSetContent(tileSet, context));
                }
                else
                {
                    TileSets.Add(new TileSetContent(tileSet, context));
                }
            }

            foreach (XmlNode layerNode in document.SelectNodes("map/layer|map/objectgroup"))
            {
                LayerContent layerContent;

                if (layerNode.Name == "layer")
                {
                    layerContent = new TileLayerContent(layerNode, context);
                }
                else if (layerNode.Name == "objectgroup")
                {
                    layerContent = new MapObjectLayerContent(layerNode, context);
                }
                else
                {
                    throw new Exception("Unknown layer name: " + layerNode.Name);
                }

                // Layer names need to be unique for our lookup system, but Tiled
                // doesn't require unique names.
                string layerName      = layerContent.Name;
                int    duplicateCount = 2;

                // if a layer already has the same name...
                if (Layers.Find(l => l.Name == layerName) != null)
                {
                    // figure out a layer name that does work
                    do
                    {
                        layerName = string.Format("{0}{1}", layerContent.Name, duplicateCount);
                        duplicateCount++;
                    } while (Layers.Find(l => l.Name == layerName) != null);

                    // log a warning for the user to see
                    context.Logger.LogWarning(string.Empty, new ContentIdentity(), "Renaming layer \"{1}\" to \"{2}\" to make a unique name.", layerContent.Type, layerContent.Name, layerName);

                    // save that name
                    layerContent.Name = layerName;
                }

                Layers.Add(layerContent);
            }
        }