Exemplo n.º 1
0
        public static TMXTileset LoadTileset(TMXTileset tileset, string fileName)
        {
            if (tileset == null)
            {
                tileset = new TMXTileset();
            }
            using (XmlReader reader = XmlReader.Create(fileName)) {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        switch (reader.Name.ToLower())
                        {
                        case "tileset":
                            tileset.Name       = reader.GetAttribute("name");
                            tileset.TileWidth  = Int32.Parse(reader.GetAttribute("tilewidth"));
                            tileset.TileHeight = Int32.Parse(reader.GetAttribute("tileheight"));
                            tileset.TileCount  = Int32.Parse(reader.GetAttribute("tilecount"));
                            tileset.Columns    = Int32.Parse(reader.GetAttribute("columns"));
                            break;

                        case "image":
                            break;

                        case "tile":
                            TMXTile tile = new TMXTile();
                            tile.Id = Int32.Parse(reader.GetAttribute("id"));
                            if (reader.ReadToDescendant("properties"))
                            {
                                if (reader.ReadToDescendant("property"))
                                {
                                    do
                                    {
                                        TMXProperty prop = new TMXProperty();
                                        prop.Name  = reader.GetAttribute("name");
                                        prop.Value = reader.GetAttribute("value");
                                        tile.Properties.Add(prop);
                                        prop = null;
                                    } while (reader.ReadToNextSibling("property"));
                                }
                            }
                            tileset.Tiles.Add(tile);
                            tile = null;
                            break;
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            return(tileset);
        }
Exemplo n.º 2
0
 private TMXTile GetTileById(int id)
 {
     foreach (TMXTileset set in this.Map.Tilesets)
     {
         TMXTile found = set.Tiles.FirstOrDefault(t => (set.FirstGid + t.Id) == id);
         if (found != null)
         {
             return(found);
         }
     }
     return(null);
 }
Exemplo n.º 3
0
        public ElementList ToElementList(string layerName)
        {
            TMXLayer thisLayer = this.Map.Layers.FirstOrDefault(s => s.Name == layerName);

            if (thisLayer == null)
            {
                throw new Exception("Unable to find layer '" + layerName + "'");
            }

            ElementList newElementList = new ElementList();
            MapCellList list           = ParseTMXData(thisLayer.Data);

            int tileX = 0;
            int tileY = 0;

            foreach (MapCell cell in list)
            {
                if (cell.TileID > 0)
                {
                    TMXTile tile = GetTileById(cell.TileID);
                    string  type = tile.Properties.FirstOrDefault(p => p.Name.ToLower() == "type").Value;
                    if (type == null)
                    {
                        type = "0";
                    }
                    string state = tile.Properties.FirstOrDefault(p => p.Name.ToLower() == "state").Value;
                    if (state == null)
                    {
                        state = "0";
                    }

                    Element ele = new Element()
                    {
                        Type  = type,
                        State = state,
                        X     = (tileX * this.Map.TileWidth),
                        Y     = (tileY * this.Map.TileHeight)
                    };
                    newElementList.Add(ele);
                }

                tileX++;
                if (tileX > this.Map.Width)
                {
                    tileX = 0;
                    tileY++;
                }
            }

            return(newElementList);
        }