/// <summary> /// Initializes a new instance of the <see cref="TilesetTile" /> class. /// </summary>> /// <param name="tmxTilesetTile">The TMX parsed tileset tile.</param> /// <param name="tileset">The associated tileset</param> public TilesetTile(TmxTilesetTile tmxTilesetTile, Tileset tileset) { this.Tileset = tileset; this.ID = tmxTilesetTile.Id; this.Probability = tmxTilesetTile.Probability; this.Properties = new Dictionary<string, string>(tmxTilesetTile.Properties); this.TerrainEdges = new List<TilesetTerrain>(); if (tmxTilesetTile.BottomRight != null) { this.BottomRight = this.Tileset.Terrains[tmxTilesetTile.BottomRight.Name]; this.TerrainEdges.Add(this.BottomRight); } if (tmxTilesetTile.BottomLeft != null) { this.BottomLeft = this.Tileset.Terrains[tmxTilesetTile.BottomLeft.Name]; this.TerrainEdges.Add(this.BottomLeft); } if (tmxTilesetTile.TopRight != null) { this.TopRight = this.Tileset.Terrains[tmxTilesetTile.TopRight.Name]; this.TerrainEdges.Add(this.TopRight); } if (tmxTilesetTile.TopLeft != null) { this.TopLeft = this.Tileset.Terrains[tmxTilesetTile.TopLeft.Name]; this.TerrainEdges.Add(this.TopLeft); } }
/// <summary> /// Gets the tile rectangle by tile identifier. /// </summary> /// <param name="tileset">The tileset.</param> /// <param name="tileId">The tile identifier.</param> /// <returns>Tile rectangle</returns> public static Rectangle GetRectangleTileByID(Tileset tileset, int tileId) { int rectangleX = (tileId % tileset.XTilesCount); int rectangleY = ((tileId - rectangleX) / tileset.XTilesCount); int x = tileset.Margin + (tileset.TileWidth + tileset.Spacing) * rectangleX; int y = tileset.Margin + (tileset.TileHeight + tileset.Spacing) * rectangleY; return new Rectangle(x, y, tileset.TileWidth, tileset.TileHeight); }
/// <summary> /// Initializes a new instance of the <see cref="LayerTile" /> class. /// </summary>> /// <param name="tmxTile">The TMX parsed tile.</param> /// <param name="tileset">Th</param> public LayerTile(TmxLayerTile tmxTile, Tileset tileset) { this.X = tmxTile.X; this.Y = tmxTile.Y; this.HorizontalFlip = tmxTile.HorizontalFlip; this.VerticalFlip = tmxTile.VerticalFlip; this.DiagonalFlip = tmxTile.DiagonalFlip; if (tileset != null) { this.Tileset = tileset; this.Id = tmxTile.Gid - this.Tileset.FirstGid; this.TilesetTile = this.Tileset.TilesTable[this.Id]; } }
/// <summary> /// Fill VertexBuffer with a tile /// </summary> /// <param name="tileset">The tileset.</param> /// <param name="tile">The tile information.</param> /// <param name="tileIndex">Current tileId</param> private void FillTile(Tileset tileset, LayerTile tile, int tileIndex) { int textureWidth = tileset.Image.Width; int textureHeight = tileset.Image.Height; var rect = TiledMapUtils.GetRectangleTileByID(tileset, tile.Id); RectangleF tileRectangle = new RectangleF( rect.X / (float)textureWidth, rect.Y / (float)textureHeight, rect.Width / (float)textureWidth, rect.Height / (float)textureHeight); int vertexId = tileIndex * verticesPerTile; Vector2 position; this.tiledMap.GetTilePosition(tile.X, tile.Y, tileset, out position); tile.LocalPosition = position; var textCoord0 = new Vector2(tileRectangle.X, tileRectangle.Y); var textCoord1 = new Vector2(tileRectangle.X + tileRectangle.Width, tileRectangle.Y); var textCoord2 = new Vector2(tileRectangle.X + tileRectangle.Width, tileRectangle.Y + tileRectangle.Height); var textCoord3 = new Vector2(tileRectangle.X, tileRectangle.Y + tileRectangle.Height); #region Flip calculation if (tile.HorizontalFlip) { var texCoordAux = textCoord0; textCoord0 = textCoord1; textCoord1 = texCoordAux; texCoordAux = textCoord2; textCoord2 = textCoord3; textCoord3 = texCoordAux; } if (tile.VerticalFlip) { var texCoordAux = textCoord0; textCoord0 = textCoord3; textCoord3 = texCoordAux; texCoordAux = textCoord2; textCoord2 = textCoord1; textCoord1 = texCoordAux; } if (tile.DiagonalFlip) { var texCoordAux = textCoord0; textCoord0 = textCoord2; textCoord2 = texCoordAux; } #endregion // Vertex 0 this.vertices[vertexId].Position = new Vector3(position.X, position.Y, 0); this.vertices[vertexId].Color = Color.White; this.vertices[vertexId].TexCoord = textCoord0; vertexId++; // Vertex 1 this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y, 0); this.vertices[vertexId].Color = Color.White; this.vertices[vertexId].TexCoord = textCoord1; vertexId++; // Vertex 2 this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y + tileset.TileHeight, 0); this.vertices[vertexId].Color = Color.White; this.vertices[vertexId].TexCoord = textCoord2; vertexId++; // Vertex 3 this.vertices[vertexId].Position = new Vector3(position.X, position.Y + tileset.TileHeight, 0); this.vertices[vertexId].Color = Color.White; this.vertices[vertexId].TexCoord = textCoord3; vertexId++; }
/// <summary> /// Create tilesets associated to this map. /// </summary> private void CreateTilesets() { // Load tilesets foreach (var tmxTileset in this.TmxMap.Tilesets) { var tileset = new Tileset(tmxTileset, this); this.tilesets.Add(tileset); } }
/// <summary> /// Obtains the tile position /// </summary> /// <param name="x">The X tile coord.</param> /// <param name="y">The Y tile coord.</param> /// <param name="tileset">The tileset.</param> /// <param name="position">The tile position</param> internal void GetTilePosition(int x, int y, Tileset tileset, out Vector2 position) { if (this.TmxMap == null) { position = Vector2.Zero; return; } switch (this.Orientation) { case TiledMapOrientationType.Orthogonal: position = new Vector2( x * this.TileWidth, y * this.TileHeight); break; case TiledMapOrientationType.Isometric: position = new Vector2( ((x - y) * this.TileWidth * 0.5f) + (this.Height * this.TileWidth * 0.5f) - this.TileWidth * 0.5f, (x + y) * this.TileHeight * 0.5f); break; case TiledMapOrientationType.Staggered: case TiledMapOrientationType.Hexagonal: int sideLengthX = 0; int sideLengthY = 0; float rowSize = 0; float columSize = 0; int staggerIndexSign = this.StaggerIndex == TiledMapStaggerIndexType.Odd ? 1 : -1; var staggerAxisOffset = this.StaggerIndex == TiledMapStaggerIndexType.Even ? 0.5f : 0; if (this.Orientation == TiledMapOrientationType.Hexagonal) { if (this.StaggerAxis == TiledMapStaggerAxisType.X) { sideLengthX = this.HexSideLength; } else { sideLengthY = this.HexSideLength; } } if (this.StaggerAxis == TiledMapStaggerAxisType.X) { rowSize = (x / 2) + ((x % 2) * 0.5f); columSize = staggerAxisOffset + y + ((x % 2) * 0.5f * staggerIndexSign); } else { rowSize = staggerAxisOffset + x + ((y % 2) * 0.5f * staggerIndexSign); columSize = y * 0.5f; } position = new Vector2( rowSize * (this.TileWidth + sideLengthX), columSize * (this.TileHeight + sideLengthY)); break; default: position = Vector2.Zero; break; } if (tileset != null) { position.X += tileset.XDrawingOffset; position.Y += tileset.YDrawingOffset + this.TileHeight - tileset.TileHeight; } }
/// <summary> /// Obtains the tile position /// </summary> /// <param name="tile">The tile</param> /// <param name="tileset">The tileset</param> /// <param name="position">The tile position</param> private void GetTilePosition(LayerTile tile, Tileset tileset, out Vector2 position) { switch (this.tiledMap.Orientation) { case TiledMapOrientationType.Orthogonal: position = new Vector2( tile.X * this.tiledMap.TileWidth, tile.Y * this.tiledMap.TileHeight); break; case TiledMapOrientationType.Isometric: position = new Vector2( ((tile.X - tile.Y) * this.tiledMap.TileWidth * 0.5f) + (this.tiledMap.Height * this.tiledMap.TileWidth * 0.5f) - this.tiledMap.TileWidth * 0.5f, ((tile.X + tile.Y) * this.tiledMap.TileHeight * 0.5f)); break; case TiledMapOrientationType.Staggered: position = new Vector2( (tile.X + ((tile.Y % 2) * 0.5f)) * this.tiledMap.TileWidth, tile.Y * 0.5f * this.tiledMap.TileHeight ); break; default: position = Vector2.Zero; break; } position.Y = position.Y + this.tiledMap.TileHeight - tileset.TileHeight; }
/// <summary> /// Fill VertexBuffer with a tile /// </summary> /// <param name="tileset">The tileset.</param> /// <param name="tile">The tile information.</param> /// <param name="tileIndex">Current tileId</param> private void FillTile(Tileset tileset, LayerTile tile, int tileIndex) { int textureWidth = tileset.Image.Width; int textureHeight = tileset.Image.Height; int tilesetTileWidth = tileset.TileWidth; int tilesetTileHeight = tileset.TileHeight; int spacing = tileset.Spacing; int margin = tileset.Margin; int tileGid = tile.Gid; int sheetIndex = tileGid - tileset.FirstGid; int rectangleX = (sheetIndex % tileset.XTilesCount); int rectangleY = ((sheetIndex - rectangleX) / tileset.XTilesCount); Rectangle rect = new Rectangle( margin + (tilesetTileWidth + spacing) * rectangleX, margin + (tilesetTileHeight + spacing) * rectangleY, tilesetTileWidth, tilesetTileHeight); RectangleF tileRectangle = new RectangleF( rect.X / (float)textureWidth, rect.Y / (float)textureHeight, rect.Width / (float)textureWidth, rect.Height / (float)textureHeight); int vertexId = tileIndex * verticesPerTile; Vector2 position; this.GetTilePosition(tile, tileset, out position); #region Flip calculation - Original version /* float texCoordXStart, texCoordXEnd; float texCoordYStart, texCoordYEnd; if (tile.HorizontalFlip) { texCoordXStart = tileRectangle.X + tileRectangle.Width; texCoordXEnd = tileRectangle.X; texCoordYStart = tileRectangle.Y; texCoordYEnd = tileRectangle.Y + tileRectangle.Height; } else if (tile.VerticalFlip) { texCoordXStart = tileRectangle.X; texCoordXEnd = tileRectangle.X + tileRectangle.Width; texCoordYStart = tileRectangle.Y + tileRectangle.Height; texCoordYEnd = tileRectangle.Y; } else if (tile.DiagonalFlip) { texCoordXStart = tileRectangle.X + tileRectangle.Width; texCoordXEnd = tileRectangle.X; texCoordYStart = tileRectangle.Y + tileRectangle.Height; texCoordYEnd = tileRectangle.Y; } else { texCoordXStart = tileRectangle.X; texCoordXEnd = tileRectangle.X + tileRectangle.Width; texCoordYStart = tileRectangle.Y; texCoordYEnd = tileRectangle.Y + tileRectangle.Height; } // Vertex 0 this.vertices[vertexId].Position = new Vector3(position.X, position.Y, 0); this.vertices[vertexId].Color = Color.White; this.vertices[vertexId].TexCoord = new Vector2(texCoordXStart, texCoordYStart); vertexId++; // Vertex 1 this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y, 0); this.vertices[vertexId].Color = Color.White; this.vertices[vertexId].TexCoord = new Vector2(texCoordXEnd, texCoordYStart); vertexId++; // Vertex 2 this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y + tileset.TileHeight, 0); this.vertices[vertexId].Color = Color.White; this.vertices[vertexId].TexCoord = new Vector2(texCoordXEnd, texCoordYEnd); vertexId++; // Vertex 3 this.vertices[vertexId].Position = new Vector3(position.X, position.Y + tileset.TileHeight, 0); this.vertices[vertexId].Color = Color.White; this.vertices[vertexId].TexCoord = new Vector2(texCoordXStart, texCoordYEnd); vertexId++; */ #endregion Flip calculation - Original version #region Flip calculation - Fixed version var topLeft = new Vector2(tileRectangle.X, tileRectangle.Y); var topRight = new Vector2(tileRectangle.X + tileRectangle.Width, tileRectangle.Y); var bottomRight = new Vector2(tileRectangle.X + tileRectangle.Width, tileRectangle.Y + tileRectangle.Height); var bottomLeft = new Vector2(tileRectangle.X, tileRectangle.Y + tileRectangle.Height); if (tile.DiagonalFlip) { var temp = topRight; topRight = bottomLeft; bottomLeft = temp; } if (tile.HorizontalFlip) { var temp = topLeft; topLeft = topRight; topRight = temp; temp = bottomRight; bottomRight = bottomLeft; bottomLeft = temp; } if (tile.VerticalFlip) { var temp = topLeft; topLeft = bottomLeft; bottomLeft = temp; temp = topRight; topRight = bottomRight; bottomRight = temp; } // Vertex 0 this.vertices[vertexId].Position = new Vector3(position.X, position.Y, 0); this.vertices[vertexId].Color = Color.White; this.vertices[vertexId].TexCoord = topLeft; vertexId++; // Vertex 1 this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y, 0); this.vertices[vertexId].Color = Color.White; this.vertices[vertexId].TexCoord = topRight; vertexId++; // Vertex 2 this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y + tileset.TileHeight, 0); this.vertices[vertexId].Color = Color.White; this.vertices[vertexId].TexCoord = bottomRight; vertexId++; // Vertex 3 this.vertices[vertexId].Position = new Vector3(position.X, position.Y + tileset.TileHeight, 0); this.vertices[vertexId].Color = Color.White; this.vertices[vertexId].TexCoord = bottomLeft; vertexId++; #endregion Flip calculation - Fixed version }