internal void PrepareCSVFormatting(TMXMap map) { foreach (TMXLayer layer in map.Layers) { layer.Data.LayerWidth = (int)layer.Width; } }
public Map Load(XmlReader reader) { TMXParser parser = new TMXParser(); TMXMap tmxMap = parser.Parse(reader); return(Load(tmxMap)); }
public Map Load(string path) { TMXParser parser = new TMXParser(); TMXMap tmxMap = parser.Parse(path); return(Load(tmxMap)); }
public Map Load(Stream stream) { TMXParser parser = new TMXParser(); TMXMap tmxMap = parser.Parse(stream); return(Load(tmxMap)); }
internal Tile LoadTile(Layer layer, TMXMap tmxMap, UInt32 gid) { if (gid == 0) { return(null); } TileSheet selectedTileSheet = null; int tileIndex = -1; uint g = gid; bool flipped_horizontally = false; bool flipped_vertically = false; bool flipped_diagonally = false; if (g >= FLIPPED_HORIZONTALLY_FLAG) { flipped_horizontally = true; g -= FLIPPED_HORIZONTALLY_FLAG; } if (g >= FLIPPED_VERTICALLY_FLAG) { flipped_vertically = true; g -= FLIPPED_VERTICALLY_FLAG; } if (g >= FLIPPED_DIAGONALLY_FLAG) { flipped_diagonally = true; } gid &= ~(FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG | FLIPPED_DIAGONALLY_FLAG); foreach (TileSheet tileSheet in layer.Map.TileSheets) { UInt32 property1 = (UInt32)tileSheet.Properties["@FirstGid"]; UInt32 property2 = (UInt32)tileSheet.Properties["@LastGid"]; if (gid >= property1 && gid <= property2) { selectedTileSheet = tileSheet; tileIndex = (int)(gid - property1); break; } } if (selectedTileSheet == null) { throw new Exception(string.Format("Invalid tile gid: {0}", gid)); } Tile result = null; if (tmxMap.Tilesets.Where(ts => ts.Name == selectedTileSheet.Id).FirstOrDefault() is TMXTileset tileset && tileset.Tiles.Where(t => t.Id == tileIndex).FirstOrDefault() is TMXTileSetTile tile && tile.Animations != null && tile.Animations.Count() > 0) { StaticTile[] array = tile.Animations.Select(frame => new StaticTile(layer, selectedTileSheet, BlendMode.Alpha, (int)frame.TileId)).ToArray(); result = new AnimatedTile(layer, array, tile.Animations[0].Duration); }
public TMXMap Parse(XmlReader reader) { TMXMap parsedMap = null; CurrentEncoding = DataEncodingType.XML; if (Serializer.Deserialize(reader) is TMXMap map) { parsedMap = map; } return(parsedMap); }
public void Export(TMXMap map, XmlWriter writer, DataEncodingType dataEncodingType = DataEncodingType.XML) { if (dataEncodingType == DataEncodingType.CSV) { PrepareCSVFormatting(map); } var ns = new XmlSerializerNamespaces(); ns.Add("", ""); CurrentEncoding = dataEncodingType; Serializer.Serialize(writer, map, ns); }
internal void LoadObjects(TMXMap tmxMap, ref Map map) { if (tmxMap.Objectgroups == null || tmxMap.Objectgroups.Count() == 0) { return; } foreach (TMXObjectgroup objectGroup in tmxMap.Objectgroups) { if (map.GetLayer(objectGroup.Name) is Layer layer) { foreach (TMXObject tiledObject in objectGroup.Objects) { if (tiledObject.Name == "TileData") { int tileX = (int)tiledObject.X / FixedTileSize.Width; int tileWidth = (int)tiledObject.Width / FixedTileSize.Height; int tileY = (int)tiledObject.Y / FixedTileSize.Width; int tileHeight = (int)tiledObject.Height / FixedTileSize.Height; for (int x = tileX; x < tileX + tileWidth; x++) { for (int y = tileY; y < tileY + tileHeight; y++) { if (layer.Tiles[x, y] is Tile tile) { foreach (TMXProperty prop in tiledObject.Properties) { if (!tile.Properties.ContainsKey(prop.Name)) { tile.Properties.Add(prop.Name, GetPropertyValue(prop)); } else { tile.Properties[prop.Name] = GetPropertyValue(prop); } } } } } } } } } }
public TMXMap Parse(Stream stream, string path = "") { TMXMap parsedMap = null; CurrentEncoding = DataEncodingType.XML; if (path == "" && stream is FileStream fs) { path = fs.Name; } CurrentDirectory = Path.GetDirectoryName(path); if (Serializer.Deserialize(stream) is TMXMap map) { parsedMap = map; } return(parsedMap); }
public void LoadImageLayers(TMXMap tmxMap, ref Map map) { if (tmxMap.ImageLayers == null || tmxMap.ImageLayers.Count == 0) { return; } foreach (TMXImageLayer layer in tmxMap.ImageLayers) { Size imageSize = new Size(layer.Image.Width, layer.Image.Height); TileSheet imagesheet = new TileSheet("zImageSheet_" + layer.Name, map, layer.Image.Source, new Size(1, 1), imageSize); map.AddTileSheet(imagesheet); Layer imageLayer = new Layer(layer.Name, map, map.Layers[0].LayerSize, FixedTileSizeMultiplied); if (layer.Image.TransparentColor is TMXColor tcolor) { imagesheet.Properties["@TColor"] = tcolor.ToString(); } if (layer.Properties != null) { foreach (TMXProperty prop in layer.Properties) { if (prop.Name == "@Description") { imageLayer.Description = prop.StringValue; } else { imageLayer.Properties[prop.Name] = GetPropertyValue(prop); } } } imageLayer.SetOffset(new Location((int)Math.Floor(layer.Offsetx * TileSizeMultiplier.Width), (int)Math.Floor(layer.Offsety * TileSizeMultiplier.Height))); imageLayer.SetOpacity(layer.Opacity); imageLayer.MakeImageLayer(); imageLayer.SetTileSheetForImageLayer(imagesheet); imageLayer.AfterDraw += ImageLayer_AfterDraw; map.AddLayer(imageLayer); } }
public void LoadTileSets(TMXMap tmxMap, ref Map map) { foreach (var tileSet in tmxMap.Tilesets) { Size sheetSize = new Size(tileSet.Image.Width / tileSet.Tilewidth, tileSet.Image.Height / tileSet.Tileheight); TileSheet tileSheet = new TileSheet(tileSet.Name, map, tileSet.Image.Source, sheetSize, FixedTileSizeMultiplied) { Spacing = new Size(0), Margin = new Size(0) }; tileSheet.Properties["@FirstGid"] = (int)tileSet.Firstgid; tileSheet.Properties["@LastGid"] = (int)tileSet.Firstgid + tileSet.Tilecount - 1; if (tileSet.Tiles != null) { foreach (var tile in tileSet.Tiles.Where(t => t.Properties != null)) { foreach (var prop in tile.Properties) { tileSheet.Properties[string.Format("@TileIndex@{0}@{1}", tile.Id, prop.Name)] = GetPropertyValue(prop); } } } if (tileSet.Properties != null) { foreach (TMXProperty prop in tileSet.Properties) { tileSheet.Properties[prop.Name] = GetPropertyValue(prop); } } map.AddTileSheet(tileSheet); } }
public Map Load(TMXMap tmxMap) { Map map = new Map(); if (tmxMap.Orientation != "orthogonal") { throw new Exception("Only orthogonal Tiled maps are supported."); } TMXProperty[] properties = GetOrdered(tmxMap.Properties); if (properties != null) { foreach (var prop in properties) { if (prop.Name == "@Description") { map.Description = prop.StringValue; } else { map.Properties[prop.Name] = GetPropertyValue(prop); } } } if (tmxMap.Backgroundcolor is TMXColor bg) { map.Properties["@BackgroundColor"] = bg.ToString(); } LoadTileSets(tmxMap, ref map); LoadLayers(tmxMap, ref map); LoadImageLayers(tmxMap, ref map); LoadObjects(tmxMap, ref map); return(map); }
public void Export(TMXMap map, string path, DataEncodingType dataEncodingType = DataEncodingType.XML) { using (Stream stream = new FileStream(path, FileMode.Create)) Export(map, stream, dataEncodingType); }
public void LoadLayers(TMXMap tmxMap, ref Map map) { if (tmxMap.Layers == null) { return; } foreach (TMXLayer layer in tmxMap.Layers) { Layer layer1 = new Layer(layer.Name, map, new Size((int)layer.Width, (int)layer.Height), FixedTileSizeMultiplied); Layer mapLayer = layer1; if (layer.Properties != null) { foreach (TMXProperty prop in layer.Properties) { if (prop.Name == "@Description") { mapLayer.Description = prop.StringValue; } else { mapLayer.Properties[prop.Name] = GetPropertyValue(prop); } } } if (layer.Data != null && layer.Data.Chunks != null && layer.Data.Chunks.Count > 0) { foreach (TMXChunk c in layer.Data.Chunks) { Location origin = new Location(c.X, c.Y); foreach (TMXTile t in c.Tiles) { mapLayer.Tiles[origin] = LoadTile(mapLayer, tmxMap, t.Gid); ++origin.X; if (origin.X >= mapLayer.LayerWidth) { origin.X = 0; ++origin.Y; } } } } else if (layer.Data != null && layer.Data.Tiles != null && layer.Data.Tiles.Count > 0) { Location origin = Location.Origin; foreach (TMXTile t in layer.Data.Tiles) { mapLayer.Tiles[origin] = LoadTile(mapLayer, tmxMap, t.Gid); ++origin.X; if (origin.X >= mapLayer.LayerWidth) { origin.X = 0; ++origin.Y; } } } mapLayer.SetOffset(new Location((int)Math.Floor(layer.Offsetx * TileSizeMultiplier.Width), (int)Math.Floor(layer.Offsety * TileSizeMultiplier.Height))); mapLayer.SetOpacity(layer.Opacity); map.AddLayer(mapLayer); } }