예제 #1
0
        private void LoadMap(string filename)
        {
            //Create a new document
            XDocument doc = new XDocument();

            doc = XDocument.Load(filename);
            //Find the root element
            XElement Root = doc.Root;

            //Get and set map width
            XElement Width = Root.Element("Width");
            int      width = Convert.ToInt32(Width.Value);

            gridWidth = width;
            map.resetMapSize(this);
            //Iterate through each element
            foreach (XElement Ele in Root.Descendants("Tile"))
            {
                //Get the x, y and ID
                int x  = Convert.ToInt32((int)Ele.Attribute("X"));
                int y  = Convert.ToInt32((int)Ele.Attribute("Y"));
                int id = Convert.ToInt32((int)Ele.Attribute("ID"));
                //Create a new image to hold the tile
                Image toLoad = new Image();
                //Get the tile according to the id
                toLoad.Source = ((Image)MapTiles.Items.GetItemAt(id)).Source;
                //Set the x and y of the tile array
                TileArray[x, y] = id;
                //Add the image to the canvas
                Canvas.SetLeft(toLoad, x * tileSize);
                Canvas.SetTop(toLoad, y * tileSize);
                Mapping.Children.Add(toLoad);
            }
        }