Esempio n. 1
0
        public void LoadMap(string path, int tileWidth, int tileHeight)
        {
            Tilemap.tileWidth  = tileWidth;
            Tilemap.tileHeight = tileHeight;

            XDocument doc = XDocument.Load(path);

            Width  = Convert.ToInt32(doc.Element("map").Attribute("width").Value);
            Height = Convert.ToInt32(doc.Element("map").Attribute("height").Value);

            // load all properties
            if (doc.Element("map").Element("properties") != null)
            {
                foreach (XElement propElement in doc.Element("map").Element("properties").Elements())
                {
                    this.Properties.Add(propElement.Attribute("name").Value, propElement.Attribute("value").Value);
                }
            }


            // load all tilesets
            foreach (XElement element in doc.Descendants("tileset"))
            {
                string assetName = element.Element("image").Attribute("source").Value;
                assetName = Path.GetFileNameWithoutExtension(assetName);

                // load all collisionGroups from this tileset ('collisionGroups' from different tiles)
                string[] tileCollisionGroups             = new string[100];
                Dictionary <string, string>[] properties = new Dictionary <string, string> [100];


                foreach (XElement tile in element.Descendants("tile"))
                {
                    int index = (int)tile.Attribute("id");

                    if (tile.Descendants("property").First().Attribute("name").Value == "tags")
                    {
                        string groupName = tile.Descendants("property").First().Attribute("value").Value;

                        tileCollisionGroups[index] = groupName;
                        this.CollisionGroups.Add(groupName);
                    }

                    foreach (XElement tileElements in tile.Element("properties").Elements())
                    {
                        properties[index] = new Dictionary <string, string>();
                        properties[index].Add(tileElements.Attribute("name").Value, tileElements.Attribute("value").Value);
                    }
                }

                Tileset tileset = new Tileset(Convert.ToInt32(element.Attribute("firstgid").Value), element.Attribute("name").Value, Convert.ToInt32(element.Element("image").Attribute("width").Value), Convert.ToInt32(element.Element("image").Attribute("height").Value), Director.Content.Load <Texture2D>(assetName), tileCollisionGroups, properties);

                Tilesets.Add(tileset);
            }

            // load all layers
            foreach (XElement element in doc.Descendants("layer"))
            {
                TileLayer layer = new TileLayer(element.Attribute("name").Value, Convert.ToInt32(element.Attribute("width").Value), Convert.ToInt32(element.Attribute("height").Value), this);

                int x = 0;
                int y = 0;

                foreach (XElement tile in element.Descendants("tile"))
                {
                    if (Convert.ToInt32(tile.Attribute("gid").Value) == 0)
                    {
                        x++;
                        if (x >= layer.Width)
                        {
                            y++;
                            x = 0;
                        }
                        continue;
                    }

                    layer.addTile(Convert.ToInt32(tile.Attribute("gid").Value), x, y, getCorrectTileset(Convert.ToInt32(tile.Attribute("gid").Value)));
                    x++;

                    // check if y needs to be incremented
                    if (x >= layer.Width)
                    {
                        y++;
                        x = 0;
                    }
                }


                Layers.Add(layer);
            }
        }
Esempio n. 2
0
        public void LoadMap(string path, int tileWidth, int tileHeight)
        {
            Tilemap.tileWidth = tileWidth;
            Tilemap.tileHeight = tileHeight;

            XDocument doc = XDocument.Load(path);

            Width = Convert.ToInt32(doc.Element("map").Attribute("width").Value);
            Height = Convert.ToInt32(doc.Element("map").Attribute("height").Value);

            // load all properties
            foreach (XElement propElement in doc.Element("map").Element("properties").Elements())
            {
                this.Properties.Add(propElement.Attribute("name").Value, propElement.Attribute("value").Value);
            }

            // load all tilesets
            foreach (XElement element in doc.Descendants("tileset"))
            {
                string assetName = element.Element("image").Attribute("source").Value;
                assetName = Path.GetFileNameWithoutExtension(assetName);

                // load all collisionGroups from this tileset ('collisionGroups' from different tiles)
                string[] tileCollisionGroups = new string[100];
                Dictionary<string, string>[] properties = new Dictionary<string, string>[100];

                foreach (XElement tile in element.Descendants("tile"))
                {
                    int index = (int)tile.Attribute("id");

                    if (tile.Descendants("property").First().Attribute("name").Value == "collisionGroups")
                    {

                        string groupName = tile.Descendants("property").First().Attribute("value").Value;

                        tileCollisionGroups[index] = groupName;
                        this.CollisionGroups.Add(groupName);
                    }

                    foreach (XElement tileElements in tile.Element("properties").Elements())
                    {
                        properties[index] = new Dictionary<string, string>();
                        properties[index].Add(tileElements.Attribute("name").Value, tileElements.Attribute("value").Value);
                    }
                }

                Tileset tileset = new Tileset(Convert.ToInt32(element.Attribute("firstgid").Value), element.Attribute("name").Value, Convert.ToInt32(element.Element("image").Attribute("width").Value), Convert.ToInt32(element.Element("image").Attribute("height").Value), GameManager.Content.Load<Texture2D>(assetName), tileCollisionGroups, properties);

                Tilesets.Add(tileset);
            }

            // load all layers
            foreach (XElement element in doc.Descendants("layer"))
            {
                TileLayer layer = new TileLayer(element.Attribute("name").Value, Convert.ToInt32(element.Attribute("width").Value), Convert.ToInt32(element.Attribute("height").Value), this);

                int x = 0;
                int y = 0;

                foreach (XElement tile in element.Descendants("tile"))
                {
                    if (Convert.ToInt32(tile.Attribute("gid").Value) == 0)
                    {
                        x++;
                        if (x >= layer.Width)
                        {
                            y++;
                            x = 0;
                        }
                        continue;
                    }

                    layer.addTile(Convert.ToInt32(tile.Attribute("gid").Value), x, y, getCorrectTileset(Convert.ToInt32(tile.Attribute("gid").Value)));
                    x++;

                    // check if y needs to be incremented
                    if (x >= layer.Width)
                    {
                        y++;
                        x = 0;
                    }
                }

                Layers.Add(layer);
            }
        }