Ejemplo n.º 1
0
    private void _serializeTsx(tileset _tileSet, string strFileName)
    {
        XmlSerializer serializer = new XmlSerializer(_tileSet.GetType());
        StreamWriter  writer     = new StreamWriter("test.tsx");

        serializer.Serialize(writer.BaseStream, _tileSet);
        writer.Close();
    }
Ejemplo n.º 2
0
        private void LoadValuesFromSource()
        {
            if (!string.IsNullOrEmpty(this._sourceField))
            {
                _sourceField = _sourceField.Replace("/", "\\");

                tileset xts = null;

                try
                {
                    xts = FileManager.XmlDeserialize <tileset>(_sourceField);
                }
                catch (FileNotFoundException)
                {
                    string fileAttemptedToLoad = _sourceField;
                    if (FileManager.IsRelative(_sourceField))
                    {
                        fileAttemptedToLoad = FileManager.RelativeDirectory + _sourceField;
                    }

                    string message = "Could not find the shared tsx file \n" + fileAttemptedToLoad +
                                     "\nIf this is a relative file name, then the loader will use " +
                                     "the FileManager's RelativeDirectory to make the file absolute.  Therefore, be sure to set the FileManger's RelativeDirectory to the file represented by " +
                                     "this fileset before setting this property if setting this property manually.";


                    throw new FileNotFoundException(message);
                }

                if (xts.image != null)
                {
                    Images = new TilesetImage[xts.image.Length];

                    Parallel.For(0, xts.image.Length, count =>
                    {
                        this.Images[count] = new TilesetImage
                        {
                            Source = xts.image[count].source,
                            height = xts.image[count].height != 0 ? xts.image[count].height : xts.tileheight,
                            width  = xts.image[count].width != 0 ? xts.image[count].width : xts.tilewidth
                        };
                    });
                }
                this.Name       = xts.name;
                this.Margin     = xts.margin;
                this.Spacing    = xts.spacing;
                this.Tileheight = xts.tileheight;
                this.Tilewidth  = xts.tilewidth;
                this.Tiles      = xts.tile;
            }
        }
