Exemple #1
0
    // Expects the name of a csv-mode .tmx file, renamed to .txt
    public static bool Import(string levelName)
    {
        string actualPath = "Resources/" + GetLevelPath(levelName) + ".txt";

        TextAsset ta = OpenLevel(levelName);

        if (ta == null)
        {
            Debug.LogError("Level not found: " + actualPath);
            return(false);
        }

        XmlDocument xml = new XmlDocument();

        xml.LoadXml(ta.text);

        XmlNode mapNode = xml.SelectSingleNode("map");
        int     width   = int.Parse(mapNode.Attributes["width"].Value);
        int     height  = int.Parse(mapNode.Attributes["height"].Value);

        Debug.Log("Found map of size: " + width + "x" + height + " at " + actualPath);
        Board.currBoard.width    = width;
        Board.currBoard.height   = height + bottomWallOffset + topWallOffset;
        Board.currBoard.startRow = topWallOffset;

        XmlNode dataNode   = xml.SelectSingleNode("map/layer/data");
        string  dataString = dataNode.InnerText;

        if (dataString.Length == 0)
        {
            Debug.LogError("TMX file has no data, or it is not using CSV encoding: " + actualPath);
            return(false);
        }
        string[] data = dataString.Split(',');

        // Read data and add tiles
        int maxY = 0;
        int i    = 0;

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                string   id       = data[i];
                TileType tileType = (TileType)int.Parse(id);

                Tile tile = TileFactory.CreateAndAddTile(tileType, x, y + topWallOffset);

                if (tile != null)
                {
                    maxY = y + topWallOffset;
                }
                i++;
            }
        }

        // Add wall at top and bottom
        TileFactory.AddFullWidthWall(0);
        TileFactory.AddFullWidthWall(maxY + bottomWallOffset);

        ProcessProperties(xml.SelectNodes("map/properties/property"));
        ProcessProperties(xml.SelectNodes("map/layer/properties/property"));

        return(true);
    }