示例#1
0
        public void addTile(int gid, int x, int y, Tileset tileset)
        {
            gid--; // remove 1 because the Tiled editor start counting at 1, instead of 0

            string[] collisionGroups = new string[10];
            if(tileset.CollisionGroups[gid] != null)
                collisionGroups = tileset.CollisionGroups[gid].Split(' ');

            int sourceY = ((gid * Tilemap.tileWidth) / (tileset.Width)) * Tilemap.tileHeight;
            Rectangle sourceRect = new Rectangle(gid * Tilemap.tileWidth - ((sourceY / Tilemap.tileHeight) * tileset.Width), sourceY, Tilemap.tileWidth, Tilemap.tileHeight);

            map[x, y] = new Tile(x, y, gid, sourceRect, tileset, collisionGroups);
            Tiles.Add(map[x, y]);
        }
示例#2
0
        public Tile(int x, int y, int gid, Rectangle sourceRect, Tileset tileset, string[] collisionGroups)
        {
            this.Position = new Vector2(x*Tilemap.tileWidth, y*Tilemap.tileHeight);
            this.gid = gid;
            this.sourceRect = sourceRect;
            this.tileset = tileset;
            this.Immovable = true;
            this.Collidable = false;
            this.Width = Tilemap.tileWidth;
            this.Height = Tilemap.tileHeight;

            for (int i = 0; collisionGroups.Length > i; i++)
                this.AddCollisionGroup(collisionGroups[i]);
        }
示例#3
0
        public Tile(int x, int y, int gid, Rectangle sourceRect, Tileset tileset, string[] collisionGroups)
        {
            this.Position = new Vector2(x*Tilemap.tileWidth, y*Tilemap.tileHeight);
            this.Gid = gid;
            this.SourceRectangle = sourceRect;
            this.Tileset = tileset;
            this.Immovable = true;
            this.Collidable = false;
            this.Width = Tilemap.tileWidth;
            this.Height = Tilemap.tileHeight;
            this.Texture = CurrentTexture;
            CurrentTexture = NormalMap;

            if (NormalMap == null)
                NormalMap = this.Tileset.Texture;

            if (SmallMap == null)
                SmallMap = Controller.Content.Load<Texture2D>("tileset32-small");

            for (int i = 0; collisionGroups.Length > i; i++)
                this.AddCollisionGroup(collisionGroups[i]);
        }
示例#4
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);
            }
        }