Ejemplo n.º 3
0
    public void TsxSerializeData()
    {
        DeserializeSpriteYaml();
        m_tileSet = new tileset();

        int nMaxRow    = 0;
        int nMaxColumn = 0;

        foreach (CustomYamlClass.Sprite _spr in m_rootDataTsx.TextureImporter.spriteSheet.sprites)
        {
            m_tileSet.tilewidth  = (byte)_spr.rect.width;
            m_tileSet.tileheight = (byte)_spr.rect.height;
            int nCol = _spr.rect.x / m_tileSet.tilewidth;
            if (nMaxColumn < nCol)
            {
                nMaxColumn = nCol;
            }
            int nRow = _spr.rect.y / m_tileSet.tileheight;
            if (nMaxRow < nRow)
            {
                nMaxRow = nRow;
            }
        }
        nMaxColumn++;
        nMaxRow++;
        m_tileSet.columns      = (byte)nMaxColumn;
        m_tileSet.tilecount    = (byte)(nMaxColumn * nMaxRow);
        m_tileSet.version      = 1.2m;
        m_tileSet.tiledversion = "1.3.1";
        m_tileSet.name         = "baba";
        tilesetImage _img = new tilesetImage();

        _img.height     = (byte)(nMaxColumn * m_tileSet.tileheight);
        _img.width      = (byte)(nMaxRow * m_tileSet.tilewidth);
        _img.source     = "Assets/Resources/Sprites/SmallPalette.png";
        m_tileSet.image = _img;

        _serializeTsx(m_tileSet, "test.tsx");
        XmlSerializer serializer = new XmlSerializer(m_tileSet.GetType());
        StreamWriter  writer     = new StreamWriter("test.tsx");

        serializer.Serialize(writer.BaseStream, m_tileSet);
        writer.Close();
    }
    int tileWidth; //the width of each tile

    #endregion Fields

    #region Methods

    //Constructs map from tmx file and saves map in a specified scene
    void constructMap(XmlDocument doc)
    {
        XmlNode root = doc.DocumentElement;
        XmlNode dataStart = root["layer"].FirstChild;
        XmlNodeList grid = dataStart.ChildNodes;

        float xpos = 0;
        float ypos = 0;
        float spacingFactor = tileSize * .01f;
        for (int i = 0; i < grid.Count; ++i)
        {
            int gid = Convert.ToInt32(grid[i].Attributes["gid"].Value);
            tileset currentTileSet = new tileset();

            bool stop = false;
            int j = 0;
            while (!stop)
            {
                if (j == tilesets.Count - 1)
                {
                    currentTileSet = tilesets[j];
                    stop = true;
                }

                else if (gid >= tilesets[j].firstGid && gid < tilesets[j + 1].firstGid)
                {
                    currentTileSet = tilesets[j];
                    stop = true;
                }

                else
                {
                    ++j;
                }
            }

            for (int z = 0; z < textureNames.Count; ++z)
            {
                if (textureNames[z] == currentTileSet.name)
                {
                    Texture2D currentTexture = Textures[z];

                    int normalizedGid = gid - (currentTileSet.firstGid - 1);
                    int columnSize = currentTileSet.imageWidth / currentTileSet.tileWidth;
                    int rowSize = currentTileSet.imageHeight / currentTileSet.tileHeight;
                    int column = normalizedGid % columnSize;
                    int row = 1;

                    if (column != 0)
                    {
                        row = rowSize - ((normalizedGid + (columnSize - column)) / columnSize) + 1;
                    }

                    else
                    {
                        column = columnSize;
                        row = rowSize - (normalizedGid / columnSize) + 1;
                    }

                    int x = (column - 1) * tileWidth;
                    int y = (row - 1) * tileHeight;
                    int blockWidth = currentTileSet.tileWidth;
                    int blockHeight = currentTileSet.tileHeight;

                    Color[] tilePixels = currentTexture.GetPixels(x, y, blockWidth, blockHeight);

                    Texture2D destTex = new Texture2D(tileWidth, tileHeight);
                    destTex.SetPixels(tilePixels);
                    destTex.Apply();

                    File.WriteAllBytes(destinationUrlName + "/" + Convert.ToString(i) + ".png", (byte[])destTex.EncodeToPNG());

                    tile.GetComponent<SpriteRenderer>().sprite = Sprite.Create(destTex, new Rect(0, 0, tileWidth, tileHeight), new Vector2(0, 0));
                    Instantiate(tile, new Vector3(xpos, ypos, 0), Quaternion.identity);
                }
            }

            xpos += spacingFactor;

            if ((i + 1) % mapWidth == 0)
            {
                xpos = 0;
                ypos -= spacingFactor;
            }
        }
    }
    //Creates tilesets from the tmx file
    void constructTileSets(XmlDocument doc)
    {
        XmlAttributeCollection mapAttributes = doc.DocumentElement.Attributes;

        mapWidth = Convert.ToInt32(mapAttributes["width"].Value);
        mapHeight = Convert.ToInt32(mapAttributes["height"].Value);
        tileWidth = Convert.ToInt32(mapAttributes["tilewidth"].Value);
        tileHeight = Convert.ToInt32(mapAttributes["tileheight"].Value);

        XmlNodeList children = doc.DocumentElement.ChildNodes;

        int i = 0;
        while (children[i].Name == "tileset")
        {
            XmlAttributeCollection tilesetAttributes = children[i].Attributes;

            int first_gid = Convert.ToInt32(tilesetAttributes["firstgid"].Value);
            string n = tilesetAttributes["name"].Value;
            int tile_width = Convert.ToInt32(tilesetAttributes["tilewidth"].Value);
            int tile_height = Convert.ToInt32(tilesetAttributes["tileheight"].Value);
            string s = children[i].FirstChild.Attributes["source"].Value;
            int image_width = Convert.ToInt32(children[i].FirstChild.Attributes["width"].Value);
            int image_height = Convert.ToInt32(children[i].FirstChild.Attributes["height"].Value);

            tileset next_tileset = new tileset(first_gid, n, tile_width, tile_height, s, image_width, image_height);

            tilesets.Add(next_tileset);
            ++i;
        }
    }