Exemplo n.º 1
0
        TiledTileset LoadTilesetData( XElement node, TiledTileset tileset )
        {
            if( node.Name != "tileset" )
            {
                Log.Editor.WriteError("Cannot load tileset data! (Element should be <tileset>, not <{0}>)", node.Name);
                return null;
            }

            if( tileset == null )
            {
                Log.Editor.WriteError("Cannot load tileset data! (Tileset is NULL)");
                return null;
            }

            // Read attributes
            foreach( var attribute in node.Attributes() )
            {
                switch( attribute.Name.LocalName )
                {
                    case "firstgid":
                        tileset.FirstGID = int.Parse(attribute.Value, System.Globalization.NumberStyles.Integer);
                        break;

                    case "name":
                        tileset.Name = attribute.Value;
                        break;

                    case "tilewidth":
                        tileset.TileW = int.Parse(attribute.Value, System.Globalization.NumberStyles.Integer);
                        break;

                    case "tileheight":
                        tileset.TileH = int.Parse(attribute.Value, System.Globalization.NumberStyles.Integer);
                        break;

                    case "tilecount":
                        tileset.TileCount = int.Parse(attribute.Value, System.Globalization.NumberStyles.Integer);
                        break;

                    // Tilemap is using an external tileset file; go recursive!
                    case "source":
                        using( TextReader txt = new StreamReader(FileOp.Open(@"Source\Media\"+attribute.Value, FileAccessMode.Read) ) )
                        {
                            var xml = new XmlData();
                            var data = txt.ReadToEnd();
                            xml.SetData(data, data.Length, Encoding.Unicode);
                            xml.Validate(); // Not sure if this is necessary

                            // Data is valid
                            if( xml.IsValid )
                                return LoadTilesetData(xml.XmlDocument.Root, tileset);

                            Log.Editor.WriteError("Unable to load external tileset!");
                            return null;
                        }

                    default:
                        Log.Editor.WriteWarning("Attribute {0} not supported in tilesets.", attribute.Name);
                        break;
                }
            }

            foreach( var xelem in node.Elements() )
            {
                var element = (XElement)xelem;

                switch( element.Name.LocalName )
                {
                    case "properties":
                        tileset.LoadProperties( element );
                        break;

                    case "tileoffset":
                        tileset.LoadOffset( element );
                        break;

                    case "image":
                        tileset.LoadImage( element );
                        break;

                    case "terraintypes":
                        tileset.LoadTerrainTypes( element );
                        break;

                    case "tile":
                        tileset.LoadTerrainTile( element );
                        break;

                    default:
                        Log.Editor.WriteWarning("Element {0} not supported in tilesets.", element.Name);
                        break;
                }
            }

            return tileset;
        }
Exemplo n.º 2
0
        TiledTileset LoadTileset( XElement node )
        {
            if( node.HasAttributes )
            {
                Log.Editor.Write("Loading tileset..."); //node.Attribute("name").Value);

                var tileset = new TiledTileset();
                tileset.Parent = this;

                return LoadTilesetData( node, tileset );
            }

            return null;
        }