/// <summary> /// Initializes a new instance of the <see cref="Tileset" /> class. /// </summary> /// <param name="tmxTileset">The TMX parsed tileset</param> /// <param name="tiledMap">The tiled map</param> public Tileset(TmxTileset tmxTileset, TiledMap tiledMap) { this.tiledMap = tiledMap; this.Name = tmxTileset.Name; this.TileWidth = tmxTileset.TileWidth; this.TileHeight = tmxTileset.TileHeight; this.Spacing = tmxTileset.Spacing; this.Margin = tmxTileset.Margin; this.XTilesCount = (int)(tmxTileset.Image.Width - (2 * this.Margin) + this.Spacing) / (this.TileWidth + this.Spacing); this.YTilesCount = (int)(tmxTileset.Image.Height - (2 * this.Margin) + this.Spacing) / (this.TileHeight + this.Spacing); this.FirstGid = tmxTileset.FirstGid; this.LastGid = tmxTileset.FirstGid + (this.XTilesCount * this.YTilesCount) - 1; this.terrains = new Dictionary<string, TilesetTerrain>(); this.Terrains = new ReadOnlyDictionary<string, TilesetTerrain>(this.terrains); foreach (var tmxTerrain in tmxTileset.Terrains) { this.terrains.Add(tmxTerrain.Name, new TilesetTerrain(tmxTerrain)); } this.tiles = new List<TilesetTile>(); this.TilesTable = new TilesetTile[this.XTilesCount * this.YTilesCount]; foreach (var tmxTilesetTile in tmxTileset.Tiles) { var tilesetTile = new TilesetTile(tmxTilesetTile, this); this.tiles.Add(tilesetTile); this.TilesTable[tilesetTile.ID] = tilesetTile; } this.Image = this.tiledMap.Owner.Scene.Assets.LoadAsset<Texture2D>(tmxTileset.Image.Source); }
// TMX tileset element constructor public TmxTileset(XElement xTileset, string tmxDir = "") { var xFirstGid = xTileset.Attribute("firstgid"); var source = (string) xTileset.Attribute("source"); if (source != null) { // Prepend the parent TMX directory if necessary source = Path.Combine(tmxDir, source); // source is always preceded by firstgid FirstGid = (int) xFirstGid; // Everything else is in the TSX file var xDocTileset = ReadXml(source); var ts = new TmxTileset(xDocTileset, TmxDirectory); Name = ts.Name; TileWidth = ts.TileWidth; TileHeight = ts.TileHeight; Spacing = ts.Spacing; Margin = ts.Margin; TileCount = ts.TileCount; TileOffset = ts.TileOffset; Image = ts.Image; Terrains = ts.Terrains; Tiles = ts.Tiles; Properties = ts.Properties; } else { // firstgid is always in TMX, but not TSX if (xFirstGid != null) FirstGid = (int) xFirstGid; Name = (string) xTileset.Attribute("name"); TileWidth = (int) xTileset.Attribute("tilewidth"); TileHeight = (int) xTileset.Attribute("tileheight"); Spacing = (int?) xTileset.Attribute("spacing") ?? 0; Margin = (int?) xTileset.Attribute("margin") ?? 0; TileCount = (int?) xTileset.Attribute("tilecount"); TileOffset = new TmxTileOffset(xTileset.Element("tileoffset")); Image = new TmxImage(xTileset.Element("image"), tmxDir); Terrains = new TmxList<TmxTerrain>(); var xTerrainType = xTileset.Element("terraintypes"); if (xTerrainType != null) { foreach (var e in xTerrainType.Elements("terrain")) Terrains.Add(new TmxTerrain(e)); } Tiles = new Collection<TmxTilesetTile>(); foreach (var xTile in xTileset.Elements("tile")) { var tile = new TmxTilesetTile(xTile, Terrains, tmxDir); Tiles.Add(tile); } Properties = new PropertyDict(xTileset.Element("properties")); } }
public Tileset(TmxTileset tmxTileset) { Texture = Global.GetTexture(tmxTileset.Name); FirstGid = tmxTileset.FirstGid; FinalGid = FirstGid + (int)tmxTileset.TileCount-1; TilesWide = Texture.Width / Global.TileSize; TilesHigh = Texture.Height / Global.TileSize; }
public Tileset(TmxTileset tileset, ContentManager content) { _tileset = tileset; _lastGid = (this.FirstGid - 1) + (((int)tileset.Image.Height / tileset.TileHeight) * ((int)tileset.Image.Width / tileset.TileWidth)); _tilesPerRow = (int)tileset.Image.Width / tileset.TileWidth; if (tileset.Image.Height > 2048 || tileset.Image.Width > 2048) throw new Exception("Textures that are greater than 2048x2048 are not supported!"); // Load the tileset's texture. _texture = ContentManagerUtilities.LoadTexture2D(content, tileset.Image.Source); }
/// <summary> /// Initializes a new instance of the <see cref="Tileset" /> class. /// </summary> /// <param name="tmxTileset">The TMX parsed tileset</param> /// <param name="tiledMap">The tiled map</param> public Tileset(TmxTileset tmxTileset, TiledMap tiledMap) { this.tiledMap = tiledMap; this.Name = tmxTileset.Name; this.TileWidth = tmxTileset.TileWidth; this.TileHeight = tmxTileset.TileHeight; this.Spacing = tmxTileset.Spacing; this.Margin = tmxTileset.Margin; this.XTilesCount = (int)(tmxTileset.Image.Width - (2 * this.Margin) + this.Spacing) / (this.TileWidth + this.Spacing); this.YTilesCount = (int)(tmxTileset.Image.Height - (2 * this.Margin) + this.Spacing) / (this.TileHeight + this.Spacing); this.XDrawingOffset = (int)tmxTileset.TileOffset.X; this.YDrawingOffset = (int)tmxTileset.TileOffset.Y; if (tmxTileset.Colums.HasValue) { this.Colums = tmxTileset.Colums.Value; } this.FirstGid = tmxTileset.FirstGid; this.LastGid = tmxTileset.FirstGid + (this.XTilesCount * this.YTilesCount) - 1; this.terrains = new Dictionary<string, TilesetTerrain>(); this.Terrains = new ReadOnlyDictionary<string, TilesetTerrain>(this.terrains); foreach (var tmxTerrain in tmxTileset.Terrains) { this.terrains.Add(tmxTerrain.Name, new TilesetTerrain(tmxTerrain)); } this.tiles = new List<TilesetTile>(); this.TilesTable = new TilesetTile[this.XTilesCount * this.YTilesCount]; foreach (var tmxTilesetTile in tmxTileset.Tiles) { var tilesetTile = new TilesetTile(tmxTilesetTile, this); this.tiles.Add(tilesetTile); this.TilesTable[tilesetTile.ID] = tilesetTile; } string fullPath = tmxTileset.Image.Source; string relativePath = fullPath.Substring(fullPath.IndexOf("Content")); this.Image = this.tiledMap.Owner.Scene.Assets.LoadAsset<Texture2D>(relativePath); }
public void CreateTileMap(TmxTileset tmxTileSet) { TileMap.TileSize = tmxTileSet.TileWidth; int columnCount = tmxTileSet.Columns; int? NumOfIds = tmxTileSet.TileCount; //create our sprite positions SpritePositions = new Dictionary<int, SpriteHolder>(); for (int i = 0; i < NumOfIds; i++) { int row = (int)Math.Floor((double)(i / columnCount)); int column = i % columnCount; int x = column * TileMap.TileSize; int y = row * TileMap.TileSize; SpritePositions[i] = new SpriteHolder() { origin = new Vector2(x + (2 * column), y + (2 * row)) }; } }
/// <summary> /// This method load images by tileset image source. Then it cropes this image to tiles /// of the same size. At the end, it add this tile-images to dictionary with unic global /// key, made from offset of tile relative tileset. /// </summary> /// <param name="tileset"></param> private void LoadImagesFromCropedImage(TmxTileset tileset) { int tileWidth = tileset.TileWidth; int tileHeight = tileset.TileHeight; int imageWidth = tileset.Image.Width.Value; int imageHeight = tileset.Image.Height.Value; int rowCount = imageHeight / tileHeight; int colCount = imageWidth / tileWidth; BitmapImage sourceBitmap = CreateBitmapImage(tileset.Image.Source, imageWidth, imageHeight); for (int i = 0; i < rowCount; i++) { for (int j = 0; j < colCount; j++) { int offset = tileset.FirstGid + i * colCount + j; Int32Rect rect = new Int32Rect(j * tileWidth, i * tileHeight, tileWidth, tileHeight); BitmapImage croppedBmp = BitmapSourceToBitmapImage(new CroppedBitmap(sourceBitmap, rect)); string key = tileset.Name + "." + offset.ToString(); tmxTilesetsImages.Add(offset, new ResourceImage(key, croppedBmp)); } } }
/// <summary> /// This method load images from image set. Every tile is the same image. At the end, /// it add this images to dictionary with unic global key, made from offset of /// tile image relative to tileset. /// </summary> /// <param name="tileset"></param> private void LoadImagesFromImageSet(TmxTileset tileset) { for (int i = 0; i < tileset.Tiles.Count; i++) { int width = tileset.Tiles[i].Image.Width.Value; int height = tileset.Tiles[i].Image.Height.Value; BitmapImage sourceBitmap = CreateBitmapImage(tileset.Tiles[i].Image.Source, width, height); string key = tileset.Name + "." + i.ToString(); tmxTilesetsImages.Add(tileset.FirstGid + i, new ResourceImage(key, sourceBitmap)); } }
/// <summary> /// This method help us to check if tileset is the set of images or one solid image /// that we have to crop. /// </summary> /// <param name="tileset"></param> private void LoadTilesetImages(TmxTileset tileset) { if (tileset.Image.Source == null) { LoadImagesFromImageSet(tileset); } else { LoadImagesFromCropedImage(tileset); } }
// TMX tileset element constructor public TmxTileset(XElement xTileset, string tmxDir = "", ICustomLoader customLoader = null) : base(customLoader) { var xFirstGid = xTileset.Attribute("firstgid"); var source = (string)xTileset.Attribute("source"); if (source != null) { // Prepend the parent TMX directory if necessary source = Path.Combine(tmxDir, source); // source is always preceded by firstgid FirstGid = (int)xFirstGid; // Everything else is in the TSX file var xDocTileset = ReadXml(source); var ts = new TmxTileset(xDocTileset, TmxDirectory, CustomLoader); Name = ts.Name; TileWidth = ts.TileWidth; TileHeight = ts.TileHeight; Spacing = ts.Spacing; Margin = ts.Margin; Columns = ts.Columns; TileCount = ts.TileCount; TileOffset = ts.TileOffset; Image = ts.Image; Terrains = ts.Terrains; Tiles = ts.Tiles; Properties = ts.Properties; } else { // firstgid is always in TMX, but not TSX if (xFirstGid != null) { FirstGid = (int)xFirstGid; } Name = (string)xTileset.Attribute("name"); TileWidth = (int)xTileset.Attribute("tilewidth"); TileHeight = (int)xTileset.Attribute("tileheight"); Spacing = (int?)xTileset.Attribute("spacing") ?? 0; Margin = (int?)xTileset.Attribute("margin") ?? 0; Columns = (int?)xTileset.Attribute("columns"); TileCount = (int?)xTileset.Attribute("tilecount"); TileOffset = new TmxTileOffset(xTileset.Element("tileoffset")); Image = new TmxImage(xTileset.Element("image"), tmxDir); Terrains = new TmxList <TmxTerrain>(); var xTerrainType = xTileset.Element("terraintypes"); if (xTerrainType != null) { foreach (var e in xTerrainType.Elements("terrain")) { Terrains.Add(new TmxTerrain(e)); } } Tiles = new Dictionary <int, TmxTilesetTile>(); foreach (var xTile in xTileset.Elements("tile")) { var tile = new TmxTilesetTile(xTile, Terrains, tmxDir); Tiles[tile.Id] = tile; } Properties = new PropertyDict(xTileset.Element("properties")); } }