예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TiledMap"/> class.
        /// </summary>
        /// <param name="FilePath">The file path.</param>
        /// <param name="graphicsDevice">The graphics device.</param>
        public TiledMap(string FilePath, GraphicsDevice graphicsDevice)
        {
            Properties = new TiledPropertyCollection();

            Stream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            using (XmlReader reader = XmlReader.Create(fileStream, new XmlReaderSettings { CloseInput = true }))
            {
                while (reader.Read())
                {
                    // reader.Read() returns each starting element with any whitespaces and <xml? so we want to
                    // make sure we just get the starting elements. it reads in order, without depth sequence.
                    if (reader.IsStartElement())
                    {
                        // Get element name and switch on it.
                        switch (reader.Name)
                        {
                            case "map":
                                Width = int.Parse(reader["width"]);
                                Height = int.Parse(reader["height"]);
                                break;
                            case "properties":
                                LoadProperties(reader);
                                break;
                            case "tileset":
                                LoadTileset(reader);
                                break;
                            case "layer":
                                LoadLayer(reader);
                                break;
                            case "objectgroup":
                                LoadObjectGroupLayer(reader);
                                break;
                        }
                    }
                }
            }
            // TODO: After parsing the map file's XML, perhaps we should load the Texture2D of the tilesets?
            for (int i = 0; i < TileSets.Count; i++)
            {
                Stream textureFileStream = new FileStream(TileSets[i].SourcePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                TileSets[i].Texture = Texture2D.FromStream(graphicsDevice, textureFileStream, TileSets[i].Width, TileSets[i].Height, false);
            }
            // Now set the tile's texture
            // data is left-to-right, top-to-bottom
            foreach (Layer layer in Layers)
            {
                for (int x = 0; x < layer.Tiles.Width; x++)
                {
                    for (int y = 0; y < layer.Tiles.Height; y++)
                    {
                        if (layer.Tiles[x, y].Id == 0) continue;
                        layer.Tiles[x, y].ParentTileSet = GetTileSetFromGID(layer.Tiles[x, y].Id);
                        layer.Tiles[x, y].X = (layer.Tiles[x, y].Id - layer.Tiles[x, y].ParentTileSet.FirstGID) % (layer.Tiles[x, y].ParentTileSet.Width / 32) * 32;
                        layer.Tiles[x, y].Y = (layer.Tiles[x, y].Id - layer.Tiles[x, y].ParentTileSet.FirstGID) / (layer.Tiles[x, y].ParentTileSet.Width / 32) * 32;
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TiledLayer"/> class.
        /// </summary>
        protected TiledLayer()
        {
            _properties = new TiledPropertyCollection();

            Name = string.Empty;
            Width = 0;
            Height = 0;
            Opacity = 1f;
            Visible = true;
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TiledLayer"/> class.
        /// </summary>
        protected TiledLayer()
        {
            _properties = new TiledPropertyCollection();

            this.Name = string.Empty;
            this.Width = 0;
            this.Height = 0;
            this.Opacity = 1f;
            this.Visible = true;
        }
예제 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TiledTile"/> class.
        /// </summary>
        public TiledTile()
        {
            _properties = new TiledPropertyCollection();

            Id = 0;
        }
예제 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TiledObject"/> class.
 /// </summary>
 protected TiledObject()
 {
     _properties = new TiledPropertyCollection();
 }
예제 #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TiledMap"/> class.
 /// </summary>
 public TiledMap()
 {
     _properties = new TiledPropertyCollection();
     _tilesets = new TiledTilesetCollection();
     _layers = new TiledLayerCollection();
 }